Skip to content
Myr

Merge pull request #3625 from mie00/tellioerror

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fix issue when the file-like object raises an IOError with tell

authored by Cory Benfield · committed by GitHub · parents b8f4c7c, f5b5c29 · Browse files · 2 files+21 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−9 removed

  • tests:+12 added−0 removed
  • other:+9 added−9 removed

2 files+21 added−9 removed

modified:requests/utils.py+9 added-9 removed
84 unmodified lines
85 # instance, we don't know what the length is, so set it to zero and86 # let requests chunk it instead.87 if total_length is not None:88 current_position = total_length89 else:90 if hasattr(o, 'seek') and total_length is None:91 # StringIO and BytesIO have seek but no useable fileno9290 if hasattr(o, 'seek') and total_length is None:91 # StringIO and BytesIO have seek but no useable fileno93 # seek to end of file94 o.seek(0, 2)95 total_length = o.tell()9693 # seek to end of file94 o.seek(0, 2)95 total_length = o.tell()97 # seek back to current position to support98 # partially read file-like objects99 o.seek(current_position or 0)10097 # seek back to current position to support98 # partially read file-like objects99 o.seek(current_position or 0)100101 if total_length is None:102 total_length = 0103104 return max(0, total_length - current_position)
709 unmodified lines
modified:tests/test_utils.py+12 added
49 unmodified lines
50 raise error()5152 assert super_len(BoomFile()) == 05354 @pytest.mark.parametrize('error', [IOError, OSError])55 def test_super_len_tell_ioerror(self, error):56 """Ensure that if tell gives an IOError super_len doesn't fail"""57 class NoLenBoomFile(object):58 def tell(self):59 raise error()6061 def seek(self, offset, whence):62 pass6364 assert super_len(NoLenBoomFile()) == 06566 def test_string(self):67 assert super_len('Test') == 46869 @pytest.mark.parametrize(
446 unmodified lines