summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--requests/utils.py7
-rw-r--r--tests/test_utils.py15
2 files changed, 20 insertions, 2 deletions
diff --git a/requests/utils.py b/requests/utils.py
index c5c3fd01..c9746d64 100644
--- a/requests/utils.py
+++ b/requests/utils.py
@@ -83,7 +83,12 @@ 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.
+ current_position = 0
return max(0, total_length - current_position)
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 5a50e366..afb38315 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -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()) == 5
+
class TestGetEnvironProxies:
"""Ensures that IP addresses are correctly matches with ranges