Skip to content

Commit d51bf99

Browse files
committed
Add conditional string encoding based on urllib3 major version
1 parent f8aa36b commit d51bf99

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/requests/compat.py

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
import importlib
1111
import sys
1212

13+
# -------
14+
# urllib3
15+
# -------
16+
from urllib3 import __version__ as urllib3_version
17+
18+
# Detect which major version of urllib3 is being used.
19+
try:
20+
is_urllib3_2 = int(urllib3_version.split('.')[0]) == 2
21+
except (TypeError, AttributeError):
22+
# If we can't discern a version, prefer old functionality.
23+
is_urllib3_2 = False
24+
1325
# -------------------
1426
# Character Detection
1527
# -------------------

src/requests/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
from .compat import parse_http_list as _parse_list_header
4343
from .compat import (
44+
is_urllib3_2,
4445
proxy_bypass,
4546
proxy_bypass_environment,
4647
quote,
@@ -136,7 +137,9 @@ def super_len(o):
136137
total_length = None
137138
current_position = 0
138139

139-
if isinstance(o, str):
140+
if is_urllib3_2 and isinstance(o, str):
141+
# urllib3 2.x treats all strings as utf-8 instead
142+
# of latin-1 (iso-8859-1) like http.client.
140143
o = o.encode("utf-8")
141144

142145
if hasattr(o, "__len__"):

0 commit comments

Comments
 (0)