Skip to content

Commit f4a48fe

Browse files
committed
Add (freezing) test for unbuf. bytes invalid UTF-8
1 parent 267939d commit f4a48fe

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

tests/conftest.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class StringIOWithLargeCursorPositions(StringIO):
1111
def seek(self, offset, whence):
1212
input_offset, output_offset = 0, 0
1313
if offset != 0:
14-
input_offset = - 2**64
14+
input_offset = -(2**64)
1515
if whence == SEEK_CUR:
1616
output_offset = 2**64
1717
if whence == SEEK_END:
@@ -56,3 +56,26 @@ def make_unseekable_bytesio(s: str):
5656
return make_unseekable_bytesio
5757
else:
5858
assert False
59+
60+
61+
# TODO: Factor out commonalities with ^
62+
@pytest.fixture(params=["bytes", "bytes-unseekable"])
63+
def bytes_to_bytes_buf(request):
64+
"""
65+
Provides function that takes bytes and returns a bytes buffer.
66+
67+
Causes dependent test to be repeated for all relevant byte buffer types
68+
(seekable, unseekable).
69+
"""
70+
if request.param == "bytes":
71+
return lambda b: BytesIO(b)
72+
elif request.param == "bytes-unseekable":
73+
74+
def make_unseekable_bytesio(b: bytes):
75+
bio = BytesIO(b)
76+
bio.seekable = lambda: False
77+
return bio
78+
79+
return make_unseekable_bytesio
80+
else:
81+
assert False

tests/test_exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,12 @@ def test_invalid_character_code():
4242
),
4343
):
4444
list(load(StringIO(r'"\uz"')))
45+
46+
47+
def test_malformed_utf8(bytes_to_bytes_buf):
48+
buf = bytes_to_bytes_buf(bytes([129]))
49+
with pytest.raises(
50+
OSError,
51+
match=re.escape("malformed UTF-8 of 1 bytes at line 1 char 1"),
52+
):
53+
list(load(buf))

0 commit comments

Comments
 (0)