Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ def super_len(o):
# of latin-1 (iso-8859-1) like http.client.
o = o.encode("utf-8")

if isinstance(o, io.StringIO):
total_length = len(o.getvalue().encode("utf-8"))
current_position = len(o.getvalue()[: o.tell()].encode("utf-8"))
return max(0, total_length - current_position)

if hasattr(o, "__len__"):
total_length = len(o)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ def test_super_len_with_tell(self):
foo.read(2)
assert super_len(foo) == 3

def test_super_len_stringio_multibyte(self):
# UTF-8: "👍" is 4 bytes
data = "👍"
s = StringIO.StringIO(data)
assert super_len(s) == 4

# Test partial read
data_mixed = "abc👍"
s = StringIO.StringIO(data_mixed)
s.read(3)
assert super_len(s) == 4

def test_super_len_with_fileno(self):
with open(__file__, "rb") as f:
length = super_len(f)
Expand Down