From 53bdae2089e34addaca3d09f0bf9e2dd84af0073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on=20Kuchenbecker?= Date: Tue, 21 Mar 2023 16:26:48 +0100 Subject: [PATCH 1/2] Fix check for binary mode --- httpx/_multipart.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpx/_multipart.py b/httpx/_multipart.py index 1d46d96a98..8648125cde 100644 --- a/httpx/_multipart.py +++ b/httpx/_multipart.py @@ -122,7 +122,7 @@ def __init__(self, name: str, value: FileTypes) -> None: # 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"): + if isinstance(fileobj, io.TextIOBase): raise TypeError( "Multipart file uploads must be opened in binary mode, not text mode." ) From 4746044fea387af571f721be2061ef33a93d76c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on=20Kuchenbecker?= Date: Tue, 21 Mar 2023 21:37:11 +0100 Subject: [PATCH 2/2] Change order of type checks --- httpx/_multipart.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httpx/_multipart.py b/httpx/_multipart.py index 8648125cde..446f4ad2df 100644 --- a/httpx/_multipart.py +++ b/httpx/_multipart.py @@ -122,14 +122,14 @@ def __init__(self, name: str, value: FileTypes) -> None: # requests does the opposite (it overwrites the header with the 3rd tuple element) headers["Content-Type"] = content_type - if isinstance(fileobj, io.TextIOBase): - 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