Use seek from end rather than getvalue

This commit is contained in:
Skipper Seabold
2016-06-16 14:56:18 -05:00
committed by Nate Prewitt
parent f12166acb6
commit af7729f64a
+8 -5
View File
@@ -45,7 +45,7 @@ def dict_to_sequence(d):
def super_len(o):
total_length = 0
total_length = None
current_position = 0
if hasattr(o, '__len__'):
@@ -54,10 +54,6 @@ def super_len(o):
elif hasattr(o, 'len'):
total_length = o.len
elif hasattr(o, 'getvalue'):
# e.g. BytesIO, cStringIO.StringIO
total_length = len(o.getvalue())
elif hasattr(o, 'fileno'):
try:
fileno = o.fileno()
@@ -89,6 +85,13 @@ def super_len(o):
# let requests chunk it instead.
current_position = total_length
if hasattr(o, 'seek') and total_length is None:
# StringIO has a notimplemented fileno
# BytesIO has seek and not fileno
total_length = o.seek(0, 2)
# seek back to current position to support partially read file-like
o.seek(current_position)
return max(0, total_length - current_position)