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
Provenance
- Commit
fae3f92- Tree
fc82230- Author
Moin <moin18@gmail.com>- Committer
Moin <moin18@gmail.com>- Parent
580b45d
3 files changed+28 added−17 removed
modified:requests/packages/__init__.py-6 removed
32 unmodified lines33 from . import chardet34 except ImportError:35 import chardet36 sys.modules['%s.chardet' % __name__] = chardet37-38-try:39- from . import idna40-except ImportError:41- import idna42- sys.modules['%s.idna' % __name__] = idnamodified:requests/models.py+27 added-11 removed
7 unmodified lines8 """9 10 import collections11 import datetime12+import sys13 14 # 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 lines21 from .structures import CaseInsensitiveDict22 23 from .auth import HTTPBasicAuth24 from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar24-from .packages import idna25 from .packages.urllib3.fields import RequestField26 from .packages.urllib3.filepost import encode_multipart_formdata27 from .packages.urllib3.util import parse_url28 from .packages.urllib3.exceptions import (301 unmodified lines330 self.method = method331 if self.method is not None:332 self.method = to_native_string(self.method.upper())333 334+ @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'] = idna343+344+ try:345+ host = idna.encode(host, uts46=True).decode('utf-8')346+ except idna.IDNAError:347+ raise UnicodeError348+ return host349+350 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 functions29 unmodified lines383 384 if not host:385 raise InvalidURL("Invalid URL %r: No host supplied" % url)386 371- # 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.')398 399 # Carefully reconstruct the network location400 netloc = auth or ''401 if netloc:521 unmodified linesmodified:AUTHORS.rst+1 added
173 unmodified lines174 - 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>`_)