summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES3
-rw-r--r--README.rst3
-rw-r--r--__init__.py4
-rw-r--r--_doc/conf.py2
-rw-r--r--_test/roundtrip.py2
-rw-r--r--_test/test_api_change.py19
-rw-r--r--cyaml.py1
-rw-r--r--main.py17
8 files changed, 43 insertions, 8 deletions
diff --git a/CHANGES b/CHANGES
index d689dae..47c2a4a 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,6 @@
+[0, 15, 21]: 2017-07-25
+ - fix for writing unicode in new API, https://stackoverflow.com/a/45281922/1307905
+
[0, 15, 20]: 2017-07-23
- wheels for windows including C extensions
diff --git a/README.rst b/README.rst
index 652f826..a2afacf 100644
--- a/README.rst
+++ b/README.rst
@@ -35,6 +35,9 @@ ChangeLog
.. should insert NEXT: at the beginning of line for next key
+0.15.21 (2017-07-25):
+ - fix for writing unicode in new API, https://stackoverflow.com/a/45281922/1307905
+
0.15.20 (2017-07-23):
- wheels for windows including C extensions
diff --git a/__init__.py b/__init__.py
index 372a1be..ddd115d 100644
--- a/__init__.py
+++ b/__init__.py
@@ -7,8 +7,8 @@ if False: # MYPY
_package_data = dict(
full_package_name='ruamel.yaml',
- version_info=(0, 15, 20),
- __version__='0.15.20',
+ version_info=(0, 15, 21),
+ __version__='0.15.21',
author='Anthon van der Neut',
author_email='a.van.der.neut@ruamel.eu',
description='ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order', # NOQA
diff --git a/_doc/conf.py b/_doc/conf.py
index 1e1acb9..19a19a3 100644
--- a/_doc/conf.py
+++ b/_doc/conf.py
@@ -74,7 +74,7 @@ try:
sys.argv = ['ryd', 'convert', '--no-pdf', str(fn)]
rc = RYDCmd()
rc.parse_args()
- print(*sys.argv, '->', rc.run())
+ print(sys.argv, '->', rc.run())
sys.argv = oldargv
except Exception as e:
diff --git a/_test/roundtrip.py b/_test/roundtrip.py
index 53bc667..7ce51ae 100644
--- a/_test/roundtrip.py
+++ b/_test/roundtrip.py
@@ -119,7 +119,7 @@ class YAML(ruamel.yaml.YAML):
unordered_lines = lkw.pop('unordered_lines', False)
if expected and expected[0] == '\n':
expected = expected[1:]
- lkw['stream'] = st = StringIO() if self.encoding is None else BytesIO()
+ lkw['stream'] = st = StringIO()
ruamel.yaml.YAML.dump(self, data, **lkw)
res = st.getvalue()
print(res)
diff --git a/_test/test_api_change.py b/_test/test_api_change.py
index 7152eda..2b8ad40 100644
--- a/_test/test_api_change.py
+++ b/_test/test_api_change.py
@@ -125,3 +125,22 @@ class TestDuplSet:
? c
? a
"""))
+
+
+class TestDumpLoadUnicode:
+ # test triggered by SamH on stackoverflow (https://stackoverflow.com/q/45281596/1307905)
+ # and answer by randomir (https://stackoverflow.com/a/45281922/1307905)
+ def test_write_unicode(self, tmpdir):
+ yaml = YAML()
+ text_dict = {"text": u"HELLO_WORLD©"}
+ file_name = str(tmpdir) + '/tstFile.yaml'
+ yaml.dump(text_dict, open(file_name, 'w'))
+ assert open(file_name, 'rb').read().decode('utf-8') == u'text: HELLO_WORLD©\n'
+
+ def test_read_unicode(self, tmpdir):
+ yaml = YAML()
+ file_name = str(tmpdir) + '/tstFile.yaml'
+ with open(file_name, 'wb') as fp:
+ fp.write(u'text: HELLO_WORLD©\n'.encode('utf-8'))
+ text_dict = yaml.load(open(file_name, 'r'))
+ assert text_dict["text"] == u"HELLO_WORLD©"
diff --git a/cyaml.py b/cyaml.py
index 78438ff..7f504a5 100644
--- a/cyaml.py
+++ b/cyaml.py
@@ -92,6 +92,7 @@ class CSafeDumper(CEmitter, SafeRepresenter, Resolver): # type: ignore
explicit_start=explicit_start,
explicit_end=explicit_end,
version=version, tags=tags)
+ self._emitter = self._serializer = self._representer = self
SafeRepresenter.__init__(self, default_style=default_style,
default_flow_style=default_flow_style)
Resolver.__init__(self)
diff --git a/main.py b/main.py
index 58b1c81..35bcdcf 100644
--- a/main.py
+++ b/main.py
@@ -94,7 +94,8 @@ class YAML(object):
self.Composer = ruamel.yaml.composer.Composer
self.Constructor = ruamel.yaml.constructor.BaseConstructor
elif self.typ == 'unsafe':
- self.Emitter = ruamel.yaml.emitter.Emitter
+ self.Emitter = ruamel.yaml.emitter.Emitter if pure or CEmitter is None \
+ else CEmitter
self.Representer = ruamel.yaml.representer.Representer
self.Parser = ruamel.yaml.parser.Parser if pure or CParser is None else CParser
self.Composer = ruamel.yaml.composer.Composer
@@ -118,7 +119,7 @@ class YAML(object):
self.version = None
self.preserve_quotes = None
self.allow_duplicate_keys = False # duplicate keys in map, set
- self.encoding = None
+ self.encoding = 'utf-8'
self.explicit_start = None
self.explicit_end = None
self.tags = None
@@ -382,7 +383,10 @@ class YAML(object):
delattr(self, "_serializer")
delattr(self, "_emitter")
if transform:
- fstream.write(transform(stream.getvalue())) # type: ignore
+ val = stream.getvalue()
+ if self.encoding:
+ val = val.decode(self.encoding)
+ fstream.write(transform(val)) # type: ignore
return None
def get_serializer_representer_emitter(self, stream, tlca):
@@ -425,7 +429,12 @@ class YAML(object):
default_flow_style=default_flow_style)
rslvr.__init__(selfx)
self._stream = stream
- dumper = XDumper(stream)
+ dumper = XDumper(stream, default_style=self.default_style,
+ default_flow_style=self.default_flow_style,
+ allow_unicode=self.allow_unicode, line_break=self.line_break,
+ explicit_start=self.explicit_start,
+ explicit_end=self.explicit_end,
+ version=self.version, tags=self.tags)
self._emitter = self._serializer = dumper
return dumper, dumper, dumper