Skip to content

Commit 472597f

Browse files
More robust check for upload files in binary mode (#2630)
* Fix check for binary mode * Change order of type checks --------- Co-authored-by: Tom Christie <[email protected]>
1 parent 26dc392 commit 472597f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

httpx/_multipart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ def __init__(self, name: str, value: FileTypes) -> None:
122122
# requests does the opposite (it overwrites the header with the 3rd tuple element)
123123
headers["Content-Type"] = content_type
124124

125-
if "b" not in getattr(fileobj, "mode", "b"):
126-
raise TypeError(
127-
"Multipart file uploads must be opened in binary mode, not text mode."
128-
)
129125
if isinstance(fileobj, io.StringIO):
130126
raise TypeError(
131127
"Multipart file uploads require 'io.BytesIO', not 'io.StringIO'."
132128
)
129+
if isinstance(fileobj, io.TextIOBase):
130+
raise TypeError(
131+
"Multipart file uploads must be opened in binary mode, not text mode."
132+
)
133133

134134
self.filename = filename
135135
self.file = fileobj

0 commit comments

Comments
 (0)