diff options
-rw-r--r-- | ext/_yaml.pyx | 2 | ||||
-rw-r--r-- | tests/lib/test_input_output.py | 31 | ||||
-rw-r--r-- | tests/lib3/test_input_output.py | 30 |
3 files changed, 60 insertions, 3 deletions
diff --git a/ext/_yaml.pyx b/ext/_yaml.pyx index ac56b5f..df19492 100644 --- a/ext/_yaml.pyx +++ b/ext/_yaml.pyx @@ -955,7 +955,7 @@ cdef class CEmitter: self.stream = stream self.dump_unicode = 0 if PY_MAJOR_VERSION < 3: - if hasattr(stream, 'encoding'): + if getattr3(stream, 'encoding', None): self.dump_unicode = 1 else: if hasattr(stream, u'encoding'): diff --git a/tests/lib/test_input_output.py b/tests/lib/test_input_output.py index 0e9621e..9ccc8fc 100644 --- a/tests/lib/test_input_output.py +++ b/tests/lib/test_input_output.py @@ -1,6 +1,6 @@ import yaml -import codecs, StringIO +import codecs, StringIO, tempfile, os, os.path def _unicode_open(file, encoding, errors='strict'): info = codecs.lookup(encoding) @@ -95,6 +95,35 @@ def test_unicode_output(unicode_filename, verbose=False): test_unicode_output.unittest = ['.unicode'] +def test_file_output(unicode_filename, verbose=False): + data = open(unicode_filename, 'rb').read().decode('utf-8') + handle, filename = tempfile.mkstemp() + os.close(handle) + try: + stream = StringIO.StringIO() + yaml.dump(data, stream, allow_unicode=True) + data1 = stream.getvalue() + stream = open(filename, 'wb') + yaml.dump(data, stream, allow_unicode=True) + stream.close() + data2 = open(filename, 'rb').read() + stream = open(filename, 'wb') + yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True) + stream.close() + data3 = open(filename, 'rb').read().decode('utf-16-le')[1:].encode('utf-8') + stream = _unicode_open(open(filename, 'wb'), 'utf-8') + yaml.dump(data, stream, allow_unicode=True) + stream.close() + data4 = open(filename, 'rb').read() + assert data1 == data2, (data1, data2) + assert data1 == data3, (data1, data3) + assert data1 == data4, (data1, data4) + finally: + if os.path.exists(filename): + os.unlink(filename) + +test_file_output.unittest = ['.unicode'] + def test_unicode_transfer(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: diff --git a/tests/lib3/test_input_output.py b/tests/lib3/test_input_output.py index c32e25d..70a945a 100644 --- a/tests/lib3/test_input_output.py +++ b/tests/lib3/test_input_output.py @@ -1,6 +1,6 @@ import yaml -import codecs, io +import codecs, io, tempfile, os, os.path def test_unicode_input(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') @@ -94,6 +94,34 @@ def test_unicode_output(unicode_filename, verbose=False): test_unicode_output.unittest = ['.unicode'] +def test_file_output(unicode_filename, verbose=False): + data = open(unicode_filename, 'rb').read().decode('utf-8') + handle, filename = tempfile.mkstemp() + os.close(handle) + try: + stream = io.StringIO() + yaml.dump(data, stream, allow_unicode=True) + data1 = stream.getvalue() + stream = io.BytesIO() + yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True) + data2 = stream.getvalue().decode('utf-16-le')[1:] + stream = open(filename, 'w', encoding='utf-16-le') + yaml.dump(data, stream, allow_unicode=True) + stream.close() + data3 = open(filename, 'r', encoding='utf-16-le').read() + stream = open(filename, 'wb') + yaml.dump(data, stream, encoding='utf-8', allow_unicode=True) + stream.close() + data4 = open(filename, 'r', encoding='utf-8').read() + assert data1 == data2, (data1, data2) + assert data1 == data3, (data1, data3) + assert data1 == data4, (data1, data4) + finally: + if os.path.exists(filename): + os.unlink(filename) + +test_file_output.unittest = ['.unicode'] + def test_unicode_transfer(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: |