Skip to content

Commit 53e2305

Browse files
Simplify ASCII fast path: drop lazy globals, use single lru_cache helper
1 parent bcce85c commit 53e2305

2 files changed

Lines changed: 23 additions & 68 deletions

File tree

‎Lib/test/test_urlparse.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,12 +1829,9 @@ def test_clear_cache_for_code_coverage(self):
18291829
urllib.parse.quote('a b/c', safe=':')
18301830
urllib.parse.quote_plus('a b/c')
18311831
urllib.parse.quote_plus('a b/c', safe=':')
1832-
self.assertGreater(urllib.parse._str_quoter_table.cache_info().currsize, 0)
1832+
self.assertGreater(urllib.parse._str_quoter.cache_info().currsize, 0)
18331833
urllib.parse.clear_cache()
1834-
self.assertEqual(urllib.parse._str_quoter_table.cache_info().currsize, 0)
1835-
self.assertIsNone(urllib.parse._STR_QUOTER_SLASH)
1836-
self.assertIsNone(urllib.parse._STR_QUOTER_EMPTY)
1837-
self.assertIsNone(urllib.parse._STR_QUOTER_PLUS_EMPTY)
1834+
self.assertEqual(urllib.parse._str_quoter.cache_info().currsize, 0)
18381835
# Re-populating after clear_cache works identically
18391836
self.assertEqual(urllib.parse.quote('a b/c'), 'a%20b/c')
18401837
self.assertEqual(urllib.parse.quote_plus('a b/c'), 'a+b%2Fc')

‎Lib/urllib/parse.py‎

Lines changed: 21 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,9 @@
9393

9494
def clear_cache():
9595
"""Clear internal performance caches. Undocumented; some tests want it."""
96-
global _STR_QUOTER_SLASH, _STR_QUOTER_EMPTY, _STR_QUOTER_PLUS_EMPTY
9796
urlsplit.cache_clear()
9897
_byte_quoter_factory.cache_clear()
99-
_str_quoter_table.cache_clear()
100-
_STR_QUOTER_SLASH = None
101-
_STR_QUOTER_EMPTY = None
102-
_STR_QUOTER_PLUS_EMPTY = None
98+
_str_quoter.cache_clear()
10399

104100
# Helpers for bytes handling
105101
# We deliberately require applications that
@@ -1027,23 +1023,18 @@ def unquote_plus(string, encoding='utf-8', errors='replace'):
10271023
'abcdefghijklmnopqrstuvwxyz'
10281024
'0123456789'
10291025
'_.-~')
1030-
_ALWAYS_SAFE_SLASH_STR = _ALWAYS_SAFE_STR + '/'
10311026
_ASCII_ENCODINGS = frozenset(('utf-8', 'ascii', 'latin-1'))
10321027

1033-
def _make_str_quoter_table(safe, plus=False):
1028+
@functools.lru_cache
1029+
def _str_quoter(safe, plus=False):
10341030
safe_set = set(_ALWAYS_SAFE_STR) | set(safe)
1031+
if plus:
1032+
safe_set.discard(' ')
10351033
table = {b: f'%{b:02X}' for b in range(128) if chr(b) not in safe_set}
10361034
if plus:
10371035
table[32] = '+'
1038-
return table
1039-
1040-
_STR_QUOTER_SLASH = None
1041-
_STR_QUOTER_EMPTY = None
1042-
_STR_QUOTER_PLUS_EMPTY = None
1043-
1044-
@functools.lru_cache
1045-
def _str_quoter_table(safe, plus=False):
1046-
return _make_str_quoter_table(safe, plus)
1036+
safe_chars = ''.join(c for c in safe_set if ord(c) < 128)
1037+
return safe_chars, table
10471038

10481039

