Skip to content

Commit

Permalink
Add conditional string encoding based on urllib3 major version
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Jul 18, 2024
1 parent f8aa36b commit d51bf99
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/requests/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
import importlib
import sys

# -------
# urllib3
# -------
from urllib3 import __version__ as urllib3_version

# Detect which major version of urllib3 is being used.
try:
is_urllib3_2 = int(urllib3_version.split('.')[0]) == 2
except (TypeError, AttributeError):
# If we can't discern a version, prefer old functionality.
is_urllib3_2 = False

# -------------------
# Character Detection
# -------------------
Expand Down
5 changes: 4 additions & 1 deletion src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
)
from .compat import parse_http_list as _parse_list_header
from .compat import (
is_urllib3_2,
proxy_bypass,
proxy_bypass_environment,
quote,
Expand Down Expand Up @@ -136,7 +137,9 @@ def super_len(o):
total_length = None
current_position = 0

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

if hasattr(o, "__len__"):
Expand Down

0 comments on commit d51bf99

Please sign in to comment.