Ensure that pure python dumper is used in PY3 for yamlex serializer

This commit is contained in:
Erik Johnson 2018-12-04 09:34:52 -06:00
parent d9c5f57437
commit 31f62622c2
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -128,9 +128,18 @@ log = logging.getLogger(__name__)
available = True
# prefer C bindings over python when available
# CSafeDumper causes test failures under python3
BaseLoader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
BaseDumper = yaml.SafeDumper if six.PY3 else getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
if six.PY3:
# CSafeDumper causes repr errors in python3, so use the pure Python one
try:
# Depending on how PyYAML was built, yaml.SafeDumper may actually be
# yaml.cyaml.CSafeDumper (i.e. the C dumper instead of pure Python).
BaseDumper = yaml.dumper.SafeDumper
except AttributeError:
# Here just in case, but yaml.dumper.SafeDumper should always exist
BaseDumper = yaml.SafeDumper
else:
BaseDumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
ERROR_MAP = {
("found character '\\t' "
@ -389,6 +398,7 @@ class Dumper(BaseDumper): # pylint: disable=W0232
def represent_odict(self, data):
return self.represent_mapping('tag:yaml.org,2002:map', list(data.items()))
Dumper.add_multi_representer(type(None), Dumper.represent_none)
if six.PY2:
Dumper.add_multi_representer(six.binary_type, Dumper.represent_str)