Skip to content
Myr

lazily load idna library

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

authored by Moin · committed by Moin · parent 580b45d · Browse files · 3 files+28 added−17 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+28 added−17 removed

  • other:+28 added−17 removed

3 files+28 added−17 removed

modified:requests/packages/__init__.py-6 removed
32 unmodified lines
33 from . import chardet34except ImportError:35 import chardet36 sys.modules['%s.chardet' % __name__] = chardet3738try:39 from . import idna40except ImportError:41 import idna42 sys.modules['%s.idna' % __name__] = idna
modified:requests/models.py+27 added-11 removed
7 unmodified lines
8"""910import collections11import datetime12import sys1314# Import encoding now, to avoid implicit import later.15# Implicit import within threads may cause LookupError when standard library is in a ZIP,16# such as in Embedded Python. See https://github.com/kennethreitz/requests/issues/3578.
4 unmodified lines
21from .structures import CaseInsensitiveDict2223from .auth import HTTPBasicAuth24from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar24from .packages import idna25from .packages.urllib3.fields import RequestField26from .packages.urllib3.filepost import encode_multipart_formdata27from .packages.urllib3.util import parse_url28from .packages.urllib3.exceptions import (
301 unmodified lines
330 self.method = method331 if self.method is not None:332 self.method = to_native_string(self.method.upper())333334 @staticmethod335 def _get_idna_encoded_host(host):336 try:337 from .packages import idna338 except ImportError:339 # tolerate the possibility of downstream repackagers unvendoring `requests`340 # For more information, read: packages/__init__.py341 import idna342 sys.modules['requests.packages.idna'] = idna343344 try:345 host = idna.encode(host, uts46=True).decode('utf-8')346 except idna.IDNAError:347 raise UnicodeError348 return host349350 def prepare_url(self, url, params):351 """Prepares the given HTTP URL."""352 #: Accept objects that have string representations.353 #: We're unable to blindly call unicode/str functions
29 unmodified lines
383384 if not host:385 raise InvalidURL("Invalid URL %r: No host supplied" % url)386371 # In general, we want to try IDNA encoding every hostname, as that372 # allows users to automatically get the correct behaviour. However,373 # we’re quite strict about IDNA encoding, so certain valid hostnames374 # may fail to encode. On failure, we verify the hostname meets a375 # minimum standard of only containing ASCII characters, and not starting376 # with a wildcard (*), before allowing the unencoded hostname through.377 try:378 host = idna.encode(host, uts46=True).decode('utf-8')379 except (UnicodeError, idna.IDNAError):380 if not unicode_is_ascii(host) or host.startswith(u'*'):387 # In general, we want to try IDNA encoding the hostname if the string contains388 # non-ASCII characters. This allows users to automatically get the correct IDNA389 # behaviour. For strings containing only ASCII characters, we need to also verify390 # it doesn't start with a wildcard (*), before allowing the unencoded hostname.391 if not unicode_is_ascii(host):392 try:393 host = self._get_idna_encoded_host(host)394 except UnicodeError:395 raise InvalidURL('URL has an invalid label.')396 elif host.startswith(u'*'):397 raise InvalidURL('URL has an invalid label.')398399 # Carefully reconstruct the network location400 netloc = auth or ''401 if netloc:
521 unmodified lines
modified:AUTHORS.rst+1 added
173 unmodified lines
174- Philipp Konrad <gardiac2002@gmail.com> (`@gardiac2002 <https://github.com/gardiac2002>`_)175- Hussain Tamboli <hussaintamboli18@gmail.com> (`@hussaintamboli <https://github.com/hussaintamboli>`_)176- Casey Davidson (`@davidsoncasey <https://github.com/davidsoncasey>`_)177- Andrii Soldatenko (`@a_soldatenko <https://github.com/andriisoldatenko>`_)178- Moinuddin Quadri <moin18@gmail.com> (`@moin18 <https://github.com/moin18>`_)