Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,33 @@ def test_tell_after_readline_with_cr(self):
remaining = f.read()
self.assertEqual(remaining, "")

def test_tell_after_seek_with_chars_to_skip(self):
# tell() after seek() to a position inside the output of one decoder
# step, when the chunk read before had many bytes per character.
for encoding, errors, data, decoded in [
("big5hkscs", "ignore", b"\x88\x62" + b"\xff" * 8190,
"\xca\u0304"),
("utf-7", "strict", b"+AGEAYg-+" + b"A" * 8183, "ab"),
]:
with self.subTest(encoding=encoding):
f = self.TextIOWrapper(self.BytesIO(data),
encoding=encoding, errors=errors)
self.assertEqual(f.read(1), decoded[0])
pos = f.tell()
f.seek(pos)
self.assertEqual(f.tell(), pos)
self.assertEqual(f.read(1), decoded[1])

def test_tell_after_seek_after_lone_cr(self):
data = b"\xff" * 8000 + "a\r\u3042\n".encode()
f = self.TextIOWrapper(self.BytesIO(data),
encoding="utf-8", errors="ignore")
self.assertEqual(f.readline(), "a\n")
pos = f.tell()
f.seek(pos)
self.assertEqual(f.tell(), pos)
self.assertEqual(f.read(), "\u3042\n")

def test_tell_after_seek_past_cr(self):
# tell() must tolerate a stale bytes-per-char ratio after seek().
for data in (b'one\rtwo\rthree\r',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`io.TextIOWrapper.tell` reading past the end of an internal buffer
when called after :meth:`~io.TextIOWrapper.seek`, which could make it raise a
spurious :exc:`UnicodeDecodeError`.
Loading