-
Notifications
You must be signed in to change notification settings - Fork 0
gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage return #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…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.
WalkthroughAdds a new test that reproduces reading a line ending with a standalone CR and exercising TextIOWrapper.tell/seek/read, updates the C tell implementation assertion to validate Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Test as Test file
participant PyAPI as TextIOWrapper (Python)
participant CImpl as _io_TextIOWrapper_tell_impl (C)
Test->>PyAPI: open file, readline()
PyAPI->>CImpl: compute internal buffer/positions
CImpl->>CImpl: compute/validate skip_bytes (was skip_back)
Note over CImpl: assertion now checks `skip_bytes`
CImpl-->>PyAPI: return position
PyAPI-->>Test: tell()/seek()/read() results used by test
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
🔇 Additional comments (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
- Remove trailing whitespace - Add missing newline at end of NEWS file
- 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.
b298058 to
e84d686
Compare
Summary
io.TextIOWrapper.tell()when reading files ending with standalone\rRoot Cause
The tell() optimization algorithm assumed buffered data would always be available when calculating position, but files ending with standalone
\rcreate empty snapshots, causingassert(skip_back <= PyBytes_GET_SIZE(next_input))to fail.Fix
Added check to skip optimization when
next_inputis empty, preventing the assertion failure while maintaining correct position calculation.Test Plan
test_tell_after_readline_with_crintest_textio.pySummary by CodeRabbit
Bug Fixes
Tests