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