Skip to content

Commit 92c14a9

Browse files
authored
[3.14] pythongh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage return (pythonGH-141331) (pythonGH-141453)
The assertion was checking wrong variable (skip_back vs skip_bytes). (cherry picked from commit af80fac)
1 parent e9c11b7 commit 92c14a9

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
@@ -3337,6 +3337,24 @@ def test_multibyte_seek_and_tell(self):
33373337
self.assertEqual(f.tell(), p1)
33383338
f.close()
33393339

3340+
def test_tell_after_readline_with_cr(self):
3341+
# Test for gh-141314: TextIOWrapper.tell() assertion failure
3342+
# when dealing with standalone carriage returns
3343+
data = b'line1\r'
3344+
with self.open(os_helper.TESTFN, "wb") as f:
3345+
f.write(data)
3346+
3347+
with self.open(os_helper.TESTFN, "r") as f:
3348+
# Read line that ends with \r
3349+
line = f.readline()
3350+
self.assertEqual(line, "line1\n")
3351+
# This should not cause an assertion failure
3352+
pos = f.tell()
3353+
# Verify we can seek back to this position
3354+
f.seek(pos)
3355+
remaining = f.read()
3356+
self.assertEqual(remaining, "")
3357+
33403358
def test_seek_with_encoder_state(self):
33413359
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
33423360
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
@@ -2844,7 +2844,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
28442844
current pos */
28452845
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
28462846
skip_back = 1;
2847-
assert(skip_back <= PyBytes_GET_SIZE(next_input));
2847+
assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
28482848
input = PyBytes_AS_STRING(next_input);
28492849
while (skip_bytes > 0) {
28502850
/* Decode up to temptative start point */

0 commit comments

Comments
 (0)