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