Catch possible exceptions while consuming content of redirect responses.
authorschlamar <marc.schlaich@gmail.com>
Mon, 10 Mar 2014 06:54:22 +0000 (07:54 +0100)
committerschlamar <marc.schlaich@gmail.com>
Mon, 12 May 2014 20:42:53 +0000 (22:42 +0200)
requests/sessions.py

index 6b21b5df646583d01f0ce8fdb79ce4c32a48409e..fe8d7e2a3b4e06bdc953a4935a0d90e70d88a0ae 100644 (file)
@@ -19,7 +19,8 @@ from .cookies import (
 from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT
 from .hooks import default_hooks, dispatch_hook
 from .utils import to_key_val_list, default_headers, to_native_string
-from .exceptions import TooManyRedirects, InvalidSchema
+from .exceptions import (
+    TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)
 from .structures import CaseInsensitiveDict
 
 from .adapters import HTTPAdapter
@@ -94,7 +95,10 @@ class SessionRedirectMixin(object):
         while resp.is_redirect:
             prepared_request = req.copy()
 
-            resp.content  # Consume socket so it can be released
+            try:
+                resp.content  # Consume socket so it can be released
+            except (ChunkedEncodingError, ContentDecodingError, RuntimeError):
+                resp.raw.read(decode_content=False)
 
             if i >= self.max_redirects:
                 raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)