]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage...
authorMohsin Mehmood <55545648+mohsinm-dev@users.noreply.github.com>
Wed, 12 Nov 2025 10:40:14 +0000 (15:40 +0500)
committerGitHub <noreply@github.com>
Wed, 12 Nov 2025 10:40:14 +0000 (12:40 +0200)
The assertion was checking wrong variable (skip_back vs skip_bytes).
(cherry picked from commit af80fac42548719ede7241bfbab3c2c0775b4760)

Lib/test/test_io.py
Misc/NEWS.d/next/Library/2025-11-10-01-47-18.gh-issue-141314.baaa28.rst [new file with mode: 0644]
Modules/_io/textio.c

index 5d83464a0dd44046cd337168f76cb41ff4092d47..fab1a0d4e51e511859c824f745a517875fe024b0 100644 (file)
@@ -3319,6 +3319,24 @@ class TextIOWrapperTest(unittest.TestCase):
         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 (file)
index 0000000..37acaab
--- /dev/null
@@ -0,0 +1 @@
+Fix assertion failure in :meth:`io.TextIOWrapper.tell` when reading files with standalone carriage return (``\r``) line endings.
index c1d8e16e12c72280f3f87abb5659bff46dd10f55..047d0fdfc76770feb271aea506c6d7c568a8a52e 100644 (file)
@@ -2829,7 +2829,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 */