Skip to content
Myr

Merge pull request #6589 from bruceadams/super_len_str_utf-8

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

Enhance `super_len` to count encoded bytes for str

authored by Ian Stapleton Cordasco · committed by GitHub · parents 3587a5f, 3fd309a · Browse files · 2 files+20 added−0 removed
This commit

State

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

Diff stats

2 files+20 added−0 removed

  • tests:+17 added−0 removed
  • other:+3 added−0 removed

2 files+20 added−0 removed

modified:src/requests/utils.py+3 added
132 unmodified lines
133def super_len(o):134 total_length = None135 current_position = 0136137 if isinstance(o, str):138 o = o.encode("utf-8")139140 if hasattr(o, "__len__"):141 total_length = len(o)142143 elif hasattr(o, "len"):
951 unmodified lines
modified:tests/test_requests.py+17 added
1806 unmodified lines
1807 p = req.prepare()18081809 assert p.headers["Content-Length"] == length18101811 def test_content_length_for_bytes_data(self, httpbin):1812 data = "This is a string containing multi-byte UTF-8 ☃️"1813 encoded_data = data.encode("utf-8")1814 length = str(len(encoded_data))1815 req = requests.Request("POST", httpbin("post"), data=encoded_data)1816 p = req.prepare()18171818 assert p.headers["Content-Length"] == length18191820 def test_content_length_for_string_data_counts_bytes(self, httpbin):1821 data = "This is a string containing multi-byte UTF-8 ☃️"1822 length = str(len(data.encode("utf-8")))1823 req = requests.Request("POST", httpbin("post"), data=data)1824 p = req.prepare()18251826 assert p.headers["Content-Length"] == length18271828 def test_nonhttp_schemes_dont_check_URLs(self):1829 test_urls = (1830 "data:image/gif;base64,R0lGODlhAQABAHAAACH5BAUAAAAALAAAAAABAAEAAAICRAEAOw==",1831 "file:///etc/passwd",
1008 unmodified lines