gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage return by mohsinm-dev · Pull Request #141331 · python/cpython

added 2 commits

November 10, 2025 06:31
…dalone 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.
…dalone carriage return

Add test case and fix assertion failure in TextIOWrapper.tell() when reading
files that end with a standalone carriage return (\r). The optimization
algorithm incorrectly assumed buffered data would always be available,
causing an assertion failure when next_input is empty.

@mohsinm-dev

- Remove trailing whitespace
- Add missing newline at end of NEWS file

sergey-miryanov

sergey-miryanov

sergey-miryanov

@mohsinm-dev

@mohsinm-dev

serhiy-storchaka

sergey-miryanov

@mohsinm-dev

- 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.

serhiy-storchaka

efimov-mikhail

sergey-miryanov

This was referenced

Nov 12, 2025

mohsinm-dev added a commit to mohsinm-dev/cpython that referenced this pull request

Nov 12, 2025
…th standalone carriage return (pythonGH-141331)

The assertion was checking wrong variable (skip_back vs skip_bytes).
(cherry picked from commit af80fac)

Co-authored-by: Mohsin Mehmood <55545648+mohsinm-dev@users.noreply.github.com>

serhiy-storchaka pushed a commit that referenced this pull request

Nov 12, 2025
…ndalone carriage return (GH-141331) (GH-141453)

The assertion was checking wrong variable (skip_back vs skip_bytes).
(cherry picked from commit af80fac)

serhiy-storchaka pushed a commit that referenced this pull request

Nov 12, 2025
…ndalone carriage return (GH-141331) (GH-141452)

The assertion was checking wrong variable (skip_back vs skip_bytes).
(cherry picked from commit af80fac)

StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request

Dec 6, 2025
…dalone carriage return (pythonGH-141331)

The assertion was checking wrong variable (skip_back vs skip_bytes).