Skip to content

More robust check for upload files in binary mode - #2630

Merged
lovelydinosaur merged 7 commits into
encode:masterfrom
lkuchenb:fix/multipart_filemode_check
Apr 20, 2023
Merged

More robust check for upload files in binary mode#2630
lovelydinosaur merged 7 commits into
encode:masterfrom
lkuchenb:fix/multipart_filemode_check

Conversation

@lkuchenb

Copy link
Copy Markdown
Contributor

This is a minor patch proposal fixing fixing an issue with a recently introduced check for binary file objects in multipart uploads. The currently used check

if"b"notingetattr(fileobj, "mode", "b"):
raiseTypeError(
"Multipart file uploads must be opened in binary mode, not text mode."
)

misses out on file objects that read and write byte-like objects, but have the mode attribute set to r such as ZipFile.open() which is binary-only.

@lkuchenb

Copy link
Copy Markdown
ContributorAuthor

The StringIO check is now somewhat obsolete, I've changed the order so that the more specific test comes first, but they could probably also be merged into one with a meaningful combined message.

@lovelydinosaur

Copy link
Copy Markdown
Contributor

do we think it's worth adding an extra test for this case or we happy to accept the pr as-is?

@lovelydinosaurlovelydinosaur changed the title Fix check for binary modeMore robust check for upload files in binary modeApr 20, 2023
@lovelydinosaur
lovelydinosaur merged commit 472597f into encode:masterApr 20, 2023
@lovelydinosaur

Copy link
Copy Markdown
Contributor

Thanks!

@lkuchenb

Copy link
Copy Markdown
ContributorAuthor

Thanks for the review @tomchristie - I didn't think covering this with a test would make much sense since the test would involve intentionally making false assumptions about standard library behavior, not something one would typically want to test IMHO.

@lkuchenb
lkuchenb deleted the fix/multipart_filemode_check branch May 10, 2023 14:42
@epenetepenet mentioned this pull request May 17, 2023
samclearman pushed a commit to titanmsp/httpx that referenced this pull request Apr 26, 2025
* Fix check for binary mode
* Change order of type checks
---------
Co-authored-by: Tom Christie <tom@tomchristie.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@lkuchenb@lovelydinosaur