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 <tom@tomchristie.com>
This commit is contained in:
Leon Kuchenbecker 2023-04-20 13:52:44 +02:00 committed by GitHub
parent 26dc39213a
commit 472597fb6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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