diff --git a/src/requests/compat.py b/src/requests/compat.py index 4e843c6cf1..7f9d754350 100644 --- a/src/requests/compat.py +++ b/src/requests/compat.py @@ -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 diff --git a/src/requests/utils.py b/src/requests/utils.py index be7fc1d2f6..699683e5d9 100644 --- a/src/requests/utils.py +++ b/src/requests/utils.py @@ -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 ( @@ -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") diff --git a/tests/test_requests.py b/tests/test_requests.py index df0d329eaf..d8fbb23688 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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 @@ -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):