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>
This commit is contained in:
Pantelis Antoniou 2021-09-22 22:45:42 +03:00 committed by Ingy döt Net
parent fecae105d7
commit a31a4fb189
3 changed files with 109 additions and 255 deletions

View File

@ -1,23 +1,8 @@
#include <yaml.h> #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 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 */ #ifdef _MSC_VER /* MS Visual C++ 6.0 */
#if _MSC_VER == 1200 #if _MSC_VER == 1200

View File

@ -7,8 +7,6 @@ cdef extern from "_yaml.h":
int PyString_CheckExact(object o) int PyString_CheckExact(object o)
int PyUnicode_CheckExact(object o) int PyUnicode_CheckExact(object o)
char *PyString_AS_STRING(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_FromString(char *u)
object PyUnicode_DecodeUTF8(char *u, int s, char *e) object PyUnicode_DecodeUTF8(char *u, int s, char *e)
object PyUnicode_AsUTF8String(object o) object PyUnicode_AsUTF8String(object o)
@ -17,7 +15,11 @@ cdef extern from "_yaml.h":
ctypedef unsigned char yaml_char_t ctypedef unsigned char yaml_char_t
object PyUnicode_FromYamlString(void *u) 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: ctypedef enum:
SIZEOF_VOID_P SIZEOF_VOID_P

View File

@ -4,10 +4,7 @@ import yaml
def get_version_string(): def get_version_string():
cdef const char *value cdef const char *value
value = yaml_get_version_string() value = yaml_get_version_string()
if PY_MAJOR_VERSION < 3: return PyUnicode_FromString(value)
return value
else:
return PyUnicode_FromString(value)
def get_version(): def get_version():
cdef int major, minor, patch cdef int major, minor, patch
@ -275,10 +272,7 @@ cdef class CParser:
try: try:
self.stream_name = stream.name self.stream_name = stream.name
except AttributeError: except AttributeError:
if PY_MAJOR_VERSION < 3: self.stream_name = u'<file>'
self.stream_name = '<file>'
else:
self.stream_name = u'<file>'
self.stream_cache = None self.stream_cache = None
self.stream_cache_len = 0 self.stream_cache_len = 0
self.stream_cache_pos = 0 self.stream_cache_pos = 0
@ -286,23 +280,14 @@ cdef class CParser:
else: else:
if PyUnicode_CheckExact(stream) != 0: if PyUnicode_CheckExact(stream) != 0:
stream = PyUnicode_AsUTF8String(stream) stream = PyUnicode_AsUTF8String(stream)
if PY_MAJOR_VERSION < 3: self.stream_name = u'<unicode string>'
self.stream_name = '<unicode string>'
else:
self.stream_name = u'<unicode string>'
self.unicode_source = 1 self.unicode_source = 1
else: else:
if PY_MAJOR_VERSION < 3: self.stream_name = u'<byte string>'
self.stream_name = '<byte string>' if PyBytes_CheckExact(stream) == 0:
else: raise TypeError(u"a string or stream input is required")
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 = stream 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_token = None
self.current_event = None self.current_event = None
self.anchors = {} self.anchors = {}
@ -318,12 +303,8 @@ cdef class CParser:
if self.parser.error == YAML_MEMORY_ERROR: if self.parser.error == YAML_MEMORY_ERROR:
return MemoryError return MemoryError
elif self.parser.error == YAML_READER_ERROR: elif self.parser.error == YAML_READER_ERROR:
if PY_MAJOR_VERSION < 3: return ReaderError(self.stream_name, self.parser.problem_offset,
return ReaderError(self.stream_name, self.parser.problem_offset, self.parser.problem_value, u'?', PyUnicode_FromString(self.parser.problem))
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))
elif self.parser.error == YAML_SCANNER_ERROR \ elif self.parser.error == YAML_SCANNER_ERROR \
or self.parser.error == YAML_PARSER_ERROR: or self.parser.error == YAML_PARSER_ERROR:
context_mark = None context_mark = None
@ -340,22 +321,13 @@ cdef class CParser:
self.parser.problem_mark.column, None, None) self.parser.problem_mark.column, None, None)
context = None context = None
if self.parser.context != NULL: if self.parser.context != NULL:
if PY_MAJOR_VERSION < 3: context = PyUnicode_FromString(self.parser.context)
context = self.parser.context problem = PyUnicode_FromString(self.parser.problem)
else:
context = PyUnicode_FromString(self.parser.context)
if PY_MAJOR_VERSION < 3:
problem = self.parser.problem
else:
problem = PyUnicode_FromString(self.parser.problem)
if self.parser.error == YAML_SCANNER_ERROR: if self.parser.error == YAML_SCANNER_ERROR:
return ScannerError(context, context_mark, problem, problem_mark) return ScannerError(context, context_mark, problem, problem_mark)
else: else:
return ParserError(context, context_mark, problem, problem_mark) return ParserError(context, context_mark, problem, problem_mark)
if PY_MAJOR_VERSION < 3: raise ValueError(u"no parser error")
raise ValueError("no parser error")
else:
raise ValueError(u"no parser error")
def raw_scan(self): def raw_scan(self):
cdef yaml_token_t token cdef yaml_token_t token
@ -414,8 +386,8 @@ cdef class CParser:
token.data.version_directive.minor), token.data.version_directive.minor),
start_mark, end_mark) start_mark, end_mark)
elif token.type == YAML_TAG_DIRECTIVE_TOKEN: elif token.type == YAML_TAG_DIRECTIVE_TOKEN:
handle = PyUnicode_FromYamlString(<void *>token.data.tag_directive.handle) handle = PyUnicode_FromYamlString(token.data.tag_directive.handle)
prefix = PyUnicode_FromYamlString(<void *>token.data.tag_directive.prefix) prefix = PyUnicode_FromYamlString(token.data.tag_directive.prefix)
return DirectiveToken(u"TAG", (handle, prefix), return DirectiveToken(u"TAG", (handle, prefix),
start_mark, end_mark) start_mark, end_mark)
elif token.type == YAML_DOCUMENT_START_TOKEN: elif token.type == YAML_DOCUMENT_START_TOKEN:
@ -445,14 +417,14 @@ cdef class CParser:
elif token.type == YAML_VALUE_TOKEN: elif token.type == YAML_VALUE_TOKEN:
return ValueToken(start_mark, end_mark) return ValueToken(start_mark, end_mark)
elif token.type == YAML_ALIAS_TOKEN: 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) return AliasToken(value, start_mark, end_mark)
elif token.type == YAML_ANCHOR_TOKEN: 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) return AnchorToken(value, start_mark, end_mark)
elif token.type == YAML_TAG_TOKEN: elif token.type == YAML_TAG_TOKEN:
handle = PyUnicode_FromYamlString(<void *>token.data.tag.handle) handle = PyUnicode_FromYamlString(token.data.tag.handle)
suffix = PyUnicode_FromYamlString(<void *>token.data.tag.suffix) suffix = PyUnicode_FromYamlString(token.data.tag.suffix)
if not handle: if not handle:
handle = None handle = None
return TagToken((handle, suffix), start_mark, end_mark) return TagToken((handle, suffix), start_mark, end_mark)
@ -475,10 +447,7 @@ cdef class CParser:
return ScalarToken(value, plain, return ScalarToken(value, plain,
start_mark, end_mark, style) start_mark, end_mark, style)
else: else:
if PY_MAJOR_VERSION < 3: raise ValueError(u"unknown token type")
raise ValueError("unknown token type")
else:
raise ValueError(u"unknown token type")
def get_token(self): def get_token(self):
if self.current_token is not None: if self.current_token is not None:
@ -571,8 +540,8 @@ cdef class CParser:
tags = {} tags = {}
tag_directive = event.data.document_start.tag_directives.start tag_directive = event.data.document_start.tag_directives.start
while tag_directive != event.data.document_start.tag_directives.end: while tag_directive != event.data.document_start.tag_directives.end:
handle = PyUnicode_FromYamlString(<void *>tag_directive.handle) handle = PyUnicode_FromYamlString(tag_directive.handle)
prefix = PyUnicode_FromYamlString(<void *>tag_directive.prefix) prefix = PyUnicode_FromYamlString(tag_directive.prefix)
tags[handle] = prefix tags[handle] = prefix
tag_directive = tag_directive+1 tag_directive = tag_directive+1
return DocumentStartEvent(start_mark, end_mark, return DocumentStartEvent(start_mark, end_mark,
@ -583,15 +552,15 @@ cdef class CParser:
explicit = True explicit = True
return DocumentEndEvent(start_mark, end_mark, explicit) return DocumentEndEvent(start_mark, end_mark, explicit)
elif event.type == YAML_ALIAS_EVENT: 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) return AliasEvent(anchor, start_mark, end_mark)
elif event.type == YAML_SCALAR_EVENT: elif event.type == YAML_SCALAR_EVENT:
anchor = None anchor = None
if event.data.scalar.anchor != NULL: if event.data.scalar.anchor != NULL:
anchor = PyUnicode_FromYamlString(<void *>event.data.scalar.anchor) anchor = PyUnicode_FromYamlString(event.data.scalar.anchor)
tag = None tag = None
if event.data.scalar.tag != NULL: 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, value = PyUnicode_DecodeUTF8(<char *>event.data.scalar.value,
event.data.scalar.length, 'strict') event.data.scalar.length, 'strict')
plain_implicit = False plain_implicit = False
@ -617,10 +586,10 @@ cdef class CParser:
elif event.type == YAML_SEQUENCE_START_EVENT: elif event.type == YAML_SEQUENCE_START_EVENT:
anchor = None anchor = None
if event.data.sequence_start.anchor != NULL: 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 tag = None
if event.data.sequence_start.tag != NULL: 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 implicit = False
if event.data.sequence_start.implicit == 1: if event.data.sequence_start.implicit == 1:
implicit = True implicit = True
@ -634,7 +603,7 @@ cdef class CParser:
elif event.type == YAML_MAPPING_START_EVENT: elif event.type == YAML_MAPPING_START_EVENT:
anchor = None anchor = None
if event.data.mapping_start.anchor != NULL: 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 tag = None
if event.data.mapping_start.tag != NULL: if event.data.mapping_start.tag != NULL:
tag = PyUnicode_FromYamlString(event.data.mapping_start.tag) tag = PyUnicode_FromYamlString(event.data.mapping_start.tag)
@ -653,10 +622,7 @@ cdef class CParser:
elif event.type == YAML_MAPPING_END_EVENT: elif event.type == YAML_MAPPING_END_EVENT:
return MappingEndEvent(start_mark, end_mark) return MappingEndEvent(start_mark, end_mark)
else: else:
if PY_MAJOR_VERSION < 3: raise ValueError(u"unknown event type")
raise ValueError("unknown event type")
else:
raise ValueError(u"unknown event type")
def get_event(self): def get_event(self):
if self.current_event is not None: 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.line,
self.parsed_event.start_mark.column, self.parsed_event.start_mark.column,
None, None) None, None)
if PY_MAJOR_VERSION < 3: raise ComposerError(u"expected a single document in the stream",
raise ComposerError("expected a single document in the stream", document.start_mark, u"but found another document", mark)
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)
return document return document
cdef object _compose_document(self): cdef object _compose_document(self):
@ -738,10 +700,7 @@ cdef class CParser:
self.parsed_event.start_mark.line, self.parsed_event.start_mark.line,
self.parsed_event.start_mark.column, self.parsed_event.start_mark.column,
None, None) None, None)
if PY_MAJOR_VERSION < 3: raise ComposerError(None, None, u"found undefined alias", mark)
raise ComposerError(None, None, "found undefined alias", mark)
else:
raise ComposerError(None, None, u"found undefined alias", mark)
yaml_event_delete(&self.parsed_event) yaml_event_delete(&self.parsed_event)
return self.anchors[anchor] return self.anchors[anchor]
anchor = None anchor = None
@ -761,12 +720,8 @@ cdef class CParser:
self.parsed_event.start_mark.line, self.parsed_event.start_mark.line,
self.parsed_event.start_mark.column, self.parsed_event.start_mark.column,
None, None) None, None)
if PY_MAJOR_VERSION < 3: raise ComposerError(u"found duplicate anchor; first occurrence",
raise ComposerError("found duplicate anchor; first occurrence", self.anchors[anchor].start_mark, u"second occurrence", mark)
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)
self.descend_resolver(parent, index) self.descend_resolver(parent, index)
if self.parsed_event.type == YAML_SCALAR_EVENT: if self.parsed_event.type == YAML_SCALAR_EVENT:
node = self._compose_scalar_node(anchor) 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: if PyUnicode_CheckExact(value) != 0:
value = PyUnicode_AsUTF8String(value) value = PyUnicode_AsUTF8String(value)
parser.unicode_source = 1 parser.unicode_source = 1
if PyString_CheckExact(value) == 0: if PyBytes_CheckExact(value) == 0:
if PY_MAJOR_VERSION < 3: raise TypeError(u"a string value is expected")
raise TypeError("a string value is expected")
else:
raise TypeError(u"a string value is expected")
parser.stream_cache = value parser.stream_cache = value
parser.stream_cache_pos = 0 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: if (parser.stream_cache_len - parser.stream_cache_pos) < <int>size:
size = parser.stream_cache_len - parser.stream_cache_pos size = parser.stream_cache_len - parser.stream_cache_pos
if size > 0: if size > 0:
memcpy(buffer, PyString_AS_STRING(parser.stream_cache) memcpy(buffer, PyBytes_AS_STRING(parser.stream_cache)
+ parser.stream_cache_pos, size) + parser.stream_cache_pos, size)
read[0] = size read[0] = size
parser.stream_cache_pos += size parser.stream_cache_pos += size
@ -957,12 +909,8 @@ cdef class CEmitter:
raise MemoryError raise MemoryError
self.stream = stream self.stream = stream
self.dump_unicode = 0 self.dump_unicode = 0
if PY_MAJOR_VERSION < 3: if hasattr(stream, u'encoding'):
if getattr3(stream, 'encoding', None): self.dump_unicode = 1
self.dump_unicode = 1
else:
if hasattr(stream, u'encoding'):
self.dump_unicode = 1
self.use_encoding = encoding self.use_encoding = encoding
yaml_emitter_set_output(&self.emitter, output_handler, <void *>self) yaml_emitter_set_output(&self.emitter, output_handler, <void *>self)
if canonical: if canonical:
@ -1003,15 +951,9 @@ cdef class CEmitter:
if self.emitter.error == YAML_MEMORY_ERROR: if self.emitter.error == YAML_MEMORY_ERROR:
return MemoryError return MemoryError
elif self.emitter.error == YAML_EMITTER_ERROR: elif self.emitter.error == YAML_EMITTER_ERROR:
if PY_MAJOR_VERSION < 3: problem = PyUnicode_FromString(self.emitter.problem)
problem = self.emitter.problem
else:
problem = PyUnicode_FromString(self.emitter.problem)
return EmitterError(problem) return EmitterError(problem)
if PY_MAJOR_VERSION < 3: raise ValueError(u"no emitter error")
raise ValueError("no emitter error")
else:
raise ValueError(u"no emitter error")
cdef int _object_to_event(self, object event_object, yaml_event_t *event) except 0: cdef int _object_to_event(self, object event_object, yaml_event_t *event) except 0:
cdef yaml_encoding_t encoding cdef yaml_encoding_t encoding
@ -1054,10 +996,7 @@ cdef class CEmitter:
tag_directives_end = NULL tag_directives_end = NULL
if event_object.tags: if event_object.tags:
if len(event_object.tags) > 128: if len(event_object.tags) > 128:
if PY_MAJOR_VERSION < 3: raise ValueError(u"too many tags")
raise ValueError("too many tags")
else:
raise ValueError(u"too many tags")
tag_directives_start = tag_directives_value tag_directives_start = tag_directives_value
tag_directives_end = tag_directives_value tag_directives_end = tag_directives_value
cache = [] cache = []
@ -1066,21 +1005,15 @@ cdef class CEmitter:
if PyUnicode_CheckExact(handle): if PyUnicode_CheckExact(handle):
handle = PyUnicode_AsUTF8String(handle) handle = PyUnicode_AsUTF8String(handle)
cache.append(handle) cache.append(handle)
if not PyString_CheckExact(handle): if not PyBytes_CheckExact(handle):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag handle must be a string")
raise TypeError("tag handle must be a string") tag_directives_end.handle = PyBytes_AS_Yaml_STRING(handle)
else:
raise TypeError(u"tag handle must be a string")
tag_directives_end.handle = PyString_AS_Yaml_STRING(handle)
if PyUnicode_CheckExact(prefix): if PyUnicode_CheckExact(prefix):
prefix = PyUnicode_AsUTF8String(prefix) prefix = PyUnicode_AsUTF8String(prefix)
cache.append(prefix) cache.append(prefix)
if not PyString_CheckExact(prefix): if not PyBytes_CheckExact(prefix):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag prefix must be a string")
raise TypeError("tag prefix must be a string") tag_directives_end.prefix = PyBytes_AS_Yaml_STRING(prefix)
else:
raise TypeError(u"tag prefix must be a string")
tag_directives_end.prefix = PyString_AS_Yaml_STRING(prefix)
tag_directives_end = tag_directives_end+1 tag_directives_end = tag_directives_end+1
implicit = 1 implicit = 1
if event_object.explicit: if event_object.explicit:
@ -1098,12 +1031,9 @@ cdef class CEmitter:
anchor_object = event_object.anchor anchor_object = event_object.anchor
if PyUnicode_CheckExact(anchor_object): if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object) anchor_object = PyUnicode_AsUTF8String(anchor_object)
if not PyString_CheckExact(anchor_object): if not PyBytes_CheckExact(anchor_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"anchor must be a string")
raise TypeError("anchor must be a string") anchor = PyBytes_AS_Yaml_STRING(anchor_object)
else:
raise TypeError(u"anchor must be a string")
anchor = PyString_AS_Yaml_STRING(anchor_object)
if yaml_alias_event_initialize(event, anchor) == 0: if yaml_alias_event_initialize(event, anchor) == 0:
raise MemoryError raise MemoryError
elif event_class is ScalarEvent: elif event_class is ScalarEvent:
@ -1112,33 +1042,24 @@ cdef class CEmitter:
if anchor_object is not None: if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object): if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object) anchor_object = PyUnicode_AsUTF8String(anchor_object)
if not PyString_CheckExact(anchor_object): if not PyBytes_CheckExact(anchor_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"anchor must be a string")
raise TypeError("anchor must be a string") anchor = PyBytes_AS_Yaml_STRING(anchor_object)
else:
raise TypeError(u"anchor must be a string")
anchor = PyString_AS_Yaml_STRING(anchor_object)
tag = NULL tag = NULL
tag_object = event_object.tag tag_object = event_object.tag
if tag_object is not None: if tag_object is not None:
if PyUnicode_CheckExact(tag_object): if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object) tag_object = PyUnicode_AsUTF8String(tag_object)
if not PyString_CheckExact(tag_object): if not PyBytes_CheckExact(tag_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag must be a string")
raise TypeError("tag must be a string") tag = PyBytes_AS_Yaml_STRING(tag_object)
else:
raise TypeError(u"tag must be a string")
tag = PyString_AS_Yaml_STRING(tag_object)
value_object = event_object.value value_object = event_object.value
if PyUnicode_CheckExact(value_object): if PyUnicode_CheckExact(value_object):
value_object = PyUnicode_AsUTF8String(value_object) value_object = PyUnicode_AsUTF8String(value_object)
if not PyString_CheckExact(value_object): if not PyBytes_CheckExact(value_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"value must be a string")
raise TypeError("value must be a string") value = PyBytes_AS_Yaml_STRING(value_object)
else: length = PyBytes_GET_SIZE(value_object)
raise TypeError(u"value must be a string")
value = PyString_AS_Yaml_STRING(value_object)
length = PyString_GET_SIZE(value_object)
plain_implicit = 0 plain_implicit = 0
quoted_implicit = 0 quoted_implicit = 0
if event_object.implicit is not None: if event_object.implicit is not None:
@ -1163,23 +1084,17 @@ cdef class CEmitter:
if anchor_object is not None: if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object): if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object) anchor_object = PyUnicode_AsUTF8String(anchor_object)
if not PyString_CheckExact(anchor_object): if not PyBytes_CheckExact(anchor_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"anchor must be a string")
raise TypeError("anchor must be a string") anchor = PyBytes_AS_Yaml_STRING(anchor_object)
else:
raise TypeError(u"anchor must be a string")
anchor = PyString_AS_Yaml_STRING(anchor_object)
tag = NULL tag = NULL
tag_object = event_object.tag tag_object = event_object.tag
if tag_object is not None: if tag_object is not None:
if PyUnicode_CheckExact(tag_object): if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object) tag_object = PyUnicode_AsUTF8String(tag_object)
if not PyString_CheckExact(tag_object): if not PyBytes_CheckExact(tag_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag must be a string")
raise TypeError("tag must be a string") tag = PyBytes_AS_Yaml_STRING(tag_object)
else:
raise TypeError(u"tag must be a string")
tag = PyString_AS_Yaml_STRING(tag_object)
implicit = 0 implicit = 0
if event_object.implicit: if event_object.implicit:
implicit = 1 implicit = 1
@ -1195,23 +1110,17 @@ cdef class CEmitter:
if anchor_object is not None: if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object): if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object) anchor_object = PyUnicode_AsUTF8String(anchor_object)
if not PyString_CheckExact(anchor_object): if not PyBytes_CheckExact(anchor_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"anchor must be a string")
raise TypeError("anchor must be a string") anchor = PyBytes_AS_Yaml_STRING(anchor_object)
else:
raise TypeError(u"anchor must be a string")
anchor = PyString_AS_Yaml_STRING(anchor_object)
tag = NULL tag = NULL
tag_object = event_object.tag tag_object = event_object.tag
if tag_object is not None: if tag_object is not None:
if PyUnicode_CheckExact(tag_object): if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object) tag_object = PyUnicode_AsUTF8String(tag_object)
if not PyString_CheckExact(tag_object): if not PyBytes_CheckExact(tag_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag must be a string")
raise TypeError("tag must be a string") tag = PyBytes_AS_Yaml_STRING(tag_object)
else:
raise TypeError(u"tag must be a string")
tag = PyString_AS_Yaml_STRING(tag_object)
implicit = 0 implicit = 0
if event_object.implicit: if event_object.implicit:
implicit = 1 implicit = 1
@ -1226,10 +1135,7 @@ cdef class CEmitter:
elif event_class is MappingEndEvent: elif event_class is MappingEndEvent:
yaml_mapping_end_event_initialize(event) yaml_mapping_end_event_initialize(event)
else: else:
if PY_MAJOR_VERSION < 3: raise TypeError(u"invalid event %s" % event_object)
raise TypeError("invalid event %s" % event_object)
else:
raise TypeError(u"invalid event %s" % event_object)
return 1 return 1
def emit(self, event_object): def emit(self, event_object):
@ -1259,23 +1165,14 @@ cdef class CEmitter:
raise error raise error
self.closed = 0 self.closed = 0
elif self.closed == 1: elif self.closed == 1:
if PY_MAJOR_VERSION < 3: raise SerializerError(u"serializer is closed")
raise SerializerError("serializer is closed")
else:
raise SerializerError(u"serializer is closed")
else: else:
if PY_MAJOR_VERSION < 3: raise SerializerError(u"serializer is already opened")
raise SerializerError("serializer is already opened")
else:
raise SerializerError(u"serializer is already opened")
def close(self): def close(self):
cdef yaml_event_t event cdef yaml_event_t event
if self.closed == -1: if self.closed == -1:
if PY_MAJOR_VERSION < 3: raise SerializerError(u"serializer is not opened")
raise SerializerError("serializer is not opened")
else:
raise SerializerError(u"serializer is not opened")
elif self.closed == 0: elif self.closed == 0:
yaml_stream_end_event_initialize(&event) yaml_stream_end_event_initialize(&event)
if yaml_emitter_emit(&self.emitter, &event) == 0: 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_start
cdef yaml_tag_directive_t *tag_directives_end cdef yaml_tag_directive_t *tag_directives_end
if self.closed == -1: if self.closed == -1:
if PY_MAJOR_VERSION < 3: raise SerializerError(u"serializer is not opened")
raise SerializerError("serializer is not opened")
else:
raise SerializerError(u"serializer is not opened")
elif self.closed == 1: elif self.closed == 1:
if PY_MAJOR_VERSION < 3: raise SerializerError(u"serializer is closed")
raise SerializerError("serializer is closed")
else:
raise SerializerError(u"serializer is closed")
cache = [] cache = []
version_directive = NULL version_directive = NULL
if self.use_version: if self.use_version:
@ -1310,10 +1201,7 @@ cdef class CEmitter:
tag_directives_end = NULL tag_directives_end = NULL
if self.use_tags: if self.use_tags:
if len(self.use_tags) > 128: if len(self.use_tags) > 128:
if PY_MAJOR_VERSION < 3: raise ValueError(u"too many tags")
raise ValueError("too many tags")
else:
raise ValueError(u"too many tags")
tag_directives_start = tag_directives_value tag_directives_start = tag_directives_value
tag_directives_end = tag_directives_value tag_directives_end = tag_directives_value
for handle in self.use_tags: for handle in self.use_tags:
@ -1321,21 +1209,15 @@ cdef class CEmitter:
if PyUnicode_CheckExact(handle): if PyUnicode_CheckExact(handle):
handle = PyUnicode_AsUTF8String(handle) handle = PyUnicode_AsUTF8String(handle)
cache.append(handle) cache.append(handle)
if not PyString_CheckExact(handle): if not PyBytes_CheckExact(handle):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag handle must be a string")
raise TypeError("tag handle must be a string") tag_directives_end.handle = PyBytes_AS_Yaml_STRING(handle)
else:
raise TypeError(u"tag handle must be a string")
tag_directives_end.handle = PyString_AS_Yaml_STRING(handle)
if PyUnicode_CheckExact(prefix): if PyUnicode_CheckExact(prefix):
prefix = PyUnicode_AsUTF8String(prefix) prefix = PyUnicode_AsUTF8String(prefix)
cache.append(prefix) cache.append(prefix)
if not PyString_CheckExact(prefix): if not PyBytes_CheckExact(prefix):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag prefix must be a string")
raise TypeError("tag prefix must be a string") tag_directives_end.prefix = PyBytes_AS_Yaml_STRING(prefix)
else:
raise TypeError(u"tag prefix must be a string")
tag_directives_end.prefix = PyString_AS_Yaml_STRING(prefix)
tag_directives_end = tag_directives_end+1 tag_directives_end = tag_directives_end+1
if yaml_document_start_event_initialize(&event, version_directive, if yaml_document_start_event_initialize(&event, version_directive,
tag_directives_start, tag_directives_end, tag_directives_start, tag_directives_end,
@ -1389,12 +1271,9 @@ cdef class CEmitter:
if anchor_object is not None: if anchor_object is not None:
if PyUnicode_CheckExact(anchor_object): if PyUnicode_CheckExact(anchor_object):
anchor_object = PyUnicode_AsUTF8String(anchor_object) anchor_object = PyUnicode_AsUTF8String(anchor_object)
if not PyString_CheckExact(anchor_object): if not PyBytes_CheckExact(anchor_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"anchor must be a string")
raise TypeError("anchor must be a string") anchor = PyBytes_AS_Yaml_STRING(anchor_object)
else:
raise TypeError(u"anchor must be a string")
anchor = PyString_AS_Yaml_STRING(anchor_object)
if node in self.serialized_nodes: if node in self.serialized_nodes:
if yaml_alias_event_initialize(&event, anchor) == 0: if yaml_alias_event_initialize(&event, anchor) == 0:
raise MemoryError raise MemoryError
@ -1417,22 +1296,16 @@ cdef class CEmitter:
if tag_object is not None: if tag_object is not None:
if PyUnicode_CheckExact(tag_object): if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object) tag_object = PyUnicode_AsUTF8String(tag_object)
if not PyString_CheckExact(tag_object): if not PyBytes_CheckExact(tag_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag must be a string")
raise TypeError("tag must be a string") tag = PyBytes_AS_Yaml_STRING(tag_object)
else:
raise TypeError(u"tag must be a string")
tag = PyString_AS_Yaml_STRING(tag_object)
value_object = node.value value_object = node.value
if PyUnicode_CheckExact(value_object): if PyUnicode_CheckExact(value_object):
value_object = PyUnicode_AsUTF8String(value_object) value_object = PyUnicode_AsUTF8String(value_object)
if not PyString_CheckExact(value_object): if not PyBytes_CheckExact(value_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"value must be a string")
raise TypeError("value must be a string") value = PyBytes_AS_Yaml_STRING(value_object)
else: length = PyBytes_GET_SIZE(value_object)
raise TypeError(u"value must be a string")
value = PyString_AS_Yaml_STRING(value_object)
length = PyString_GET_SIZE(value_object)
style_object = node.style style_object = node.style
scalar_style = YAML_PLAIN_SCALAR_STYLE scalar_style = YAML_PLAIN_SCALAR_STYLE
if style_object == "'" or style_object == u"'": if style_object == "'" or style_object == u"'":
@ -1458,12 +1331,9 @@ cdef class CEmitter:
if tag_object is not None: if tag_object is not None:
if PyUnicode_CheckExact(tag_object): if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object) tag_object = PyUnicode_AsUTF8String(tag_object)
if not PyString_CheckExact(tag_object): if not PyBytes_CheckExact(tag_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag must be a string")
raise TypeError("tag must be a string") tag = PyBytes_AS_Yaml_STRING(tag_object)
else:
raise TypeError(u"tag must be a string")
tag = PyString_AS_Yaml_STRING(tag_object)
sequence_style = YAML_BLOCK_SEQUENCE_STYLE sequence_style = YAML_BLOCK_SEQUENCE_STYLE
if node.flow_style: if node.flow_style:
sequence_style = YAML_FLOW_SEQUENCE_STYLE sequence_style = YAML_FLOW_SEQUENCE_STYLE
@ -1490,12 +1360,9 @@ cdef class CEmitter:
if tag_object is not None: if tag_object is not None:
if PyUnicode_CheckExact(tag_object): if PyUnicode_CheckExact(tag_object):
tag_object = PyUnicode_AsUTF8String(tag_object) tag_object = PyUnicode_AsUTF8String(tag_object)
if not PyString_CheckExact(tag_object): if not PyBytes_CheckExact(tag_object):
if PY_MAJOR_VERSION < 3: raise TypeError(u"tag must be a string")
raise TypeError("tag must be a string") tag = PyBytes_AS_Yaml_STRING(tag_object)
else:
raise TypeError(u"tag must be a string")
tag = PyString_AS_Yaml_STRING(tag_object)
mapping_style = YAML_BLOCK_MAPPING_STYLE mapping_style = YAML_BLOCK_MAPPING_STYLE
if node.flow_style: if node.flow_style:
mapping_style = YAML_FLOW_MAPPING_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 buffer = <char *>bufferu
emitter = <CEmitter>data emitter = <CEmitter>data
if emitter.dump_unicode == 0: if emitter.dump_unicode == 0:
value = PyString_FromStringAndSize(buffer, size) value = PyBytes_FromStringAndSize(buffer, size)
else: else:
value = PyUnicode_DecodeUTF8(buffer, size, 'strict') value = PyUnicode_DecodeUTF8(buffer, size, 'strict')
emitter.stream.write(value) emitter.stream.write(value)