def total_len(o):
# Stolen from requests_toolbelt and modified
if hasattr(o, '__len__'):
return len(o)
if hasattr(o, 'len'):
return o.len
if hasattr(o, 'fileno'):
try:
fileno = o.fileno()
except io.UnsupportedOperation:
pass
else:
return os.fstat(fileno).st_size
if hasattr(o, 'getvalue'):
# e.g. BytesIO, cStringIO.StringIO
return len(o.getvalue())
try:
current_pos = o.tell()
length = o.seek(0, 2)
o.seek(current_pos, 0)
return length
except IOError:
pass
评论列表
文章目录