Skip to content
Myr

Add conditional string encoding based on urllib3 major version

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nate Prewitt · committed by Nate Prewitt · parent f8aa36b · Browse files · 3 files+40 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+40 added−18 removed

  • tests:+24 added−17 removed
  • other:+16 added−1 removed

3 files+40 added−18 removed

modified:src/requests/compat.py+12 added
8 unmodified lines
910import importlib11import sys1213# -------14# urllib315# -------16from urllib3 import __version__ as urllib3_version1718# Detect which major version of urllib3 is being used.19try:20 is_urllib3_2 = int(urllib3_version.split(".")[0]) == 221except (TypeError, AttributeError):22 # If we can't discern a version, prefer old functionality.23 is_urllib3_2 = False2425# -------------------26# Character Detection27# -------------------28
78 unmodified lines
modified:src/requests/utils.py+4 added-1 removed
36 unmodified lines
37 bytes,38 getproxies,39 getproxies_environment,40 integer_types,41 is_urllib3_2,42)43from .compat import parse_http_list as _parse_list_header44from .compat import (45 proxy_bypass,
90 unmodified lines
136def super_len(o):137 total_length = None138 current_position = 0139139 if isinstance(o, str):140 if is_urllib3_2 and isinstance(o, str):141 # urllib3 2.x treats all strings as utf-8 instead142 # of latin-1 (iso-8859-1) like http.client.143 o = o.encode("utf-8")144145 if hasattr(o, "__len__"):146 total_length = len(o)
953 unmodified lines
modified:tests/test_requests.py+24 added-17 removed
23 unmodified lines
24 MutableMapping,25 builtin_str,26 cookielib,27 getproxies,28 is_urllib3_2,29 urlparse,30)31from requests.cookies import cookiejar_from_dict, morsel_to_cookie32from requests.exceptions import (
1777 unmodified lines
1810 p = req.prepare()18111812 assert p.headers["Content-Length"] == length18131813 def test_content_length_for_bytes_data(self, httpbin):1814 data = "This is a string containing multi-byte UTF-8 ☃️"1815 encoded_data = data.encode("utf-8")1816 length = str(len(encoded_data))1817 req = requests.Request("POST", httpbin("post"), data=encoded_data)1818 p = req.prepare()18191820 assert p.headers["Content-Length"] == length18211822 def test_content_length_for_string_data_counts_bytes(self, httpbin):1823 data = "This is a string containing multi-byte UTF-8 ☃️"1824 length = str(len(data.encode("utf-8")))1825 req = requests.Request("POST", httpbin("post"), data=data)1826 p = req.prepare()18271828 assert p.headers["Content-Length"] == length18291814 def test_nonhttp_schemes_dont_check_URLs(self):1815 test_urls = (1816 "data:image/gif;base64,R0lGODlhAQABAHAAACH5BAUAAAAALAAAAAABAAEAAAICRAEAOw==",1817 "file:///etc/passwd",
1131 unmodified lines
29492950 assert client_cert is not None295129522953def test_content_length_for_bytes_data(httpbin):2954 data = "This is a string containing multi-byte UTF-8 ☃️"2955 encoded_data = data.encode("utf-8")2956 length = str(len(encoded_data))2957 req = requests.Request("POST", httpbin("post"), data=encoded_data)2958 p = req.prepare()29592960 assert p.headers["Content-Length"] == length296129622963@pytest.mark.skipif(2964 not is_urllib3_2,2965 reason="urllib3 2.x encodes all strings to utf-8, urllib3 1.x uses latin-1",2966)2967def test_content_length_for_string_data_counts_bytes(httpbin):2968 data = "This is a string containing multi-byte UTF-8 ☃️"2969 length = str(len(data.encode("utf-8")))2970 req = requests.Request("POST", httpbin("post"), data=data)2971 p = req.prepare()29722973 assert p.headers["Content-Length"] == length297429752976def test_json_decode_errors_are_serializable_deserializable():2977 json_decode_error = requests.exceptions.JSONDecodeError(2978 "Extra data",2979 '{"responseCode":["706"],"data":null}{"responseCode":["706"],"data":null}',
4 unmodified lines