Skip to content
Myr

Wrap urllib3's SSLError as requests' SSLError (#6057)

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

authored by Nate Prewitt · committed by GitHub · parent 7ae3887 · Browse files · 3 files+55 added−5 removed
This commit

State

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

Diff stats

3 files+55 added−5 removed

  • tests:+41 added−4 removed
  • other:+14 added−1 removed

3 files+55 added−5 removed

modified:requests/models.py+9 added-1 removed
17 unmodified lines
18from urllib3.fields import RequestField19from urllib3.filepost import encode_multipart_formdata20from urllib3.util import parse_url21from urllib3.exceptions import (22 DecodeError, ReadTimeoutError, ProtocolError, LocationParseError)22 DecodeError,23 LocationParseError,24 ProtocolError,25 ReadTimeoutError,26 SSLError,27)2829from io import UnsupportedOperation30from .hooks import default_hooks31from .structures import CaseInsensitiveDict
4 unmodified lines
36 HTTPError, MissingSchema, InvalidURL, ChunkedEncodingError,37 ContentDecodingError, ConnectionError, StreamConsumedError,38 InvalidJSONError)39from .exceptions import JSONDecodeError as RequestsJSONDecodeError40from .exceptions import SSLError as RequestsSSLError41from ._internal_utils import to_native_string, unicode_is_ascii42from .utils import (43 guess_filename, get_auth_from_url, requote_uri,44 stream_decode_response_unicode, to_key_val_list, parse_header_links,
725 unmodified lines
770 except DecodeError as e:771 raise ContentDecodingError(e)772 except ReadTimeoutError as e:773 raise ConnectionError(e)774 except SSLError as e:775 raise RequestsSSLError(e)776 else:777 # Standard file-like object.778 while True:779 chunk = self.raw.read(chunk_size)
202 unmodified lines
modified:tests/test_requests.py+41 added-4 removed
12 unmodified lines
1314import io15import requests16import pytest17import urllib318from requests.adapters import HTTPAdapter19from requests.auth import HTTPDigestAuth, _basic_auth_str20from requests.compat import (21 Morsel, cookielib, getproxies, str, urlparse,22 builtin_str)23from requests.cookies import (24 cookiejar_from_dict, morsel_to_cookie)25from requests.exceptions import (25 ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL,26 MissingSchema, ReadTimeout, Timeout, RetryError, RequestException, TooManyRedirects,27 ProxyError, InvalidHeader, UnrewindableBodyError, SSLError, InvalidProxyURL, InvalidJSONError)26 ChunkedEncodingError,27 ConnectionError,28 ConnectTimeout,29 ContentDecodingError,30 InvalidHeader,31 InvalidJSONError,32 InvalidProxyURL,33 InvalidSchema,34 InvalidURL,35 MissingSchema,36 ProxyError,37 ReadTimeout,38 RequestException,39 RetryError,40 Timeout,41 TooManyRedirects,42 UnrewindableBodyError,43)44from requests.exceptions import SSLError as RequestsSSLError45from requests.models import PreparedRequest46from requests.structures import CaseInsensitiveDict47from requests.sessions import SessionRedirectMixin48from requests.models import urlencode
877 unmodified lines
926 def test_certificate_failure(self, httpbin_secure):927 """928 When underlying SSL problems occur, an SSLError is raised.929 """913 with pytest.raises(SSLError):930 with pytest.raises(RequestsSSLError):931 # Our local httpbin does not have a trusted CA, so this call will932 # fail if we use our default trust bundle.933 requests.get(httpbin_secure('status', '200'))934
401 unmodified lines
1336 r.raw = io.BytesIO(b'the content')1337 with pytest.raises(TypeError):1338 chunks = r.iter_content("1024")13391340 @pytest.mark.parametrize(1341 'exception, args, expected', (1342 (urllib3.exceptions.ProtocolError, tuple(), ChunkedEncodingError),1343 (urllib3.exceptions.DecodeError, tuple(), ContentDecodingError),1344 (urllib3.exceptions.ReadTimeoutError, (None, '', ''), ConnectionError),1345 (urllib3.exceptions.SSLError, tuple(), RequestsSSLError),1346 )1347 )1348 def test_iter_content_wraps_exceptions(1349 self, httpbin, mocker, exception, args, expected1350 ):1351 r = requests.Response()1352 r.raw = mocker.Mock()1353 # ReadTimeoutError can't be initialized by mock1354 # so we'll manually create the instance with args1355 r.raw.stream.side_effect = exception(*args)13561357 with pytest.raises(expected):1358 next(r.iter_content(1024))13591360 def test_request_and_response_are_pickleable(self, httpbin):1361 r = requests.get(httpbin('get'))13621363 # verify we can pickle the original request
1273 unmodified lines
modified:HISTORY.md+5 added
4 unmodified lines
5---67- \[Short description of non-trivial change.\]89**Bugfixes**1011- Fixed urllib3 exception leak, wrapping `urllib3.exceptions.SSLError` with12 `requests.exceptions.SSLError` for `content` and `iter_content`.13142.27.1 (2022-01-05)15-------------------1617**Bugfixes**
1795 unmodified lines