Skip to content

Commit

Permalink
Invert major version check
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Jul 23, 2024
1 parent 4e38364 commit 01353d3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/requests/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

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

# -------------------
# Character Detection
Expand Down
6 changes: 3 additions & 3 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
getproxies,
getproxies_environment,
integer_types,
is_urllib3_2,
is_urllib3_1,
)
from .compat import parse_http_list as _parse_list_header
from .compat import (
Expand Down Expand Up @@ -137,8 +137,8 @@ def super_len(o):
total_length = None
current_position = 0

if is_urllib3_2 and isinstance(o, str):
# urllib3 2.x treats all strings as utf-8 instead
if not is_urllib3_1 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")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
builtin_str,
cookielib,
getproxies,
is_urllib3_2,
is_urllib3_1,
urlparse,
)
from requests.cookies import cookiejar_from_dict, morsel_to_cookie
Expand Down Expand Up @@ -2961,7 +2961,7 @@ def test_content_length_for_bytes_data(httpbin):


@pytest.mark.skipif(
not is_urllib3_2,
is_urllib3_1,
reason="urllib3 2.x encodes all strings to utf-8, urllib3 1.x uses latin-1",
)
def test_content_length_for_string_data_counts_bytes(httpbin):
Expand Down

0 comments on commit 01353d3

Please sign in to comment.