Skip to content

Commit af7729f

Browse files
Skipper Seaboldnateprewitt
Skipper Seabold
authored andcommitted
Use seek from end rather than getvalue
1 parent f12166a commit af7729f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

requests/utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def dict_to_sequence(d):
4545

4646

4747
def super_len(o):
48-
total_length = 0
48+
total_length = None
4949
current_position = 0
5050

5151
if hasattr(o, '__len__'):
@@ -54,10 +54,6 @@ def super_len(o):
5454
elif hasattr(o, 'len'):
5555
total_length = o.len
5656

57-
elif hasattr(o, 'getvalue'):
58-
# e.g. BytesIO, cStringIO.StringIO
59-
total_length = len(o.getvalue())
60-
6157
elif hasattr(o, 'fileno'):
6258
try:
6359
fileno = o.fileno()
@@ -89,6 +85,13 @@ def super_len(o):
8985
# let requests chunk it instead.
9086
current_position = total_length
9187

88+
if hasattr(o, 'seek') and total_length is None:
89+
# StringIO has a notimplemented fileno
90+
# BytesIO has seek and not fileno
91+
total_length = o.seek(0, 2)
92+
# seek back to current position to support partially read file-like
93+
o.seek(current_position)
94+
9295
return max(0, total_length - current_position)
9396

9497

0 commit comments

Comments
 (0)