Skip to content

Preserve unread text when SpooledStringIO rolls over - #582

Closed
Aryangupta-05 wants to merge 1 commit into
mahmoud:masterfrom
Aryangupta-05:fix/spooled-text-rollover-position
Closed

Aryangupta-05 wants to merge 1 commit into
mahmoud:masterfrom
Aryangupta-05:fix/spooled-text-rollover-position

Conversation

@Aryangupta-05

Copy link
Copy Markdown

The change

Fixes #581. Preserve the UTF-8 reader's pending bytes, decoded characters and buffered lines when SpooledStringIO rolls over to a temporary file. Keeping only the underlying byte position skips unread text after readline() and can raise UnicodeDecodeError if read-ahead ended inside a multibyte character.

The patch keeps the existing byte position and transfers the reader buffers, preserving existing read/write behavior. Tests cover explicit rollover, fileno(), automatic rollover during a write, multiline and multibyte text, and a length-query compatibility boundary. Existing length-query behavior is outside this fix's scope.

Validation: full tests and doctests on Windows / CPython 3.11: 681 passed, 18 subtests passed. git diff --check passed. The regression tests fail on the original implementation with skipped text or a Unicode decoding error. The full suite needed normal local socket access for an unrelated Windows asyncio test.

The backstory

i'm getting started with open source and want to learn by fixing real bugs. small, reusable python utilities interest me because they're a practical way to understand how reliable library code is built. this report was selected while looking for an existing bug to contribute to; it did not come from prior production use of boltons.

AI assistance

  • Harness/tooling: OpenAI Codex. Investigation, code, tests and this description (including the backstory draft) were AI-assisted. No claim of personal production use or independent human review is made.
  • Model: GPT-6 through Codex; the exact model variant is not exposed in this session.
  • Original prompts: The account owner asked for five useful open-source contributions across different established repositories for learning and career opportunities. The latest continuation prompt was now conitnue to do your work. When asked for a personal backstory, the owner requested genrate a sentnce and make it all in lower case letter and human text. The backstory above is a disclosed draft based on that stated intent.

Retain pending UTF-8 bytes, decoded characters and buffered lines so rollover and fileno do not skip unread text or start decoding mid-character.

Fixes mahmoud#581.

AI assistance: OpenAI Codex (GPT-6) investigated, implemented and validated this change at the account owner request.
@XtReL

XtReL commented Oct 19, 2026

Copy link
Copy Markdown

I ran additional checks on base 4e5faa3 and head f3017b5 using CPython 3.12.15 on Linux.

All 166 reading cases matched a text model and io.StringIO on head, including fragment boundaries and positions. Base differed in 64 cases. These are additional manifestations of the known read ahead problem, not 64 new defects. The set includes Unicode, CRLF and repeated rollover and fileno calls.

In 36 mixed operation, length query and error cases, head matched its behavior without rollover. This checks compatibility rather than independent correctness of those operations. Each set was repeated three times with matching case records, and two targeted mutations were detected. No head regression was observed in this finite set. The full suite and other platforms were not tested.

I have attached a short report and reproducible evidence. This was AI assisted using Codex and GPT 6.1 Sol, after reading the patch and existing tests.
boltons-PR582-evidence.zip
boltons-PR582-review-EN.pdf

@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Same bug as #581/#591; the fix in #591 avoids reaching into codecs.StreamReader internals, so that is the one folded. Thanks.

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.

SpooledStringIO.rollover() skips unread text after readline()

3 participants