mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Merge pull request #3625 from mie00/tellioerror
fix issue when the file-like object raises an IOError with tell
This commit is contained in:
+9
-9
@@ -86,17 +86,17 @@ def super_len(o):
|
||||
# let requests chunk it instead.
|
||||
if total_length is not None:
|
||||
current_position = total_length
|
||||
else:
|
||||
if hasattr(o, 'seek') and total_length is None:
|
||||
# StringIO and BytesIO have seek but no useable fileno
|
||||
|
||||
if hasattr(o, 'seek') and total_length is None:
|
||||
# StringIO and BytesIO have seek but no useable fileno
|
||||
# seek to end of file
|
||||
o.seek(0, 2)
|
||||
total_length = o.tell()
|
||||
|
||||
# seek to end of file
|
||||
o.seek(0, 2)
|
||||
total_length = o.tell()
|
||||
|
||||
# seek back to current position to support
|
||||
# partially read file-like objects
|
||||
o.seek(current_position or 0)
|
||||
# seek back to current position to support
|
||||
# partially read file-like objects
|
||||
o.seek(current_position or 0)
|
||||
|
||||
if total_length is None:
|
||||
total_length = 0
|
||||
|
||||
@@ -51,6 +51,18 @@ class TestSuperLen:
|
||||
|
||||
assert super_len(BoomFile()) == 0
|
||||
|
||||
@pytest.mark.parametrize('error', [IOError, OSError])
|
||||
def test_super_len_tell_ioerror(self, error):
|
||||
"""Ensure that if tell gives an IOError super_len doesn't fail"""
|
||||
class NoLenBoomFile(object):
|
||||
def tell(self):
|
||||
raise error()
|
||||
|
||||
def seek(self, offset, whence):
|
||||
pass
|
||||
|
||||
assert super_len(NoLenBoomFile()) == 0
|
||||
|
||||
def test_string(self):
|
||||
assert super_len('Test') == 4
|
||||
|
||||
|
||||
Reference in New Issue
Block a user