10491040
class _Quoter(dict):
@@ -1106,35 +1097,17 @@ def quote(string, safe='/', encoding=None, errors=None):
11061097
By default, encoding='utf-8' (characters are encoded with UTF-8), and
11071098
errors='strict' (unsupported characters raise a UnicodeEncodeError).
11081099
"""
1109-
global _STR_QUOTER_SLASH, _STR_QUOTER_EMPTY
1110-
if type(string) is str:
1100+
if isinstance(string, str):
11111101
if not string:
11121102
return string
1113-
if (string.isascii() and type(safe) is str and
1103+
if (type(string) is str and string.isascii() and type(safe) is str and
11141104
(encoding is None or
11151105
(type(encoding) is str and encoding in _ASCII_ENCODINGS)) and
11161106
(errors is None or isinstance(errors, str))):
1117-
if safe == '/':
1118-
if not string.rstrip(_ALWAYS_SAFE_SLASH_STR):
1119-
return string
1120-
if (table := _STR_QUOTER_SLASH) is None:
1121-
table = _STR_QUOTER_SLASH = _make_str_quoter_table('/', False)
1122-
return string.translate(table)
1123-
if safe == '':
1124-
if not string.rstrip(_ALWAYS_SAFE_STR):
1125-
return string
1126-
if (table := _STR_QUOTER_EMPTY) is None:
1127-
table = _STR_QUOTER_EMPTY = _make_str_quoter_table('', False)
1128-
return string.translate(table)
1129-
return string.translate(_str_quoter_table(safe, False))
1130-
if encoding is None:
1131-
encoding = 'utf-8'
1132-
if errors is None:
1133-
errors = 'strict'
1134-
string = string.encode(encoding, errors)
1135-
elif isinstance(string, str):
1136-
if not string:
1137-
return string
1107+
safe_chars, table = _str_quoter(safe, False)
1108+
if not string.rstrip(safe_chars):
1109+
return string
1110+
return string.translate(table)
11381111
if encoding is None:
11391112
encoding = 'utf-8'
11401113
if errors is None:
@@ -1152,30 +1125,15 @@ def quote_plus(string, safe='', encoding=None, errors=None):
11521125
HTML form values. Plus signs in the original string are escaped unless
11531126
they are included in safe. It also does not have safe default to '/'.
11541127
"""
1155-
global _STR_QUOTER_PLUS_EMPTY
1156-
if type(string) is str:
1157-
if not string:
1128+
if (type(string) is str and string and string.isascii() and
1129+
type(safe) is str and
1130+
(encoding is None or
1131+
(type(encoding) is str and encoding in _ASCII_ENCODINGS)) and
1132+
(errors is None or isinstance(errors, str))):
1133+
safe_chars, table = _str_quoter(safe, True)
1134+
if not string.rstrip(safe_chars):
11581135
return string
1159-
if (string.isascii() and type(safe) is str and
1160-
(encoding is None or
1161-
(type(encoding) is str and encoding in _ASCII_ENCODINGS)) and
1162-
(errors is None or isinstance(errors, str))):
1163-
if safe == '':
1164-
if not string.rstrip(_ALWAYS_SAFE_STR):
1165-
return string
1166-
if (table := _STR_QUOTER_PLUS_EMPTY) is None:
1167-
table = _STR_QUOTER_PLUS_EMPTY = _make_str_quoter_table('', True)
1168-
return string.translate(table)
1169-
return string.translate(_str_quoter_table(safe, True))
1170-
has_space = ' ' in string
1171-
if has_space:
1172-
safe = safe + (' ' if isinstance(safe, str) else b' ')
1173-
if encoding is None:
1174-
encoding = 'utf-8'
1175-
if errors is None:
1176-
errors = 'strict'
1177-
res = quote_from_bytes(string.encode(encoding, errors), safe)
1178-
return res.replace(' ', '+') if has_space else res
1136+
return string.translate(table)
11791137
# Check if ' ' in string, where string may either be a str or bytes. If
11801138
# there are no spaces, the regular quote will produce the right answer.
11811139
if ((isinstance(string, str) and ' ' not in string) or

0 commit comments

Comments
 (0)