1 2import yaml.reader 3 4def _run_reader(data, verbose): 5 try: 6 stream = yaml.reader.Reader(data) 7 while stream.peek() != '\0': 8 stream.forward() 9 except yaml.reader.ReaderError as exc: 10 if verbose: 11 print(exc) 12 else: 13 raise AssertionError("expected an exception") 14 15def test_stream_error(error_filename, verbose=False): 16 with open(error_filename, 'rb') as file: 17 _run_reader(file, verbose) 18 with open(error_filename, 'rb') as file: 19 _run_reader(file.read(), verbose) 20 for encoding in ['utf-8', 'utf-16-le', 'utf-16-be']: 21 try: 22 with open(error_filename, 'rb') as file: 23 data = file.read().decode(encoding) 24 break 25 except UnicodeDecodeError: 26 pass 27 else: 28 return 29 _run_reader(data, verbose) 30 with open(error_filename, encoding=encoding) as file: 31 _run_reader(file, verbose) 32 33test_stream_error.unittest = ['.stream-error'] 34 35if __name__ == '__main__': 36 import test_appliance 37 test_appliance.run(globals()) 38 39