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