Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9768676

Browse files
committed
Speed up IDNA for the common case
1 parent de20b0b commit 9768676

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lib/encodings/idna.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ def encode(self, input, errors='strict'):
153153
if not input:
154154
return b'', 0
155155

156+
try:
157+
result = input.encode('ascii')
158+
except UnicodeEncodeError:
159+
pass
160+
else:
161+
# ASCII name: fast path
162+
labels = result.split(b'.')
163+
for label in labels[:-1]:
164+
if not (0 < len(label) < 64):
165+
raise UnicodeError("label empty or too long")
166+
if len(labels[-1]) >= 64:
167+
raise UnicodeError("label too long")
168+
return result, len(input)
169+
156170
result = bytearray()
157171
labels = dots.split(input)
158172
if labels and not labels[-1]:
@@ -179,6 +193,14 @@ def decode(self, input, errors='strict'):
179193
if not isinstance(input, bytes):
180194
# XXX obviously wrong, see #3232
181195
input = bytes(input)
196+
197+
if ace_prefix not in input:
198+
# Fast path
199+
try:
200+
return input.decode('ascii'), len(input)
201+
except UnicodeDecodeError:
202+
pass
203+
182204
labels = input.split(b".")
183205

184206
if labels and len(labels[-1]) == 0:

0 commit comments

Comments
 (0)