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
Provenance
- Commit
4e38364- Tree
510fec9- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
Nate Prewitt <nate.prewitt@gmail.com>- Parent
f8aa36b
3 files changed+40 added−18 removed
modified:src/requests/compat.py+12 added
8 unmodified lines9 10 import importlib11 import sys12 13+# -------14+# urllib315+# -------16+from urllib3 import __version__ as urllib3_version17+18+# Detect which major version of urllib3 is being used.19+try:20+ is_urllib3_2 = int(urllib3_version.split(".")[0]) == 221+except (TypeError, AttributeError):22+ # If we can't discern a version, prefer old functionality.23+ is_urllib3_2 = False24+25 # -------------------26 # Character Detection27 # -------------------28 78 unmodified linesmodified:src/requests/utils.py+4 added-1 removed
36 unmodified lines37 bytes,38 getproxies,39 getproxies_environment,40 integer_types,41+ is_urllib3_2,42 )43 from .compat import parse_http_list as _parse_list_header44 from .compat import (45 proxy_bypass,90 unmodified lines136 def super_len(o):137 total_length = None138 current_position = 0139 139- 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")144 145 if hasattr(o, "__len__"):146 total_length = len(o)953 unmodified linesmodified:tests/test_requests.py+24 added-17 removed
23 unmodified lines24 MutableMapping,25 builtin_str,26 cookielib,27 getproxies,28+ is_urllib3_2,29 urlparse,30 )31 from requests.cookies import cookiejar_from_dict, morsel_to_cookie32 from requests.exceptions import (1777 unmodified lines1810 p = req.prepare()1811 1812 assert p.headers["Content-Length"] == length1813 1813- 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()1819-1820- assert p.headers["Content-Length"] == length1821-1822- 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()1827-1828- assert p.headers["Content-Length"] == length1829-1814 def test_nonhttp_schemes_dont_check_URLs(self):1815 test_urls = (1816 "data:image/gif;base64,R0lGODlhAQABAHAAACH5BAUAAAAALAAAAAABAAEAAAICRAEAOw==",1817 "file:///etc/passwd",1131 unmodified lines2949 2950 assert client_cert is not None2951 2952 2953+def 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()2959+2960+ assert p.headers["Content-Length"] == length2961+2962+2963+@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+)2967+def 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()2972+2973+ assert p.headers["Content-Length"] == length2974+2975+2976 def 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