Skip to content

Commit 91115ae

Browse files
committed
Speed up urllib.parse.unquote() for ASCII input
1 parent 1c327a4 commit 91115ae

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎Lib/test/test_urllib.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,20 @@ def test_unquote_with_unicode(self):
12571257
self.assertEqual(expect, result,
12581258
"using unquote(): %r != %r" % (expect, result))
12591259

1260+
def test_unquote_ascii_and_non_ascii_agree(self):
1261+
# ASCII input takes a fast path; a non-ASCII prefix forces the
1262+
# general path. Both must decode the escapes the same way.
1263+
for given in ['%41%zz%', 'a%20b%2Fc%E2%82%AC', '%F3%B1', '%C3%BC+%2B']:
1264+
for encoding, errors in [('utf-8', 'replace'),
1265+
('latin-1', 'strict'),
1266+
('ascii', 'ignore')]:
1267+
with self.subTest(given=given, encoding=encoding,
1268+
errors=errors):
1269+
expect = urllib.parse.unquote(given, encoding, errors)
1270+
result = urllib.parse.unquote('\u00e9' + given,
1271+
encoding, errors)
1272+
self.assertEqual(result, '\u00e9' + expect)
1273+
12601274
def test_unquoting_with_bytes_input(self):
12611275
# ASCII characters decoded to a string
12621276
given = b'blueberryjam'

‎Lib/urllib/parse.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,8 @@ def unquote(string, encoding='utf-8', errors='replace'):
853853
encoding = 'utf-8'
854854
if errors is None:
855855
errors = 'replace'
856+
if string.isascii():
857+
return _unquote_impl(string).decode(encoding, errors)
856858
return ''.join(_generate_unquoted_parts(string, encoding, errors))
857859

858860

0 commit comments

Comments
 (0)