https://github.com/python/cpython/commit/af80fac42548719ede7241bfbab3c2c0775b4760
commit: af80fac42548719ede7241bfbab3c2c0775b4760
branch: main
author: Mohsin Mehmood <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-11-11T23:49:54+02:00
summary:
gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage
return (GH-141331)
The assertion was checking wrong variable (skip_back vs skip_bytes).
files:
A Misc/NEWS.d/next/Library/2025-11-10-01-47-18.gh-issue-141314.baaa28.rst
M Lib/test/test_io/test_textio.py
M Modules/_io/textio.c
diff --git a/Lib/test/test_io/test_textio.py b/Lib/test/test_io/test_textio.py
index d8d0928b4ba69b..6331ed2b958552 100644
--- a/Lib/test/test_io/test_textio.py
+++ b/Lib/test/test_io/test_textio.py
@@ -686,6 +686,25 @@ def test_multibyte_seek_and_tell(self):
self.assertEqual(f.tell(), p1)
f.close()
+ def test_tell_after_readline_with_cr(self):
+ # Test for gh-141314: TextIOWrapper.tell() assertion failure
+ # when dealing with standalone carriage returns
+ data = b'line1\r'
+ with self.open(os_helper.TESTFN, "wb") as f:
+ f.write(data)
+
+ with self.open(os_helper.TESTFN, "r") as f:
+ # Read line that ends with \r
+ line = f.readline()
+ self.assertEqual(line, "line1\n")
+ # This should not cause an assertion failure
+ pos = f.tell()
+ # Verify we can seek back to this position
+ f.seek(pos)
+ remaining = f.read()
+ self.assertEqual(remaining, "")
+
+
def test_seek_with_encoder_state(self):
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
f.write("\u00e6\u0300")
diff --git
a/Misc/NEWS.d/next/Library/2025-11-10-01-47-18.gh-issue-141314.baaa28.rst
b/Misc/NEWS.d/next/Library/2025-11-10-01-47-18.gh-issue-141314.baaa28.rst
new file mode 100644
index 00000000000000..37acaabfa3eada
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-11-10-01-47-18.gh-issue-141314.baaa28.rst
@@ -0,0 +1 @@
+Fix assertion failure in :meth:`io.TextIOWrapper.tell` when reading files with
standalone carriage return (``\r``) line endings.
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 84b7d9df400578..65da300abcf3bc 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2845,7 +2845,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
current pos */
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
skip_back = 1;
- assert(skip_back <= PyBytes_GET_SIZE(next_input));
+ assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
input = PyBytes_AS_STRING(next_input);
while (skip_bytes > 0) {
/* Decode up to temptative start point */
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]