summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPantelis Antoniou <pantelis.antoniou@konsulko.com>2021-09-22 22:45:42 +0300
committerIngy döt Net <ingy@ingy.net>2021-09-23 14:42:00 -0700
commita31a4fb1894c49bad0bf43b942650fb521423bae (patch)
treef97014493c0519724d4d981c0d80d940b7af7553
parentfecae105d743cea9ad6a43ebe9a9974842fdf425 (diff)
downloadpyyaml-git-a31a4fb1894c49bad0bf43b942650fb521423bae.tar.gz
Remove Python 2 support and simplify
It's time to stop pretending this is anymore compatible to version 2 by using macros to hide the fact that on 3 objects are bytes and not string. Removing the support for version 2 makes things clearer. Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
-rw-r--r--yaml/_yaml.h17
-rw-r--r--yaml/_yaml.pxd8
-rw-r--r--yaml/_yaml.pyx339
3 files changed, 109 insertions, 255 deletions
diff --git a/yaml/_yaml.h b/yaml/_yaml.h
index 4c41967..e3984c4 100644
--- a/yaml/_yaml.h
+++ b/yaml/_yaml.h
@@ -1,23 +1,8 @@
#include <yaml.h>
-#if PY_MAJOR_VERSION < 3
-
-#define PyUnicode_FromString(s) PyUnicode_DecodeUTF8((s), strlen(s), "strict")
-
-#else
-
-// really puzzling, but, not being a string is required
-#undef PyString_CheckExact
-#define PyString_CheckExact PyBytes_CheckExact
-#define PyString_AS_STRING PyBytes_AS_STRING
-#define PyString_GET_SIZE PyBytes_GET_SIZE
-#define PyString_FromStringAndSize PyBytes_FromStringAndSize
-
-#endif
-
#define PyUnicode_FromYamlString(s) PyUnicode_FromString((const char *)(void *)(s))
-#define PyString_AS_Yaml_STRING(s) ((yaml_char_t *)PyString_AS_STRING(s))
+#define PyBytes_AS_Yaml_STRING(s) ((yaml_char_t *)PyBytes_AS_STRING(s))
#ifdef _MSC_VER /* MS Visual C++ 6.0 */
#if _MSC_VER == 1200
diff --git a/yaml/_yaml.pxd b/yaml/_yaml.pxd
index 8a1d3d8..713244d 100644
--- a/yaml/_yaml.pxd
+++ b/yaml/_yaml.pxd
@@ -7,8 +7,6 @@ cdef extern from "_yaml.h":
int PyString_CheckExact(object o)
int PyUnicode_CheckExact(object o)
char *PyString_AS_STRING(object o)
- int PyString_GET_SIZE(object o)
- object PyString_FromStringAndSize(char *v, int l)
object PyUnicode_FromString(char *u)
object PyUnicode_DecodeUTF8(char *u, int s, char *e)
object PyUnicode_AsUTF8String(object o)
@@ -17,7 +15,11 @@ cdef extern from "_yaml.h":
ctypedef unsigned char yaml_char_t
object PyUnicode_FromYamlString(void *u)
- yaml_char_t *PyString_AS_Yaml_STRING(object o)
+ yaml_char_t *PyBytes_AS_Yaml_STRING(object o)
+ const char *PyBytes_AS_STRING(object o)
+ int PyBytes_CheckExact(object o)
+ int PyBytes_GET_SIZE(object o)
+ object PyBytes_FromStringAndSize(char *v, int l)
ctypedef enum:
SIZEOF_VOID_P
diff --git a/yaml/_yaml.pyx b/yaml/_yaml.pyx
index 2833bf4..e3e93e2 100644
--- a/yaml/_yaml.pyx
+++ b/yaml/_yaml.pyx
@@ -4,10 +4,7 @@ import yaml
def get_version_string():
cdef const char *value
value = yaml_get_version_string()
- if PY_MAJOR_VERSION < 3:
- return value
- else:
- return PyUnicode_FromString(value)
+ return PyUnicode_FromString(value)
def get_version():
cdef int major, minor, patch
@@ -275,10 +272,7 @@ cdef class CParser:
try:
self.stream_name = stream.name
except AttributeError:
- if PY_MAJOR_VERSION < 3:
- self.stream_name = '<file>'
- else:
- self.stream_name = u'<file>'
+ self.stream_name = u'<file>'
self.stream_cache = None
self.stream_cache_len = 0
self.stream_cache_pos = 0
@@ -286,23 +280,14 @@ cdef class CParser:
else:
if PyUnicode_CheckExact(stream) != 0:
stream = PyUnicode_AsUTF8String(stream)
- if PY_MAJOR_VERSION < 3:
- self.stream_name = '<unicode string>'
- else:
- self.stream_name = u'<unicode string>'
+ self.stream_name = u'<unicode string>'
self.unicode_source = 1
else:
- if PY_MAJOR_VERSION < 3:
- self.stream_name = '<byte string>'
- else:
- self.stream_name = u'<byte string>'
- if PyString_CheckExact(stream) == 0:
- if PY_MAJOR_VERSION < 3:
- raise TypeError("a string or stream input is required")
- else:
- raise TypeError(u"a string or stream input is required")
+ self.stream_name = u'<byte string>'
+ if PyBytes_CheckExact(stream) == 0:
+ raise TypeError(u"a string or stream input is required")
self.stream = stream
- yaml_parser_set_input_string(&self.parser, PyString_AS_Yaml_STRING(stream), PyString_GET_SIZE(stream))
+ yaml_parser_set_input_string(&self.parser, PyBytes_AS_Yaml_STRING(stream), PyBytes_GET_SIZE(stream))
self.current_token = None
self.current_event = None
self.anchors = {}
@@ -318,12 +303,8 @@ cdef class CParser:
if self.parser.error == YAML_MEMORY_ERROR:
return MemoryError
elif self.parser.error == YAML_READER_ERROR:
- if PY_MAJOR_VERSION < 3:
- return ReaderError(self.stream_name, self.parser.problem_offset,
- self.parser.problem_value, '?', self.parser.problem)
- else:
- return ReaderError(self.stream_name, self.parser.problem_offset,
- self.parser.problem_value, u'?', PyUnicode_FromString(self.parser.problem))
+ return ReaderError(self.stream_name, self.parser.problem_offset,
+ self.parser.problem_value, u'?', PyUnicode_FromString(self.parser.problem))
elif self.parser.error == YAML_SCANNER_ERROR \
or self.parser.error == YAML_PARSER_ERROR:
context_mark = None
@@ -340,22 +321,13 @@ cdef class CParser:
self.parser.problem_mark.column, None, None)
context = None
if self.parser.context != NULL:
- if PY_MAJOR_VERSION < 3:
- context = self.parser.context
- else:
- context = PyUnicode_FromString(self.parser.context)
- if PY_MAJOR_VERSION < 3:
- problem = self.parser.problem
- else:
- problem = PyUnicode_FromString(self.parser.problem)
+ context = PyUnicode_FromString(self.parser.context)
+ problem = PyUnicode_FromString(self.parser.problem)
if self.parser.error == YAML_SCANNER_ERROR:
return ScannerError(context, context_mark, problem, problem_mark)
else:
return ParserError(context, context_mark, problem, problem_mark)
- if PY_MAJOR_VERSION < 3:
- raise ValueError("no parser error")
- else:
- raise ValueError(u"no parser error")
+ raise ValueError(u"no parser error")
def raw_scan(self):
cdef yaml_token_t token
@@ -414,8 +386,8 @@ cdef class CParser:
token.data.version_directive.minor),
start_mark, end_mark)
elif token.type == YAML_TAG_DIRECTIVE_TOKEN:
- handle = PyUnicode_FromYamlString(<void *>token.data.tag_directive.handle)
- prefix = PyUnicode_FromYamlString(<void *>token.data.tag_directive.prefix)
+ handle = PyUnicode_FromYamlString(token.data.tag_directive.handle)
+ prefix = PyUnicode_FromYamlString(token.data.tag_directive.prefix)
return DirectiveToken(u"TAG", (handle, prefix),
start_mark, end_mark)
elif token.type == YAML_DOCUMENT_START_TOKEN:
@@ -445,14 +417,14 @@ cdef class CParser:
elif token.type == YAML_VALUE_TOKEN:
return ValueToken(start_mark, end_mark)
elif token.type == YAML_ALIAS_TOKEN:
- value = PyUnicode_FromYamlString(<void *>token.data.alias.value)
+ value = PyUnicode_FromYamlString(token.data.alias.value)
return AliasToken(value, start_mark, end_mark)
elif token.type == YAML_ANCHOR_TOKEN:
- value = PyUnicode_FromYamlString(<void *>token.data.anchor.value)
+ value = PyUnicode_FromYamlString(token.data.anchor.value)
return AnchorToken(value, start_mark, end_mark)
elif token.type == YAML_TAG_TOKEN:
- handle = PyUnicode_FromYamlString(<void *>token.data.tag.handle)
- suffix = PyUnicode_FromYamlString(<void *>token.data.tag.suffix)
+ handle = PyUnicode_FromYamlString(token.data.tag.handle)
+ suffix = PyUnicode_FromYamlString(token.data.tag.suffix)
if not handle:
handle = None
return TagToken((handle, suffix), start_mark, end_mark)
@@ -475,10 +447,7 @@ cdef class CParser:
return ScalarToken(value, plain,
start_mark, end_mark, style)
else:
- if PY_MAJOR_VERSION < 3:
- raise ValueError("unknown token type")
- else:
- raise ValueError(u"unknown token type")
+ raise ValueError(u"unknown token type")
def get_token(self):
if self.current_token is not None:
@@ -571,8 +540,8 @@ cdef class CParser:
tags = {}
tag_directive = event.data.document_start.tag_directives.start
while tag_directive != event.data.document_start.tag_directives.end:
- handle = PyUnicode_FromYamlString(<void *>tag_directive.handle)
- prefix = PyUnicode_FromYamlString(<void *>tag_directive.prefix)
+ handle = PyUnicode_FromYamlString(tag_directive.handle)
+ prefix = PyUnicode_FromYamlString(tag_directive.prefix)
tags[handle] = prefix
tag_directive = tag_directive+1
return DocumentStartEvent(start_mark, end_mark,
@@ -583,15 +552,15 @@ cdef class CParser:
explicit = True
return DocumentEndEvent(start_mark, end_mark, explicit)
elif event.type == YAML_ALIAS_EVENT:
- anchor = PyUnicode_FromYamlString(<void *>event.data.alias.anchor)
+ anchor = PyUnicode_FromYamlString(event.data.alias.anchor)
return AliasEvent(anchor, start_mark, end_mark)
elif event.type == YAML_SCALAR_EVENT:
anchor = None
if event.data.scalar.anchor != NULL:
- anchor = PyUnicode_FromYamlString(<void *>event.data.scalar.anchor)
+ anchor = PyUnicode_FromYamlString(event.data.scalar.anchor)
tag = None
if event.data.scalar.tag != NULL:
- tag = PyUnicode_FromYamlString(<void *>event.data.scalar.tag)
+ tag = PyUnicode_FromYamlString(event.data.scalar.tag)
value = PyUnicode_DecodeUTF8(<char *>event.data.scalar.value,
event.data.scalar.length, 'strict')
plain_implicit = False
@@ -617,10 +586,10 @@ cdef class CParser:
elif event.type == YAML_SEQUENCE_START_EVENT:
anchor = None
if event.data.sequence_start.anchor != NULL:
- anchor = PyUnicode_FromYamlString(<void *>event.data.sequence_start.anchor)
+ anchor = PyUnicode_FromYamlString(event.data.sequence_start.anchor)
tag = None
if event.data.sequence_start.tag != NULL:
- tag = PyUnicode_FromYamlString(<void *>event.data.sequence_start.tag)
+ tag = PyUnicode_FromYamlString(event.data.sequence_start.tag)
implicit = False
if event.data.sequence_start.implicit == 1:
implicit = True
@@ -634,7 +603,7 @@ cdef class CParser:
elif event.type == YAML_MAPPING_START_EVENT:
anchor = None
if event.data.mapping_start.anchor != NULL:
- anchor = PyUnicode_FromYamlString(<void *>event.data.mapping_start.anchor)
+ anchor = PyUnicode_FromYamlString(event.data.mapping_start.anchor)
tag = None
if event.data.mapping_start.tag != NULL:
tag = PyUnicode_FromYamlString(event.data.mapping_start.tag)
@@ -653,10 +622,7 @@ cdef class CParser:
elif event.type == YAML_MAPPING_END_EVENT:
return MappingEndEvent(start_mark, end_mark)
else:
- if PY_MAJOR_VERSION < 3:
- raise ValueError("unknown event type")
- else:
- raise ValueError(u"unknown event type")
+ raise ValueError(u"unknown event type")
def get_event(self):
if self.current_event is not None:
@@ -712,12 +678,8 @@ cdef class CParser:
self.parsed_event.start_mark.line,
self.parsed_event.start_mark.column,
None, None)
- if PY_MAJOR_VERSION < 3:
- raise ComposerError("expected a single document in the stream",
- document.start_mark, "but found another document", mark)
- else:
- raise ComposerError(u"expected a single document in the stream",
- document.start_mark, u"but found another document", mark)
+ raise ComposerError(u"expected a single document in the stream",
+ document.start_mark, u"but found another document", mark)
return document
cdef object _compose_document(self):
@@ -738,10 +700,7 @@ cdef class CParser:
self.parsed_event.start_mark.line,
self.parsed_event.start_mark.column,
None, None)
- if PY_MAJOR_VERSION < 3:
- raise ComposerError(None, None, "found undefined alias", mark)
- else:
- raise ComposerError(None, None, u"found undefined alias", mark)
+ raise ComposerError(None, None, u"found undefined alias", mark)
yaml_event_delete(&self.parsed_event)
return self.anchors[anchor]
anchor = None
@@ -761,12 +720,8 @@ cdef class CParser:
self.parsed_event.start_mark.line,
self.parsed_event.start_mark.column,
None, None)
- if PY_MAJOR_VERSION < 3:
- raise ComposerError("found duplicate anchor; first occurrence",
- self.anchors[anchor].start_mark, "second occurrence", mark)
- else:
- raise ComposerError(u"found duplicate anchor; first occurrence",
- self.anchors[anchor].start_mark, u"second occurrence", mark)
+ raise ComposerError(u"found duplicate anchor; first occurrence",
+ self.anchors[anchor].start_mark, u"second occurrence", mark)
self.descend_resolver(parent, index)
if self.parsed_event.type == YAML_SCALAR_EVENT:
node = self._compose_scalar_node(anchor)
@@ -913,18 +868,15 @@ cdef int input_handler(void *data, unsigned char *buffer, size_t size, size_t *r
if PyUnicode_CheckExact(value) != 0:
value = PyUnicode_AsUTF8String(value)
parser.unicode_source = 1
- if PyString_CheckExact(value) == 0:
- if PY_MAJOR_VERSION < 3:
- raise TypeError("a string value is expected")
- else:
- raise TypeError(u"a string value is expected")
+ if PyBytes_CheckExact(value) == 0:
+ raise TypeError(u"a string value is expected")
parser.stream_cache = value
parser.stream_cache_pos = 0
- parser.stream_cache_len = PyString_GET_SIZE(value)
+ parser.stream_cache_len = PyBytes_GET_SIZE(value)
if (parser.stream_cache_len - parser.stream_cache_pos) < <int>size:
size = parser.stream_cache_len - parser.stream_cache_pos
if size > 0:
- memcpy(buffer, PyString_AS_STRING(parser.stream_cache)
+ memcpy(buffer, PyBytes_AS_STRING(parser.stream_cache)
+ parser.stream_cache_pos, size)
read[0] = size
parser.stream_cache_pos += size
@@ -957,12 +909,8 @@ cdef class CEmitter:
raise MemoryError
self.stream = stream
self.dump_unicode = 0
- if PY_MAJOR_VERSION < 3:
- if getattr3(stream, 'encoding', None):
- self.dump_unicode = 1
- else:
- if hasattr(stream, u'encoding'):
- self.dump_unicode = 1
+ if hasattr(stream, u'encoding'):
+ self.dump_unicode = 1
self.use_encoding = encoding
yaml_emitter_set_output(&self.emitter, output_handler, <void *>self)
if canonical:
@@ -1003,15 +951,9 @@ cdef class CEmitter:
if self.emitter.error == YAML_MEMORY_ERROR:
return MemoryError
elif self.emitter.error == YAML_EMITTER_ERROR:
- if PY_MAJOR_VERSION < 3:
- problem = self.emitter.problem
- else:
- problem = PyUnicode_FromString(self.emitter.problem)
+ problem = PyUnicode_FromString(self.emitter.problem)
return EmitterError(problem)
- if PY_MAJOR_VERSION < 3:
- raise ValueError("no emitter error")
- else:
- raise ValueError(u"no emitter error")
+ raise ValueError(u"no emitter error")
cdef int _object_to_event(self, object event_object, yaml_event_t *event) except 0:
cdef yaml_encoding_t encoding
@@ -1054,10 +996,7 @@ cdef class CEmitter:
tag_directives_end = NULL
if event_object.tags:
if len(event_object.tags) > 128:
- if PY_MAJOR_VERSION < 3:
- raise ValueError("too many tags")
- else:
- raise ValueError(u"too many tags")
+ raise ValueError(u"too many tags")
tag_directives_start = tag_directives_value
tag_directives_end = tag_directives_value
cache = []
@@ -1066,21 +1005,15 @@ cdef class CEmitter:
if PyUnicode_CheckExact(handle):
handle = PyUnicode_AsUTF8String(handle)
cache.append(handle)
- if not PyString_CheckExact(handle):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag handle must be a string")
- else:
- raise TypeError(u"tag handle must be a string")
- tag_directives_end.handle = PyString_AS_Yaml_STRING(handle)
+ if not PyBytes_CheckExact(handle):
+ raise TypeError(u"tag handle must be a string")
+ tag_directives_end.handle = PyBytes_AS_Yaml_STRING(handle)
if PyUnicode_CheckExact(prefix):
prefix = PyUnicode_AsUTF8String(prefix)
cache.append(prefix)
- if not PyString_CheckExact(prefix):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag prefix must be a string")
- else:
- raise TypeError(u"tag prefix must be a string")
- tag_directives_end.prefix = PyString_AS_Yaml_STRING(prefix)
+ if not PyBytes_CheckExact(prefix):
+ raise TypeError(u"tag prefix must be a string")
+ tag_directives_end.prefix = PyBytes_AS_Yaml_STRING(prefix)
tag_directives_end = tag_directives_end+1
implicit = 1
if event_object.explicit:
@@ -1098,12 +1031,9 @@ cdef class CEmitter:
anchor_object = event_object.anchor
if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object)
- if not PyString_CheckExact(anchor_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("anchor must be a string")
- else:
- raise TypeError(u"anchor must be a string")
- anchor = PyString_AS_Yaml_STRING(anchor_object)
+ if not PyBytes_CheckExact(anchor_object):
+ raise TypeError(u"anchor must be a string")
+ anchor = PyBytes_AS_Yaml_STRING(anchor_object)
if yaml_alias_event_initialize(event, anchor) == 0:
raise MemoryError
elif event_class is ScalarEvent:
@@ -1112,33 +1042,24 @@ cdef class CEmitter:
if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object)
- if not PyString_CheckExact(anchor_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("anchor must be a string")
- else:
- raise TypeError(u"anchor must be a string")
- anchor = PyString_AS_Yaml_STRING(anchor_object)
+ if not PyBytes_CheckExact(anchor_object):
+ raise TypeError(u"anchor must be a string")
+ anchor = PyBytes_AS_Yaml_STRING(anchor_object)
tag = NULL
tag_object = event_object.tag
if tag_object is not None:
if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object)
- if not PyString_CheckExact(tag_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag must be a string")
- else:
- raise TypeError(u"tag must be a string")
- tag = PyString_AS_Yaml_STRING(tag_object)
+ if not PyBytes_CheckExact(tag_object):
+ raise TypeError(u"tag must be a string")
+ tag = PyBytes_AS_Yaml_STRING(tag_object)
value_object = event_object.value
if PyUnicode_CheckExact(value_object):
value_object = PyUnicode_AsUTF8String(value_object)
- if not PyString_CheckExact(value_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("value must be a string")
- else:
- raise TypeError(u"value must be a string")
- value = PyString_AS_Yaml_STRING(value_object)
- length = PyString_GET_SIZE(value_object)
+ if not PyBytes_CheckExact(value_object):
+ raise TypeError(u"value must be a string")
+ value = PyBytes_AS_Yaml_STRING(value_object)
+ length = PyBytes_GET_SIZE(value_object)
plain_implicit = 0
quoted_implicit = 0
if event_object.implicit is not None:
@@ -1163,23 +1084,17 @@ cdef class CEmitter:
if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object)
- if not PyString_CheckExact(anchor_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("anchor must be a string")
- else:
- raise TypeError(u"anchor must be a string")
- anchor = PyString_AS_Yaml_STRING(anchor_object)
+ if not PyBytes_CheckExact(anchor_object):
+ raise TypeError(u"anchor must be a string")
+ anchor = PyBytes_AS_Yaml_STRING(anchor_object)
tag = NULL
tag_object = event_object.tag
if tag_object is not None:
if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object)
- if not PyString_CheckExact(tag_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag must be a string")
- else:
- raise TypeError(u"tag must be a string")
- tag = PyString_AS_Yaml_STRING(tag_object)
+ if not PyBytes_CheckExact(tag_object):
+ raise TypeError(u"tag must be a string")
+ tag = PyBytes_AS_Yaml_STRING(tag_object)
implicit = 0
if event_object.implicit:
implicit = 1
@@ -1195,23 +1110,17 @@ cdef class CEmitter:
if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object)
- if not PyString_CheckExact(anchor_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("anchor must be a string")
- else:
- raise TypeError(u"anchor must be a string")
- anchor = PyString_AS_Yaml_STRING(anchor_object)
+ if not PyBytes_CheckExact(anchor_object):
+ raise TypeError(u"anchor must be a string")
+ anchor = PyBytes_AS_Yaml_STRING(anchor_object)
tag = NULL
tag_object = event_object.tag
if tag_object is not None:
if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object)
- if not PyString_CheckExact(tag_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag must be a string")
- else:
- raise TypeError(u"tag must be a string")
- tag = PyString_AS_Yaml_STRING(tag_object)
+ if not PyBytes_CheckExact(tag_object):
+ raise TypeError(u"tag must be a string")
+ tag = PyBytes_AS_Yaml_STRING(tag_object)
implicit = 0
if event_object.implicit:
implicit = 1
@@ -1226,10 +1135,7 @@ cdef class CEmitter:
elif event_class is MappingEndEvent:
yaml_mapping_end_event_initialize(event)
else:
- if PY_MAJOR_VERSION < 3:
- raise TypeError("invalid event %s" % event_object)
- else:
- raise TypeError(u"invalid event %s" % event_object)
+ raise TypeError(u"invalid event %s" % event_object)
return 1
def emit(self, event_object):
@@ -1259,23 +1165,14 @@ cdef class CEmitter:
raise error
self.closed = 0
elif self.closed == 1:
- if PY_MAJOR_VERSION < 3:
- raise SerializerError("serializer is closed")
- else:
- raise SerializerError(u"serializer is closed")
+ raise SerializerError(u"serializer is closed")
else:
- if PY_MAJOR_VERSION < 3:
- raise SerializerError("serializer is already opened")
- else:
- raise SerializerError(u"serializer is already opened")
+ raise SerializerError(u"serializer is already opened")
def close(self):
cdef yaml_event_t event
if self.closed == -1:
- if PY_MAJOR_VERSION < 3:
- raise SerializerError("serializer is not opened")
- else:
- raise SerializerError(u"serializer is not opened")
+ raise SerializerError(u"serializer is not opened")
elif self.closed == 0:
yaml_stream_end_event_initialize(&event)
if yaml_emitter_emit(&self.emitter, &event) == 0:
@@ -1291,15 +1188,9 @@ cdef class CEmitter:
cdef yaml_tag_directive_t *tag_directives_start
cdef yaml_tag_directive_t *tag_directives_end
if self.closed == -1:
- if PY_MAJOR_VERSION < 3:
- raise SerializerError("serializer is not opened")
- else:
- raise SerializerError(u"serializer is not opened")
+ raise SerializerError(u"serializer is not opened")
elif self.closed == 1:
- if PY_MAJOR_VERSION < 3:
- raise SerializerError("serializer is closed")
- else:
- raise SerializerError(u"serializer is closed")
+ raise SerializerError(u"serializer is closed")
cache = []
version_directive = NULL
if self.use_version:
@@ -1310,10 +1201,7 @@ cdef class CEmitter:
tag_directives_end = NULL
if self.use_tags:
if len(self.use_tags) > 128:
- if PY_MAJOR_VERSION < 3:
- raise ValueError("too many tags")
- else:
- raise ValueError(u"too many tags")
+ raise ValueError(u"too many tags")
tag_directives_start = tag_directives_value
tag_directives_end = tag_directives_value
for handle in self.use_tags:
@@ -1321,21 +1209,15 @@ cdef class CEmitter:
if PyUnicode_CheckExact(handle):
handle = PyUnicode_AsUTF8String(handle)
cache.append(handle)
- if not PyString_CheckExact(handle):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag handle must be a string")
- else:
- raise TypeError(u"tag handle must be a string")
- tag_directives_end.handle = PyString_AS_Yaml_STRING(handle)
+ if not PyBytes_CheckExact(handle):
+ raise TypeError(u"tag handle must be a string")
+ tag_directives_end.handle = PyBytes_AS_Yaml_STRING(handle)
if PyUnicode_CheckExact(prefix):
prefix = PyUnicode_AsUTF8String(prefix)
cache.append(prefix)
- if not PyString_CheckExact(prefix):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag prefix must be a string")
- else:
- raise TypeError(u"tag prefix must be a string")
- tag_directives_end.prefix = PyString_AS_Yaml_STRING(prefix)
+ if not PyBytes_CheckExact(prefix):
+ raise TypeError(u"tag prefix must be a string")
+ tag_directives_end.prefix = PyBytes_AS_Yaml_STRING(prefix)
tag_directives_end = tag_directives_end+1
if yaml_document_start_event_initialize(&event, version_directive,
tag_directives_start, tag_directives_end,
@@ -1389,12 +1271,9 @@ cdef class CEmitter:
if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object)
- if not PyString_CheckExact(anchor_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("anchor must be a string")
- else:
- raise TypeError(u"anchor must be a string")
- anchor = PyString_AS_Yaml_STRING(anchor_object)
+ if not PyBytes_CheckExact(anchor_object):
+ raise TypeError(u"anchor must be a string")
+ anchor = PyBytes_AS_Yaml_STRING(anchor_object)
if node in self.serialized_nodes:
if yaml_alias_event_initialize(&event, anchor) == 0:
raise MemoryError
@@ -1417,22 +1296,16 @@ cdef class CEmitter:
if tag_object is not None:
if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object)
- if not PyString_CheckExact(tag_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag must be a string")
- else:
- raise TypeError(u"tag must be a string")
- tag = PyString_AS_Yaml_STRING(tag_object)
+ if not PyBytes_CheckExact(tag_object):
+ raise TypeError(u"tag must be a string")
+ tag = PyBytes_AS_Yaml_STRING(tag_object)
value_object = node.value
if PyUnicode_CheckExact(value_object):
value_object = PyUnicode_AsUTF8String(value_object)
- if not PyString_CheckExact(value_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("value must be a string")
- else:
- raise TypeError(u"value must be a string")
- value = PyString_AS_Yaml_STRING(value_object)
- length = PyString_GET_SIZE(value_object)
+ if not PyBytes_CheckExact(value_object):
+ raise TypeError(u"value must be a string")
+ value = PyBytes_AS_Yaml_STRING(value_object)
+ length = PyBytes_GET_SIZE(value_object)
style_object = node.style
scalar_style = YAML_PLAIN_SCALAR_STYLE
if style_object == "'" or style_object == u"'":
@@ -1458,12 +1331,9 @@ cdef class CEmitter:
if tag_object is not None:
if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object)
- if not PyString_CheckExact(tag_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag must be a string")
- else:
- raise TypeError(u"tag must be a string")
- tag = PyString_AS_Yaml_STRING(tag_object)
+ if not PyBytes_CheckExact(tag_object):
+ raise TypeError(u"tag must be a string")
+ tag = PyBytes_AS_Yaml_STRING(tag_object)
sequence_style = YAML_BLOCK_SEQUENCE_STYLE
if node.flow_style:
sequence_style = YAML_FLOW_SEQUENCE_STYLE
@@ -1490,12 +1360,9 @@ cdef class CEmitter:
if tag_object is not None:
if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object)
- if not PyString_CheckExact(tag_object):
- if PY_MAJOR_VERSION < 3:
- raise TypeError("tag must be a string")
- else:
- raise TypeError(u"tag must be a string")
- tag = PyString_AS_Yaml_STRING(tag_object)
+ if not PyBytes_CheckExact(tag_object):
+ raise TypeError(u"tag must be a string")
+ tag = PyBytes_AS_Yaml_STRING(tag_object)
mapping_style = YAML_BLOCK_MAPPING_STYLE
if node.flow_style:
mapping_style = YAML_FLOW_MAPPING_STYLE
@@ -1521,7 +1388,7 @@ cdef int output_handler(void *data, unsigned char *bufferu, size_t size) except
buffer = <char *>bufferu
emitter = <CEmitter>data
if emitter.dump_unicode == 0:
- value = PyString_FromStringAndSize(buffer, size)
+ value = PyBytes_FromStringAndSize(buffer, size)
else:
value = PyUnicode_DecodeUTF8(buffer, size, 'strict')
emitter.stream.write(value)