]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
More robust check for upload files in binary mode (#2630)
authorLeon Kuchenbecker <science@lkit.eu>
Thu, 20 Apr 2023 11:52:44 +0000 (13:52 +0200)
committerGitHub <noreply@github.com>
Thu, 20 Apr 2023 11:52:44 +0000 (12:52 +0100)
* Fix check for binary mode

* Change order of type checks

---------

Co-authored-by: Tom Christie <tom@tomchristie.com>
httpx/_multipart.py

index 1d46d96a989b1b7269253046039350d10efe1172..446f4ad2df3eb0b566e11c9aab9bbfc4875edfba 100644 (file)
@@ -122,14 +122,14 @@ class FileField:
             # requests does the opposite (it overwrites the header with the 3rd tuple element)
             headers["Content-Type"] = content_type
 
-        if "b" not in getattr(fileobj, "mode", "b"):
-            raise TypeError(
-                "Multipart file uploads must be opened in binary mode, not text mode."
-            )
         if isinstance(fileobj, io.StringIO):
             raise TypeError(
                 "Multipart file uploads require 'io.BytesIO', not 'io.StringIO'."
             )
+        if isinstance(fileobj, io.TextIOBase):
+            raise TypeError(
+                "Multipart file uploads must be opened in binary mode, not text mode."
+            )
 
         self.filename = filename
         self.file = fileobj