Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage return#141331
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 ourterms of service andprivacy 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?
gh-141314: Fix TextIOWrapper.tell() assertion failure with standalone carriage return#141331
Conversation
…dalone carriage returnWhen TextIOWrapper.tell() is called after reading a line that ends with astandalone carriage return (\r), the tell optimization algorithm incorrectlyassumes there is buffered data to search through. This causes an assertionfailure 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 optimizationcannot function without buffered data.
…dalone carriage returnAdd test case and fix assertion failure in TextIOWrapper.tell() when readingfiles that end with a standalone carriage return (\r). The optimizationalgorithm incorrectly assumed buffered data would always be available,causing an assertion failure when next_input is empty.
- Remove trailing whitespace- Add missing newline at end of NEWS file
| skip_back=1; | ||
| assert(skip_back <=PyBytes_GET_SIZE(next_input)); | ||
| input=PyBytes_AS_STRING(next_input); | ||
| while (skip_bytes>0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
| while (skip_bytes>0) { | |
| } | |
| while (skip_bytes>0) { |
I think it is better - you don't need to ident while body in this this case.
| skip_back *=2; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
| } |
| self.assertEqual(f.tell(),p1) | ||
| f.close() | ||
| deftest_tell_after_readline_with_cr(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Could you please check how your solution works with data that have multiple\r char?
Uh oh!
There was an error while loading.Please reload this page.
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.py_io_TextIOWrapper_tell_impl: Assertion 'skip_back <= PyBytes_GET_SIZE(next_input)' failed#141314