From: Leon Kuchenbecker Date: Thu, 20 Apr 2023 11:52:44 +0000 (+0200) Subject: More robust check for upload files in binary mode (#2630) X-Git-Tag: 0.24.1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=472597fb6b63fc112f2e6ab52bab871a10b08929;p=thirdparty%2Fhttpx.git 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 --- diff --git a/httpx/_multipart.py b/httpx/_multipart.py index 1d46d96a..446f4ad2 100644 --- a/httpx/_multipart.py +++ b/httpx/_multipart.py @@ -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