Skip to content

Commit e84d686

Browse files
committed
Fix TextIOWrapper assertion typo and simplify test
- Fix assertion to check skip_bytes instead of skip_back - Use simpler test data b'line1\r' instead of b'line1=1\r' - Remove unnecessary multiple CR test case - Clean up workaround code The assertion was checking wrong variable (skip_back vs skip_bytes). skip_back is search step size, skip_bytes is buffer offset needing validation.
1 parent 1d9f77f commit e84d686

File tree

2 files changed

+5
-49
lines changed

2 files changed

+5
-49
lines changed

Lib/test/test_io/test_textio.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -689,60 +689,21 @@ def test_multibyte_seek_and_tell(self):
689689
def test_tell_after_readline_with_cr(self):
690690
# Test for gh-141314: TextIOWrapper.tell() assertion failure
691691
# when dealing with standalone carriage returns
692-
data = b'line1=1\r'
692+
data = b'line1\r'
693693
with self.open(os_helper.TESTFN, "wb") as f:
694694
f.write(data)
695695

696696
with self.open(os_helper.TESTFN, "r") as f:
697697
# Read line that ends with \r
698698
line = f.readline()
699-
self.assertEqual(line, "line1=1\n")
699+
self.assertEqual(line, "line1\n")
700700
# This should not cause an assertion failure
701701
pos = f.tell()
702702
# Verify we can seek back to this position
703703
f.seek(pos)
704704
remaining = f.read()
705705
self.assertEqual(remaining, "")
706706

707-
def test_tell_after_readline_with_multiple_cr(self):
708-
# Test for gh-141314: TextIOWrapper.tell() assertion failure
709-
# when dealing with multiple standalone carriage returns
710-
test_cases = [
711-
(b'line1\r\rline2\r', ['line1\n', '\n', 'line2\n']),
712-
(b'line1\r\r\rline2\r', ['line1\n', '\n', '\n', 'line2\n']),
713-
(b'line1\rline2\rline3\r', ['line1\n', 'line2\n', 'line3\n']),
714-
(b'\r\rdata\r', ['\n', '\n', 'data\n']),
715-
]
716-
717-
for data, expected_lines in test_cases:
718-
with self.subTest(data=data):
719-
with self.open(os_helper.TESTFN, "wb") as f:
720-
f.write(data)
721-
722-
with self.open(os_helper.TESTFN, "r") as f:
723-
# Read all lines and call tell() after each
724-
lines_read = []
725-
positions = []
726-
while True:
727-
pos_before = f.tell()
728-
line = f.readline()
729-
if not line:
730-
break
731-
lines_read.append(line)
732-
# This should not cause an assertion failure
733-
pos_after = f.tell()
734-
positions.append((pos_before, pos_after))
735-
736-
# Verify lines read correctly
737-
self.assertEqual(lines_read, expected_lines)
738-
739-
# Verify we can seek back to each position
740-
f.seek(0)
741-
for i, (pos_before, pos_after) in enumerate(positions):
742-
f.seek(pos_before)
743-
line = f.readline()
744-
self.assertEqual(line, expected_lines[i])
745-
self.assertEqual(f.tell(), pos_after)
746707

747708
def test_seek_with_encoder_state(self):
748709
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")

Modules/_io/textio.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,14 +2844,9 @@ _io_TextIOWrapper_tell_impl(textio *self)
28442844
/* Fast search for an acceptable start point, close to our
28452845
current pos */
28462846
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
2847-
/* Skip the optimization if next_input is empty */
2848-
if (PyBytes_GET_SIZE(next_input) == 0) {
2849-
skip_bytes = 0;
2850-
} else {
2851-
skip_back = 1;
2852-
assert(skip_back <= PyBytes_GET_SIZE(next_input));
2853-
input = PyBytes_AS_STRING(next_input);
2854-
}
2847+
skip_back = 1;
2848+
assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
2849+
input = PyBytes_AS_STRING(next_input);
28552850
while (skip_bytes > 0) {
28562851
/* Decode up to temptative start point */
28572852
if (_textiowrapper_decoder_setstate(self, &cookie) < 0)

0 commit comments

Comments
 (0)