From 7e422821b9f800f5345c37011c510dc9e76f552c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 7 Dec 2018 11:43:38 +0800 Subject: remove idna as a primary dependency (#4624) * remove idna as a primary dependency * empty commit * dynamodb test fix (thanks to Matt Bullock) * review feedback --- src/cryptography/x509/general_name.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/cryptography/x509/general_name.py b/src/cryptography/x509/general_name.py index 42082746..1b0f8c8f 100644 --- a/src/cryptography/x509/general_name.py +++ b/src/cryptography/x509/general_name.py @@ -30,6 +30,20 @@ _GENERAL_NAMES = { } +def _lazy_import_idna(): + # Import idna lazily becase it allocates a decent amount of memory, and + # we're only using it in deprecated paths. + try: + import idna + return idna + except ImportError: + raise ImportError( + "idna is not installed, but a deprecated feature that requires it" + " was used. See: https://cryptography.io/en/latest/faq/#importe" + "rror-idna-is-not-installed" + ) + + class UnsupportedGeneralNameType(Exception): def __init__(self, msg, type): super(UnsupportedGeneralNameType, self).__init__(msg) @@ -81,10 +95,7 @@ class RFC822Name(object): return instance def _idna_encode(self, value): - # Import idna lazily becase it allocates a decent amoutn of memory, and - # we're only using it in deprecated paths. - import idna - + idna = _lazy_import_idna() _, address = parseaddr(value) parts = address.split(u"@") return parts[0] + "@" + idna.encode(parts[1]).decode("ascii") @@ -106,10 +117,7 @@ class RFC822Name(object): def _idna_encode(value): - # Import idna lazily becase it allocates a decent amoutn of memory, and - # we're only using it in deprecated paths. - import idna - + idna = _lazy_import_idna() # Retain prefixes '*.' for common/alt names and '.' for name constraints for prefix in ['*.', '.']: if value.startswith(prefix): @@ -193,10 +201,7 @@ class UniformResourceIdentifier(object): return instance def _idna_encode(self, value): - # Import idna lazily becase it allocates a decent amoutn of memory, and - # we're only using it in deprecated paths. - import idna - + idna = _lazy_import_idna() parsed = urllib_parse.urlparse(value) if parsed.port: netloc = ( -- cgit v1.2.3