| [333] | 1 | |
|---|
| 2 | import yaml |
|---|
| [366] | 3 | import codecs, StringIO, tempfile, os, os.path |
|---|
| [333] | 4 | |
|---|
| 5 | def _unicode_open(file, encoding, errors='strict'): |
|---|
| 6 | info = codecs.lookup(encoding) |
|---|
| [335] | 7 | if isinstance(info, tuple): |
|---|
| 8 | reader = info[2] |
|---|
| 9 | writer = info[3] |
|---|
| 10 | else: |
|---|
| 11 | reader = info.streamreader |
|---|
| 12 | writer = info.streamwriter |
|---|
| 13 | srw = codecs.StreamReaderWriter(file, reader, writer, errors) |
|---|
| [333] | 14 | srw.encoding = encoding |
|---|
| 15 | return srw |
|---|
| 16 | |
|---|
| 17 | def test_unicode_input(unicode_filename, verbose=False): |
|---|
| 18 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
|---|
| 19 | value = ' '.join(data.split()) |
|---|
| 20 | output = yaml.load(_unicode_open(StringIO.StringIO(data.encode('utf-8')), 'utf-8')) |
|---|
| 21 | assert output == value, (output, value) |
|---|
| 22 | for input in [data, data.encode('utf-8'), |
|---|
| 23 | codecs.BOM_UTF8+data.encode('utf-8'), |
|---|
| 24 | codecs.BOM_UTF16_BE+data.encode('utf-16-be'), |
|---|
| 25 | codecs.BOM_UTF16_LE+data.encode('utf-16-le')]: |
|---|
| 26 | if verbose: |
|---|
| 27 | print "INPUT:", repr(input[:10]), "..." |
|---|
| 28 | output = yaml.load(input) |
|---|
| 29 | assert output == value, (output, value) |
|---|
| 30 | output = yaml.load(StringIO.StringIO(input)) |
|---|
| 31 | assert output == value, (output, value) |
|---|
| 32 | |
|---|
| 33 | test_unicode_input.unittest = ['.unicode'] |
|---|
| 34 | |
|---|
| 35 | def test_unicode_input_errors(unicode_filename, verbose=False): |
|---|
| 36 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
|---|
| 37 | for input in [data.encode('latin1', 'ignore'), |
|---|
| 38 | data.encode('utf-16-be'), data.encode('utf-16-le'), |
|---|
| 39 | codecs.BOM_UTF8+data.encode('utf-16-be'), |
|---|
| 40 | codecs.BOM_UTF16_BE+data.encode('utf-16-le'), |
|---|
| 41 | codecs.BOM_UTF16_LE+data.encode('utf-8')+'!']: |
|---|
| 42 | try: |
|---|
| 43 | yaml.load(input) |
|---|
| 44 | except yaml.YAMLError, exc: |
|---|
| 45 | if verbose: |
|---|
| 46 | print exc |
|---|
| 47 | else: |
|---|
| 48 | raise AssertionError("expected an exception") |
|---|
| 49 | try: |
|---|
| 50 | yaml.load(StringIO.StringIO(input)) |
|---|
| 51 | except yaml.YAMLError, exc: |
|---|
| 52 | if verbose: |
|---|
| 53 | print exc |
|---|
| 54 | else: |
|---|
| 55 | raise AssertionError("expected an exception") |
|---|
| 56 | |
|---|
| 57 | test_unicode_input_errors.unittest = ['.unicode'] |
|---|
| 58 | |
|---|
| 59 | def test_unicode_output(unicode_filename, verbose=False): |
|---|
| 60 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
|---|
| 61 | value = ' '.join(data.split()) |
|---|
| [334] | 62 | for allow_unicode in [False, True]: |
|---|
| 63 | data1 = yaml.dump(value, allow_unicode=allow_unicode) |
|---|
| 64 | for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: |
|---|
| [333] | 65 | stream = StringIO.StringIO() |
|---|
| 66 | yaml.dump(value, _unicode_open(stream, 'utf-8'), encoding=encoding, allow_unicode=allow_unicode) |
|---|
| 67 | data2 = stream.getvalue() |
|---|
| 68 | data3 = yaml.dump(value, encoding=encoding, allow_unicode=allow_unicode) |
|---|
| 69 | stream = StringIO.StringIO() |
|---|
| 70 | yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) |
|---|
| 71 | data4 = stream.getvalue() |
|---|
| 72 | for copy in [data1, data2, data3, data4]: |
|---|
| 73 | if allow_unicode: |
|---|
| 74 | try: |
|---|
| 75 | copy[4:].encode('ascii') |
|---|
| 76 | except (UnicodeDecodeError, UnicodeEncodeError), exc: |
|---|
| 77 | if verbose: |
|---|
| 78 | print exc |
|---|
| 79 | else: |
|---|
| 80 | raise AssertionError("expected an exception") |
|---|
| 81 | else: |
|---|
| 82 | copy[4:].encode('ascii') |
|---|
| 83 | assert isinstance(data1, str), (type(data1), encoding) |
|---|
| 84 | data1.decode('utf-8') |
|---|
| 85 | assert isinstance(data2, str), (type(data2), encoding) |
|---|
| 86 | data2.decode('utf-8') |
|---|
| 87 | if encoding is None: |
|---|
| 88 | assert isinstance(data3, unicode), (type(data3), encoding) |
|---|
| 89 | assert isinstance(data4, unicode), (type(data4), encoding) |
|---|
| 90 | else: |
|---|
| 91 | assert isinstance(data3, str), (type(data3), encoding) |
|---|
| 92 | data3.decode(encoding) |
|---|
| 93 | assert isinstance(data4, str), (type(data4), encoding) |
|---|
| 94 | data4.decode(encoding) |
|---|
| 95 | |
|---|
| 96 | test_unicode_output.unittest = ['.unicode'] |
|---|
| 97 | |
|---|
| [366] | 98 | def test_file_output(unicode_filename, verbose=False): |
|---|
| 99 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
|---|
| 100 | handle, filename = tempfile.mkstemp() |
|---|
| [367] | 101 | os.close(handle) |
|---|
| [366] | 102 | try: |
|---|
| 103 | stream = StringIO.StringIO() |
|---|
| 104 | yaml.dump(data, stream, allow_unicode=True) |
|---|
| 105 | data1 = stream.getvalue() |
|---|
| 106 | stream = open(filename, 'wb') |
|---|
| 107 | yaml.dump(data, stream, allow_unicode=True) |
|---|
| 108 | stream.close() |
|---|
| 109 | data2 = open(filename, 'rb').read() |
|---|
| 110 | stream = open(filename, 'wb') |
|---|
| 111 | yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True) |
|---|
| 112 | stream.close() |
|---|
| 113 | data3 = open(filename, 'rb').read().decode('utf-16-le')[1:].encode('utf-8') |
|---|
| 114 | stream = _unicode_open(open(filename, 'wb'), 'utf-8') |
|---|
| 115 | yaml.dump(data, stream, allow_unicode=True) |
|---|
| 116 | stream.close() |
|---|
| 117 | data4 = open(filename, 'rb').read() |
|---|
| 118 | assert data1 == data2, (data1, data2) |
|---|
| 119 | assert data1 == data3, (data1, data3) |
|---|
| 120 | assert data1 == data4, (data1, data4) |
|---|
| 121 | finally: |
|---|
| 122 | if os.path.exists(filename): |
|---|
| 123 | os.unlink(filename) |
|---|
| 124 | |
|---|
| 125 | test_file_output.unittest = ['.unicode'] |
|---|
| 126 | |
|---|
| [333] | 127 | def test_unicode_transfer(unicode_filename, verbose=False): |
|---|
| 128 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
|---|
| 129 | for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: |
|---|
| 130 | input = data |
|---|
| 131 | if encoding is not None: |
|---|
| 132 | input = (u'\ufeff'+input).encode(encoding) |
|---|
| 133 | output1 = yaml.emit(yaml.parse(input), allow_unicode=True) |
|---|
| 134 | stream = StringIO.StringIO() |
|---|
| 135 | yaml.emit(yaml.parse(input), _unicode_open(stream, 'utf-8'), |
|---|
| 136 | allow_unicode=True) |
|---|
| 137 | output2 = stream.getvalue() |
|---|
| 138 | if encoding is None: |
|---|
| 139 | assert isinstance(output1, unicode), (type(output1), encoding) |
|---|
| 140 | else: |
|---|
| 141 | assert isinstance(output1, str), (type(output1), encoding) |
|---|
| 142 | output1.decode(encoding) |
|---|
| 143 | assert isinstance(output2, str), (type(output2), encoding) |
|---|
| 144 | output2.decode('utf-8') |
|---|
| 145 | |
|---|
| 146 | test_unicode_transfer.unittest = ['.unicode'] |
|---|
| 147 | |
|---|
| 148 | if __name__ == '__main__': |
|---|
| 149 | import test_appliance |
|---|
| 150 | test_appliance.run(globals()) |
|---|
| 151 | |
|---|