Skip to content

Keep text streams usable after reverse iteration - #466

Closed
codewithfourtix wants to merge 1 commit into
mahmoud:masterfrom
codewithfourtix:fix-reverse-lines-borrowed-stream
Closed

codewithfourtix wants to merge 1 commit into
mahmoud:masterfrom
codewithfourtix:fix-reverse-lines-borrowed-stream

Conversation

@codewithfourtix

Copy link
Copy Markdown
Contributor

Reverse iteration detaches the buffer from a caller-owned text stream. This makes later reads and even exiting its with block raise ValueError. Read from the existing buffer without detaching it.

Adds a regression that reverses, seeks, reads, and closes the same text stream.

Copilot AI lite review requested due to automatic review settings September 5, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The touched reverse_iter_lines() code still contains an encoding override that forces UTF-8 for text streams, ignoring both the stream’s encoding and the encoding= argument.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes reverse_iter_lines() so reverse iteration on a caller-owned text stream no longer detaches the underlying buffer (which previously could break subsequent reads and even with-block cleanup), and adds a regression test to ensure the stream remains usable.

Changes:

  • Update reverse_iter_lines() to read from TextIOWrapper.buffer instead of calling detach().
  • Add a regression test that reverses, seeks, reads, and closes the same text stream.
File summaries
File Description
boltons/jsonutils.py Avoids detaching buffers during reverse iteration by using the existing .buffer on text streams.
tests/test_jsonutils.py Adds a regression test ensuring a text stream still supports seek/read and closes cleanly after reverse iteration.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread boltons/jsonutils.py
Comment on lines 84 to +86
orig_obj = file_obj
try:
file_obj = orig_obj.detach()
file_obj = orig_obj.buffer
mahmoud added a commit that referenced this pull request Sep 7, 2026
Fold #463, #466, #476, #478, #460, #459, and #477 with focused regressions and 26.2.0 release notes.

Verified 672 local tests and the full GitHub Actions matrix before merge.

Co-authored-by: Ali Zulfiqar <[email protected]>
Co-authored-by: Guo Jiarui <[email protected]>
Co-authored-by: Nikolaus Schuetz <[email protected]>
Co-authored-by: Raiyan Zaman <[email protected]>
@mahmoud

mahmoud commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Folded into #481 with credit (Co-authored-by). Thanks!

@mahmoud mahmoud closed this Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants