Skip to content

Commit 8402c28

Browse files
authored
[3.13] gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage return (GH-141331) (GH-141452)
The assertion was checking wrong variable (skip_back vs skip_bytes). (cherry picked from commit af80fac)
1 parent c317cf5 commit 8402c28

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_io.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,24 @@ def test_multibyte_seek_and_tell(self):
33193319
self.assertEqual(f.tell(), p1)
33203320
f.close()
33213321

3322+
def test_tell_after_readline_with_cr(self):
3323+
# Test for gh-141314: TextIOWrapper.tell() assertion failure
3324+
# when dealing with standalone carriage returns
3325+
data = b'line1\r'
3326+
with self.open(os_helper.TESTFN, "wb") as f:
3327+
f.write(data)
3328+
3329+
with self.open(os_helper.TESTFN, "r") as f:
3330+
# Read line that ends with \r
3331+
line = f.readline()
3332+
self.assertEqual(line, "line1\n")
3333+
# This should not cause an assertion failure
3334+
pos = f.tell()
3335+
# Verify we can seek back to this position
3336+
f.seek(pos)
3337+
remaining = f.read()
3338+
self.assertEqual(remaining, "")
3339+
33223340
def test_seek_with_encoder_state(self):
33233341
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
33243342
f.write("\u00e6\u0300")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix assertion failure in :meth:`io.TextIOWrapper.tell` when reading files with standalone carriage return (``\r``) line endings.

Modules/_io/textio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2829,7 +2829,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
28292829
current pos */
28302830
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
28312831
skip_back = 1;
2832-
assert(skip_back <= PyBytes_GET_SIZE(next_input));
2832+
assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
28332833
input = PyBytes_AS_STRING(next_input);
28342834
while (skip_bytes > 0) {
28352835
/* Decode up to temptative start point */

0 commit comments

Comments
 (0)