summaryrefslogtreecommitdiffstats
path: root/Lib/encodings
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-11-10 21:49:20 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-11-10 21:49:20 (GMT)
commit9768676f6f1cc0c7b2efcf3258ef300c0417dbcb (patch)
treec2f1623a887c748970da8bab8d54046057dc2ac0 /Lib/encodings
parentde20b0b50e7b28a8f6eba8fb7e6f18063d5709b4 (diff)
downloadcpython-9768676f6f1cc0c7b2efcf3258ef300c0417dbcb.zip
cpython-9768676f6f1cc0c7b2efcf3258ef300c0417dbcb.tar.gz
cpython-9768676f6f1cc0c7b2efcf3258ef300c0417dbcb.tar.bz2
Speed up IDNA for the common case
Diffstat (limited to 'Lib/encodings')
-rw-r--r--Lib/encodings/idna.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/encodings/idna.py b/Lib/encodings/idna.py
index 583bdf1..ea40585 100644
--- a/Lib/encodings/idna.py
+++ b/Lib/encodings/idna.py
@@ -153,6 +153,20 @@ class Codec(codecs.Codec):
if not input:
return b'', 0
+ try:
+ result = input.encode('ascii')
+ except UnicodeEncodeError:
+ pass
+ else:
+ # ASCII name: fast path
+ labels = result.split(b'.')
+ for label in labels[:-1]:
+ if not (0 < len(label) < 64):
+ raise UnicodeError("label empty or too long")
+ if len(labels[-1]) >= 64:
+ raise UnicodeError("label too long")
+ return result, len(input)
+
result = bytearray()
labels = dots.split(input)
if labels and not labels[-1]:
@@ -179,6 +193,14 @@ class Codec(codecs.Codec):
if not isinstance(input, bytes):
# XXX obviously wrong, see #3232
input = bytes(input)
+
+ if ace_prefix not in input:
+ # Fast path
+ try:
+ return input.decode('ascii'), len(input)
+ except UnicodeDecodeError:
+ pass
+
labels = input.split(b".")
if labels and len(labels[-1]) == 0: