Skip to content

Explain unavailable multipart body lengths - #417

Open
Aschenbath wants to merge 1 commit into
requests:mainfrom
Aschenbath:fix/unknown-multipart-length-295-20261120
Open

Aschenbath wants to merge 1 commit into
requests:mainfrom
Aschenbath:fix/unknown-multipart-length-295-20261120

Conversation

@Aschenbath

Copy link
Copy Markdown

Fixes #295.

Constructing a MultipartEncoder with an unknown-length stream currently fails with an int + NoneType or NoneType - int error. Check for an unavailable length before those calculations and raise an explanatory TypeError, without consuming the input. The total_len() helper keeps its existing None result, and exceptions from broken length or file-descriptor implementations still propagate unchanged.

Add regressions for both failure paths, known empty/nonempty streams, and exception propagation. Document the known-length requirement. This follows the two-path diagnosis in the issue discussion.

Validation on Windows / Python 3.11:

  • Original source: 2 regression failures and 4 passing controls.
  • Full suite with deprecation warnings as errors: 172 passed, 5 skipped.
  • Package Flake8 and git diff --check pass. The changed test file retains three existing style findings; the previously unused pytest import is now used.
  • Sphinx HTML build passes with the existing theme-path deprecation warning.

Python 2.7 was not run locally. Prepared and tested with OpenAI Codex assistance.

Unknown-length streams fail with arithmetic errors when multipart parts
or file wrappers calculate their lengths. Detect the missing length at
those sites and report the unsupported input without reading its body.
Keep the helper's None result and propagate original stream exceptions.

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

Exception needed in encoder.total_len when all if conditions are False (requests_toolbelt)

1 participant