Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone…
… carriage return

When TextIOWrapper.tell() is called after reading a line that ends with a
standalone carriage return (\r), the tell optimization algorithm incorrectly
assumes there is buffered data to search through. This causes an assertion
failure when skip_back=1 exceeds the empty buffer size.

The fix detects when next_input is empty and skips the optimization phase,
falling back to the byte-by-byte decoding method which always works correctly.
This properly handles the architectural constraint that buffer optimization
cannot function without buffered data.
  • Loading branch information
mohsinm-dev committed Nov 10, 2025
commit fe6dd199f46dc23e29cb9e4d6e1261d2656cf13d
18 changes: 18 additions & 0 deletions Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,24 @@ def test_multibyte_seek_and_tell(self):
self.assertEqual(f.tell(), p1)
f.close()

def test_tell_after_readline_with_cr(self):
Comment thread
sergey-miryanov marked this conversation as resolved.
# Test for gh-141314: TextIOWrapper.tell() assertion failure
# when dealing with standalone carriage returns
data = b'line1=1\r'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be simply 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=1\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")
Expand Down
15 changes: 11 additions & 4 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2844,10 +2844,16 @@ _io_TextIOWrapper_tell_impl(textio *self)
/* Fast search for an acceptable start point, close to our
current pos */
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
skip_back = 1;
assert(skip_back <= PyBytes_GET_SIZE(next_input));
input = PyBytes_AS_STRING(next_input);
while (skip_bytes > 0) {

/* Skip the optimization if next_input is empty */
if (PyBytes_GET_SIZE(next_input) == 0) {
skip_bytes = 0;
}
else {
skip_back = 1;
assert(skip_back <= PyBytes_GET_SIZE(next_input));
input = PyBytes_AS_STRING(next_input);
while (skip_bytes > 0) {
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated
/* Decode up to temptative start point */
if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
goto fail;
Expand All @@ -2870,6 +2876,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
skip_back *= 2;
}
}
}
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated
if (skip_bytes <= 0) {
skip_bytes = 0;
if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
Expand Down