Skip to content

Cache multipart body lengths while streaming - #418

Open
Aschenbath wants to merge 1 commit into
requests:mainfrom
Aschenbath:fix/cache-part-body-length-215-20261120
Open

Aschenbath wants to merge 1 commit into
requests:mainfrom
Aschenbath:fix/cache-part-body-length-215-20261120

Conversation

@Aschenbath

Copy link
Copy Markdown

Multipart streaming no longer repeatedly queries each body object's length. The encoder already fixes its advertised length when it creates the parts; retain that initial body length and subtract the bytes actually written. This avoids repeated remote size requests for file-like objects with expensive length properties while preserving partial-read handling.

Fixes #215.

All 12 regression cases fail on the original source. They verify exact multipart output, empty bodies, pre-positioned streams, short reads, multiple read sizes, and no size queries after initialization. The complete Python 3.11 suite passes with deprecation warnings treated as errors (178 passed, five skipped). Existing core Flake8 findings remain; the new code introduces none. Documentation builds with the same pre-existing theme deprecation warning. Python 2.7 was not run locally.

Multipart lengths are determined before streaming, but each loop queries
the body again. For remote file objects this can cause repeated network
requests just to obtain the size. Track the remaining bytes locally and
subtract actual writes, including short reads.

Closes requests#215
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.

MultipartEncoder assumes total_len(file) is cheap

1 participant