mirror of
https://github.com/django/django.git
synced 2025-07-04 01:39:20 +00:00
Move idna import to module level
This commit is contained in:
parent
85b066c9f2
commit
8f9165bfed
@ -6,6 +6,12 @@ from urllib.parse import quote
|
||||
|
||||
from django.utils.functional import Promise
|
||||
|
||||
try:
|
||||
# try importing idna package for IDNA 2008 compliance
|
||||
import idna
|
||||
except ImportError:
|
||||
idna = None
|
||||
|
||||
|
||||
class DjangoUnicodeDecodeError(UnicodeDecodeError):
|
||||
def __init__(self, obj, *args):
|
||||
@ -213,18 +219,16 @@ def punycode(domain):
|
||||
"""Return the Punycode of the given domain if it's non-ASCII."""
|
||||
if not domain:
|
||||
return domain
|
||||
if idna:
|
||||
try:
|
||||
# try importing idna package for IDNA 2008 compliance
|
||||
import idna
|
||||
|
||||
# enable uts46 mapping for mapping: uppercase letters, normalization, etc.
|
||||
return idna.encode(domain, uts46=True, transitional=False).decode("ascii")
|
||||
except ImportError:
|
||||
# will fall back to Python IDNA 2003
|
||||
return domain.encode("idna").decode("ascii")
|
||||
except idna.IDNAError as e:
|
||||
raise UnicodeError(e) from e
|
||||
|
||||
# will fall back to Python IDNA 2003
|
||||
return domain.encode("idna").decode("ascii")
|
||||
|
||||
|
||||
def repercent_broken_unicode(path):
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user