diff options
author | xi <xi@18f92427-320e-0410-9341-c67f048884a3> | 2009-08-31 08:47:05 +0000 |
---|---|---|
committer | xi <xi@18f92427-320e-0410-9341-c67f048884a3> | 2009-08-31 08:47:05 +0000 |
commit | 85760a2ea558ac3d4c23bef019e12600e553048a (patch) | |
tree | 4fe033c1af7e5a0d8dcff193ab24aca20023a874 | |
parent | b64c663aed57a578a28bd4575bf4d30630c598e8 (diff) | |
download | pyyaml-85760a2ea558ac3d4c23bef019e12600e553048a.tar.gz |
Fixed another encoding issue.
git-svn-id: http://svn.pyyaml.org/pyyaml/trunk@366 18f92427-320e-0410-9341-c67f048884a3
-rw-r--r-- | ext/_yaml.pyx | 2 | ||||
-rw-r--r-- | tests/lib/test_input_output.py | 30 | ||||
-rw-r--r-- | tests/lib3/test_input_output.py | 29 |
3 files changed, 58 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..131262a 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,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() + 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..62b20c7 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,33 @@ 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() + 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']: |