Skip to content
Myr

Merge pull request #2873 from sigmavirus24/bug/2872

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

Fix super_len for partially read files

authored by Cory Benfield · committed by Cory Benfield · parents 4e89ba7, 5397ef7 · Browse files · 2 files+24 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+24 added−9 removed

  • other:+24 added−9 removed

2 files+24 added−9 removed

modified:requests/utils.py+15 added-9 removed
46 unmodified lines
47 return d484950def super_len(o):51 total_length = 052 current_position = 05354 if hasattr(o, '__len__'):52 return len(o)55 total_length = len(o)5654 if hasattr(o, 'len'):55 return o.len57 elif hasattr(o, 'len'):58 total_length = o.len5960 elif hasattr(o, 'getvalue'):61 # e.g. BytesIO, cStringIO.StringIO62 total_length = len(o.getvalue())6357 if hasattr(o, 'fileno'):64 elif hasattr(o, 'fileno'):65 try:66 fileno = o.fileno()67 except io.UnsupportedOperation:68 pass69 else:63 filesize = os.fstat(fileno).st_size70 total_length = os.fstat(fileno).st_size7172 # Having used fstat to determine the file length, we need to73 # confirm that this file was opened up in binary mode.74 if 'b' not in o.mode:
6 unmodified lines
81 "for files in text mode."),82 FileModeWarning83 )8478 return filesize85 if hasattr(o, 'tell'):86 current_position = o.tell()8780 if hasattr(o, 'getvalue'):81 # e.g. BytesIO, cStringIO.StringIO82 return len(o.getvalue())88 return max(0, total_length - current_position)899091def get_netrc_auth(url, raise_errors=False):92 """Returns the Requests tuple auth for a given url from netrc."""
625 unmodified lines
modified:test_requests.py+9 added
1349 unmodified lines
1350 else:1351 assert super_len(1352 cStringIO.StringIO('but some how, some way...')) == 2513531354 def test_super_len_correctly_calculates_len_of_partially_read_file(self):1355 """Ensure that we handle partially consumed file like objects."""1356 from requests.utils import super_len1357 s = StringIO.StringIO()1358 s.write('foobarbogus')1359 assert super_len(s) == 013601361 def test_get_environ_proxies_ip_ranges(self):1362 """Ensures that IP addresses are correctly matches with ranges1363 in no_proxy variable."""1364 from requests.utils import get_environ_proxies
362 unmodified lines
1727 s.get(httpbin('status/200'))1728 except ConnectionError as e:1729 assert u"Pool is closed." in str(e)173017311732def test_vendor_aliases():1733 from requests.packages import urllib31734 from requests.packages import chardet17351736 with pytest.raises(ImportError):1737 from requests.packages import webbrowser173817391740if __name__ == '__main__':1741 unittest.main()