diff --git a/Lib/test/test_io/test_textio.py b/Lib/test/test_io/test_textio.py index 246b2dc6087bd3..b4aa7397860a10 100644 --- a/Lib/test/test_io/test_textio.py +++ b/Lib/test/test_io/test_textio.py @@ -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', diff --git a/Misc/NEWS.d/next/Library/2026-11-01-01-03-27.gh-issue-161053.crBlVH.rst b/Misc/NEWS.d/next/Library/2026-11-01-01-03-27.gh-issue-161053.crBlVH.rst new file mode 100644 index 00000000000000..bdb013447a439a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-01-01-03-27.gh-issue-161053.crBlVH.rst @@ -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`.