Skip to content

Preserve buffered bytes in StreamingIterator - #404

Draft
lexingtonhibiki wants to merge 3 commits into
requests:mainfrom
lexingtonhibiki:fix/streaming-iterator-buffered-read-all
Draft

lexingtonhibiki wants to merge 3 commits into
requests:mainfrom
lexingtonhibiki:fix/streaming-iterator-buffered-read-all

Conversation

@lexingtonhibiki

@lexingtonhibiki lexingtonhibiki commented Oct 21, 2026 •

Copy link
Copy Markdown

Problem

A sized read can leave unread bytes in StreamingIterator's internal buffer. A subsequent read() or read(-1) originally joined only the remaining iterator, silently dropping those buffered bytes.

from requests_toolbelt import StreamingIterator

stream = StreamingIterator(8, iter([b'abcdef', b'gh']))
assert stream.read(2) == b'ab'
assert stream.read() == b'cdefgh'  # previously returned b'gh'

Two related read-path inconsistencies are also covered:

  • Finite empty iterator chunks could be returned as EOF during sized reads, even with nonempty chunks still available.
  • Text chunks are encoded during sized reads, but read-all joined them without encoding and raised TypeError. For example, a UTF-8 text iterator works with repeated read(1) but failed with read() or a sized read followed by read().

Change

  • Drain the buffered tail before joining the remaining iterator.
  • Skip finite empty iterator chunks until nonempty data or actual iterator exhaustion.
  • Encode only text chunks during read-all with the configured encoding. Existing bytes-like chunks remain untouched, and invalid non-text inputs retain their read-all TypeError behavior.

The file-like input path is unchanged. Regression coverage includes partial chunks, buffered tails after iterator exhaustion, empty input and chunks, stable EOF, UTF-8 and Latin-1 text, mixed text/bytes, bytes-like inputs, and encoding errors.

Current-head validation

Local Python 3.12 focused checks, with DNS/connect and subprocess guards:

  • Complete StreamingIterator test module: 56 passed, with DeprecationWarning treated as an error; zero guard events. The byte-identical test module was run outside the unrelated project cassette configuration.
  • The same final test module against the preceding head 13d1f553: 7 failed / 49 passed. The 23 added cases expose six text read-all inconsistencies plus the encoding-error path, and include compatibility controls.
  • Independent AI review ran the 56 focused cases plus 11 additional boundary controls. No human-review claim is made.
  • git diff --check passes.

The initial text-encoding candidate was rejected in review because it also transformed bytes-like and invalid inputs; the final change encodes text types only and preserves those controls.

Older Python versions, the latest full project suite, tox and documentation build have not been run for this head. Hosted CI results are not claimed here; see the checks for the exact current commit.

Historical validation, not current-head full-suite results

The earlier buffered-tail-only revision had 26 StreamingIterator tests passing and 58 focused streaming/multipart/monitor tests passing with three URL-wrapper cases deselected. Its full suite with deprecation warnings ignored had 184 passed / 5 skipped, versus 182 passed / 2 regression failures / 5 skipped for its baseline. Under strict deprecation warnings, that revision had 157 passed / 27 failed / 5 skipped: the baseline had the same 27 Betamax datetime.utcnow() deprecation failures plus its two regression failures. These historical runs do not validate the latest code as a full-suite pass.

Prepared with AI assistance. No dependency changes are included.

Drain the unread buffer before joining the remaining iterator on read().
This prevents data loss after a sized read stops inside a yielded chunk.

Cover partial chunks, exhausted iterators, empty input, repeated EOF reads,
and equivalent file-like inputs.
Keep finite empty byte chunks from looking like EOF to sized-read consumers. Preserve the buffered read-all correction and file-like behavior, and add regression coverage in the existing streaming iterator test module.
@lexingtonhibiki

lexingtonhibiki commented Oct 21, 2026 •

Copy link
Copy Markdown
Author

Added a related boundary fix in 13d1f553d1829ed91306c7671cd35da84e7eaf6d: finite empty chunks from an iterator no longer look like EOF to sized-read consumers. The earlier buffered read-all fix is preserved, and file-like inputs still use their existing path.

Seven added regressions in the existing test module produced two failures before this change and now pass. An independent AI review also ran a 484-case finite-input comparison: 200 false-EOF/data-loss cases before, zero after. These were local guarded checks, not a full-suite pass. The initial evidence harness executed the seven new test methods only. A subsequent check using an existing pytest environment ran the complete, byte-identical StreamingIterator test module with its fixtures, parameterization, and strict deprecation-warning policy: 33 passed on this head, versus 2 failed / 31 passed on the previous source. Both runs had zero network/process guard events. This isolated module run omits unrelated Betamax cassette configuration; it is not a full-project pytest/tox pass.

The new head currently has a failed Read the Docs status: its log stops during checkout with fatal: reference is not a tree: 13d1f553d1829ed91306c7671cd35da84e7eaf6d, before any documentation build. GitHub Actions currently reports action_required. Earlier CI/docs results do not apply to this head.

Keep text iterator chunks consistent across sized and read-all reads.\nOnly encode text values so existing bytes-like chunks and invalid-input\nTypeError behavior are preserved.
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.

2 participants