Skip to content

Commit af80fac

Browse files
authored
gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage return (GH-141331)
The assertion was checking wrong variable (skip_back vs skip_bytes).
1 parent 37e2762 commit af80fac

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

‎Lib/test/test_io/test_textio.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,25 @@ def test_multibyte_seek_and_tell(self):
686686
self.assertEqual(f.tell(), p1)
687687
f.close()
688688

689+
def test_tell_after_readline_with_cr(self):
690+
# Test for gh-141314: TextIOWrapper.tell() assertion failure
691+
# when dealing with standalone carriage returns
692+
data = b'line1\r'
693+
with self.open(os_helper.TESTFN, "wb") as f:
694+
f.write(data)
695+
696+
with self.open(os_helper.TESTFN, "r") as f:
697+
# Read line that ends with \r
698+
line = f.readline()
699+
self.assertEqual(line, "line1\n")
700+
# This should not cause an assertion failure
701+
pos = f.tell()
702+
# Verify we can seek back to this position
703+
f.seek(pos)
704+
remaining = f.read()
705+
self.assertEqual(remaining, "")
706+
707+
689708
def test_seek_with_encoder_state(self):
690709
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
691710
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
@@ -2845,7 +2845,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
28452845
current pos */
28462846
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
28472847
skip_back = 1;
2848-
assert(skip_back <= PyBytes_GET_SIZE(next_input));
2848+
assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
28492849
input = PyBytes_AS_STRING(next_input);
28502850
while (skip_bytes > 0) {
28512851
/* Decode up to temptative start point */

0 commit comments

Comments
 (0)