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