From f47aff68f1352c3a5030fe70bbdf7c00171e207b Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Tue, 14 Feb 2017 07:45:41 -0700 Subject: [PATCH] properly handled failed seek --- requests/utils.py | 16 +++++++++------- tests/test_requests.py | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/requests/utils.py b/requests/utils.py index e5ecd350..6365034c 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -92,14 +92,16 @@ def super_len(o): else: if hasattr(o, 'seek') and total_length is None: # StringIO and BytesIO have seek but no useable fileno + try: + # 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) + except (OSError, IOError): + total_length = 0 if total_length is None: total_length = 0 diff --git a/tests/test_requests.py b/tests/test_requests.py index cd4c68db..26d4951e 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1546,7 +1546,7 @@ class TestRequests: def tell(self): return 0 - def seek(self, pos): + def seek(self, pos, whence=0): raise OSError() def __iter__(self):