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