1 2import yaml 3import codecs, io, tempfile, os, os.path 4 5def 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.full_load(data) 9 assert output == value, (output, value) 10 output = yaml.full_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.full_load(input) 19 assert output == value, (output, value) 20 output = yaml.full_load(io.BytesIO(input)) 21 assert output == value, (output, value) 22 23test_unicode_input.unittest = ['.unicode'] 24 25def test_unicode_input_errors(unicode_filename, verbose=False): 26 data = open(unicode_filename, 'rb').read().decode('utf-8') 27 for input in [data.encode('utf-16-be'), 28 data.encode('utf-16-le'), 29 codecs.BOM_UTF8+data.encode('utf-16-be'), 30 codecs.BOM_UTF8+data.encode('utf-16-le')]: 31 32 try: 33 yaml.full_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.full_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 47test_unicode_input_errors.unittest = ['.unicode'] 48 49def 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 if encoding is not None: 60 assert isinstance(data3, bytes) 61 data3 = data3.decode(encoding) 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) 78 79 assert isinstance(data1, str), (type(data1), encoding) 80 assert isinstance(data2, str), (type(data2), encoding) 81 82test_unicode_output.unittest = ['.unicode'] 83 84def test_file_output(unicode_filename, verbose=False): 85 data = open(unicode_filename, 'rb').read().decode('utf-8') 86 handle, filename = tempfile.mkstemp() 87 os.close(handle) 88 try: 89 stream = io.StringIO() 90 yaml.dump(data, stream, allow_unicode=True) 91 data1 = stream.getvalue() 92 stream = io.BytesIO() 93 yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True) 94 data2 = stream.getvalue().decode('utf-16-le')[1:] 95 stream = open(filename, 'w', encoding='utf-16-le') 96 yaml.dump(data, stream, allow_unicode=True) 97 stream.close() 98 data3 = open(filename, 'r', encoding='utf-16-le').read() 99 stream = open(filename, 'wb') 100 yaml.dump(data, stream, encoding='utf-8', allow_unicode=True) 101 stream.close() 102 data4 = open(filename, 'r', encoding='utf-8').read() 103 assert data1 == data2, (data1, data2) 104 assert data1 == data3, (data1, data3) 105 assert data1 == data4, (data1, data4) 106 finally: 107 if os.path.exists(filename): 108 os.unlink(filename) 109 110test_file_output.unittest = ['.unicode'] 111 112def test_unicode_transfer(unicode_filename, verbose=False): 113 data = open(unicode_filename, 'rb').read().decode('utf-8') 114 for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: 115 input = data 116 if encoding is not None: 117 input = ('\ufeff'+input).encode(encoding) 118 output1 = yaml.emit(yaml.parse(input), allow_unicode=True) 119 if encoding is None: 120 stream = io.StringIO() 121 else: 122 stream = io.BytesIO() 123 yaml.emit(yaml.parse(input), stream, allow_unicode=True) 124 output2 = stream.getvalue() 125 assert isinstance(output1, str), (type(output1), encoding) 126 if encoding is None: 127 assert isinstance(output2, str), (type(output1), encoding) 128 else: 129 assert isinstance(output2, bytes), (type(output1), encoding) 130 output2.decode(encoding) 131 132test_unicode_transfer.unittest = ['.unicode'] 133 134if __name__ == '__main__': 135 import test_appliance 136 test_appliance.run(globals()) 137