Skip to content
Myr

Merge pull request #2148 from romanlevin/handle-socket-error-in-iter_content

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Handle socket errors in iter_content

authored by Kenneth Reitz · committed by Kenneth Reitz · parents fe693c4, cf01a74 · Browse files · 2 files+17 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+17 added−2 removed

  • other:+17 added−2 removed

2 files+17 added−2 removed

modified:requests/models.py+4 added-1 removed
7 unmodified lines
8"""910import collections11import datetime12import socket1314from io import BytesIO, UnsupportedOperation15from .hooks import default_hooks16from .structures import CaseInsensitiveDict
5 unmodified lines
22from .packages.urllib3.util import parse_url23from .packages.urllib3.exceptions import DecodeError24from .exceptions import (25 HTTPError, RequestException, MissingSchema, InvalidURL,25 ChunkedEncodingError, ContentDecodingError)26 ChunkedEncodingError, ContentDecodingError, ConnectionError)27from .utils import (28 guess_filename, get_auth_from_url, requote_uri,29 stream_decode_response_unicode, to_key_val_list, parse_header_links,30 iter_slices, guess_json_utf, super_len, to_native_string)
609 unmodified lines
640 except IncompleteRead as e:641 raise ChunkedEncodingError(e)642 except DecodeError as e:643 raise ContentDecodingError(e)644 except socket.error as e:645 raise ConnectionError(e)646 except AttributeError:647 # Standard file-like object.648 while True:649 chunk = self.raw.read(chunk_size)
167 unmodified lines
modified:test_requests.py+13 added-1 removed
16 unmodified lines
17from requests.auth import HTTPDigestAuth, _basic_auth_str18from requests.compat import (19 Morsel, cookielib, getproxies, str, urljoin, urlparse, is_py3, builtin_str)20from requests.cookies import cookiejar_from_dict, morsel_to_cookie21from requests.exceptions import InvalidURL, MissingSchema21from requests.exceptions import InvalidURL, MissingSchema, ConnectionError22from requests.models import PreparedRequest23from requests.structures import CaseInsensitiveDict24from requests.sessions import SessionRedirectMixin25from requests.models import urlencode
693 unmodified lines
719 r.raw = io720 assert next(iter(r))721 io.close()722723 def test_iter_content_handles_socket_error(self):724 r = requests.Response()725 import socket726727 class RawMock(object):728 def stream(self, chunk_size, decode_content=None):729 raise socket.error()730731 r.raw = RawMock()732 with pytest.raises(ConnectionError):733 list(r.iter_content())734735 def test_response_decode_unicode(self):736 """737 When called with decode_unicode, Response.iter_content should always738 return unicode.
699 unmodified lines