From 47d0517d66e8cf5832768262221f0357ae134ad1 Mon Sep 17 00:00:00 2001 From: Josh Schneier Date: Tue, 29 Jul 2014 23:27:38 -0400 Subject: [PATCH] handle urllib3 api changes; closes #2045 --- requests/compat.py | 2 -- requests/models.py | 10 +++++----- test_requests.py | 12 ------------ 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/requests/compat.py b/requests/compat.py index 84d703b6..be5a1ed6 100644 --- a/requests/compat.py +++ b/requests/compat.py @@ -92,7 +92,6 @@ if is_py2: from Cookie import Morsel from StringIO import StringIO from .packages.urllib3.packages.ordered_dict import OrderedDict - from httplib import IncompleteRead builtin_str = str bytes = str @@ -108,7 +107,6 @@ elif is_py3: from http.cookies import Morsel from io import StringIO from collections import OrderedDict - from http.client import IncompleteRead builtin_str = str str = str diff --git a/requests/models.py b/requests/models.py index 2ababaf8..03ff627a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -9,7 +9,6 @@ This module contains the primary objects that power Requests. import collections import datetime -import socket from io import BytesIO, UnsupportedOperation from .hooks import default_hooks @@ -20,7 +19,8 @@ from .cookies import cookiejar_from_dict, get_cookie_header from .packages.urllib3.fields import RequestField from .packages.urllib3.filepost import encode_multipart_formdata from .packages.urllib3.util import parse_url -from .packages.urllib3.exceptions import DecodeError +from .packages.urllib3.exceptions import ( + DecodeError, ReadTimeoutError, ProtocolError) from .exceptions import ( HTTPError, RequestException, MissingSchema, InvalidURL, ChunkedEncodingError, ContentDecodingError, ConnectionError) @@ -30,7 +30,7 @@ from .utils import ( iter_slices, guess_json_utf, super_len, to_native_string) from .compat import ( cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO, - is_py2, chardet, json, builtin_str, basestring, IncompleteRead) + is_py2, chardet, json, builtin_str, basestring) from .status_codes import codes #: The set of HTTP status codes that indicate an automatically @@ -637,11 +637,11 @@ class Response(object): try: for chunk in self.raw.stream(chunk_size, decode_content=True): yield chunk - except IncompleteRead as e: + except ProtocolError as e: raise ChunkedEncodingError(e) except DecodeError as e: raise ContentDecodingError(e) - except socket.error as e: + except ReadTimeoutError as e: raise ConnectionError(e) except AttributeError: # Standard file-like object. diff --git a/test_requests.py b/test_requests.py index 694b779c..c9804517 100755 --- a/test_requests.py +++ b/test_requests.py @@ -720,18 +720,6 @@ class RequestsTestCase(unittest.TestCase): assert next(iter(r)) io.close() - def test_iter_content_handles_socket_error(self): - r = requests.Response() - import socket - - class RawMock(object): - def stream(self, chunk_size, decode_content=None): - raise socket.error() - - r.raw = RawMock() - with pytest.raises(ConnectionError): - list(r.iter_content()) - def test_response_decode_unicode(self): """ When called with decode_unicode, Response.iter_content should always