mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 14:50:16 +00:00
Merge pull request #3036 from Lukasa/issue/3035
Allow for exceptions from tell()
This commit is contained in:
@@ -9,6 +9,8 @@ Release History
|
||||
**Bugfixes**
|
||||
|
||||
- Don't use redirect_cache if allow_redirects=False
|
||||
- When passed objects that throw exceptions from ``tell()``, send them via
|
||||
chunked transfer encoding instead of failing.
|
||||
|
||||
2.9.1 (2015-12-21)
|
||||
++++++++++++++++++
|
||||
|
||||
+8
-1
@@ -83,7 +83,14 @@ def super_len(o):
|
||||
)
|
||||
|
||||
if hasattr(o, 'tell'):
|
||||
current_position = o.tell()
|
||||
try:
|
||||
current_position = o.tell()
|
||||
except (OSError, IOError):
|
||||
# This can happen in some weird situations, such as when the file
|
||||
# is actually a special file descriptor like stdin. In this
|
||||
# instance, we don't know what the length is, so set it to zero and
|
||||
# let requests chunk it instead.
|
||||
current_position = total_length
|
||||
|
||||
return max(0, total_length - current_position)
|
||||
|
||||
|
||||
+14
-1
@@ -15,7 +15,6 @@ from .compat import StringIO, cStringIO
|
||||
|
||||
|
||||
class TestSuperLen:
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'stream, value', (
|
||||
(StringIO.StringIO, 'Test'),
|
||||
@@ -33,6 +32,20 @@ class TestSuperLen:
|
||||
s.write('foobarbogus')
|
||||
assert super_len(s) == 0
|
||||
|
||||
@pytest.mark.parametrize('error', [IOError, OSError])
|
||||
def test_super_len_handles_files_raising_weird_errors_in_tell(self, error):
|
||||
"""
|
||||
If tell() raises errors, assume the cursor is at position zero.
|
||||
"""
|
||||
class BoomFile(object):
|
||||
def __len__(self):
|
||||
return 5
|
||||
|
||||
def tell(self):
|
||||
raise error()
|
||||
|
||||
assert super_len(BoomFile()) == 0
|
||||
|
||||
|
||||
class TestGetEnvironProxies:
|
||||
"""Ensures that IP addresses are correctly matches with ranges
|
||||
|
||||
Reference in New Issue
Block a user