From bcce85c71bb4a51639785c282374ebd0b3995c1e Mon Sep 17 00:00:00 2001 From: gateway12 Date: Thu, 3 Dec 2026 15:07:55 -0600 Subject: [PATCH 1/3] gh-163115: Speed up urllib.parse.quote() and quote_plus() for ASCII str inputs Bypass the bytes encoding and map(quoter, bs) loop for exact ASCII str inputs using str.rstrip() and str.translate(). Initialize default translation tables lazily, cache custom safe tables via lru_cache, and clear all of them in clear_cache(). --- Lib/test/test_urlparse.py | 127 ++++++++++++++++++ Lib/urllib/parse.py | 80 ++++++++++- ...-12-03-21-55-54.gh-issue-163115.idXJuQ.rst | 2 + 3 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-12-03-21-55-54.gh-issue-163115.idXJuQ.rst diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index 37e172f7f2caca..1d19326ccae0e8 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1589,6 +1589,120 @@ def test_quote_errors(self): self.assertRaises(TypeError, urllib.parse.quote, b'foo', encoding='utf-8') self.assertRaises(TypeError, urllib.parse.quote, b'foo', errors='strict') + self.assertRaises(TypeError, urllib.parse.quote, 'abc', errors=123) + self.assertRaises(TypeError, urllib.parse.quote_plus, 'abc', errors=123) + + def test_quote_ascii_fastpath(self): + all_ascii = ''.join(chr(i) for i in range(128)) + all_ascii_bytes = bytes(range(128)) + + # Verify quote() and quote_plus() match quote_from_bytes() across all + # 128 ASCII characters and custom safe values. + for safe in ('/', '', ' ', '+', ' /?=&', '~_-.'): + expected_quote = urllib.parse.quote_from_bytes(all_ascii_bytes, safe) + self.assertEqual(urllib.parse.quote(all_ascii, safe=safe), + expected_quote) + + expected_plus = urllib.parse.quote_from_bytes( + all_ascii_bytes, safe + ' ' + ).replace(' ', '+') + self.assertEqual(urllib.parse.quote_plus(all_ascii, safe=safe), + expected_plus) + + # Every individual ASCII char as safe + for i in range(128): + ch = chr(i) + expected = urllib.parse.quote_from_bytes(all_ascii_bytes, ch) + self.assertEqual(urllib.parse.quote(all_ascii, safe=ch), expected) + expected_plus = urllib.parse.quote_from_bytes( + all_ascii_bytes, ch + ' ' + ).replace(' ', '+') + self.assertEqual(urllib.parse.quote_plus(all_ascii, safe=ch), + expected_plus) + + # safe=' ' in quote_plus still encodes spaces as '+' + self.assertEqual(urllib.parse.quote_plus('a b+c', safe=' '), 'a+b%2Bc') + self.assertEqual(urllib.parse.quote_plus('a b+c', safe='+'), 'a+b+c') + + # Non-ASCII safe characters are ignored + self.assertEqual(urllib.parse.quote('a/b c', safe='é/'), 'a/b%20c') + self.assertEqual(urllib.parse.quote_plus('a/b c', safe='é/'), 'a/b+c') + + # Non-ASCII string fallback + self.assertEqual(urllib.parse.quote('café /a'), 'caf%C3%A9%20/a') + self.assertEqual(urllib.parse.quote_plus('café /a'), 'caf%C3%A9+%2Fa') + + # Encodings: ASCII-compatible and non-ASCII-compatible + for enc in (None, 'utf-8', 'ascii', 'latin-1'): + self.assertEqual(urllib.parse.quote('a b/c', encoding=enc), + 'a%20b/c') + self.assertEqual(urllib.parse.quote_plus('a b/c', encoding=enc), + 'a+b%2Fc') + self.assertEqual( + urllib.parse.quote('abc', encoding='utf-16'), + urllib.parse.quote_from_bytes('abc'.encode('utf-16'), '/'), + ) + self.assertEqual( + urllib.parse.quote_plus('a b', encoding='utf-16'), + urllib.parse.quote_from_bytes('a b'.encode('utf-16'), ' ').replace(' ', '+'), + ) + + # bytes inputs and bytes safe + self.assertEqual(urllib.parse.quote(b'a b/c'), 'a%20b/c') + self.assertEqual(urllib.parse.quote('a b/c', safe=b'/'), 'a%20b/c') + self.assertEqual(urllib.parse.quote_plus(b'a b/c'), 'a+b%2Fc') + self.assertEqual(urllib.parse.quote_plus('a b/c', safe=b'/'), 'a+b/c') + + def test_quote_str_subclass(self): + class PlainSubStr(str): + pass + + # Empty str subclass is returned as-is; non-empty returns plain str. + empty = PlainSubStr('') + self.assertIs(urllib.parse.quote(empty), empty) + self.assertIs(urllib.parse.quote_plus(empty), empty) + + for s in (PlainSubStr('abc'), PlainSubStr('a b/c')): + res_q = urllib.parse.quote(s) + self.assertIs(type(res_q), str) + self.assertEqual(res_q, urllib.parse.quote(str(s))) + res_qp = urllib.parse.quote_plus(s) + self.assertIs(type(res_qp), str) + self.assertEqual(res_qp, urllib.parse.quote_plus(str(s))) + + # str subclass overriding encode/rstrip/translate/isascii must bypass + # the fast path and use encode() as before. + class CustomEncodeStr(str): + def encode(self, encoding='utf-8', errors='strict'): + return b'custom' + def rstrip(self, chars=None): + raise AssertionError('rstrip should not be called on str subclass') + def translate(self, table): + raise AssertionError('translate should not be called on str subclass') + def isascii(self): + raise AssertionError('isascii should not be called on str subclass') + + self.assertEqual(urllib.parse.quote(CustomEncodeStr('a b')), 'custom') + self.assertEqual(urllib.parse.quote_plus(CustomEncodeStr('a b')), 'custom') + + class CustomSafeStr(str): + def encode(self, encoding='utf-8', errors='strict'): + return b'+ ' + def __add__(self, other): + return CustomSafeStr(super().__add__(other)) + + self.assertEqual( + urllib.parse.quote('a+b/c', safe=CustomSafeStr('/')), + 'a+b%2Fc', + ) + self.assertEqual( + urllib.parse.quote_plus('a+b', safe=CustomSafeStr('')), + 'a+b', + ) + self.assertEqual( + urllib.parse.quote_plus('a+b c', safe=CustomSafeStr('')), + 'a+b+c', + ) def test_issue14072(self): p1 = urllib.parse.urlsplit('tel:+31-641044153') @@ -1710,7 +1824,20 @@ def test_Quoter_repr(self): self.assertIn('Quoter', repr(quoter)) def test_clear_cache_for_code_coverage(self): + urllib.parse.quote('a b/c') + urllib.parse.quote('a b/c', safe='') + urllib.parse.quote('a b/c', safe=':') + urllib.parse.quote_plus('a b/c') + urllib.parse.quote_plus('a b/c', safe=':') + self.assertGreater(urllib.parse._str_quoter_table.cache_info().currsize, 0) urllib.parse.clear_cache() + self.assertEqual(urllib.parse._str_quoter_table.cache_info().currsize, 0) + self.assertIsNone(urllib.parse._STR_QUOTER_SLASH) + self.assertIsNone(urllib.parse._STR_QUOTER_EMPTY) + self.assertIsNone(urllib.parse._STR_QUOTER_PLUS_EMPTY) + # Re-populating after clear_cache works identically + self.assertEqual(urllib.parse.quote('a b/c'), 'a%20b/c') + self.assertEqual(urllib.parse.quote_plus('a b/c'), 'a+b%2Fc') def test_urllib_parse_getattr_failure(self): """Test that urllib.parse.__getattr__() fails correctly.""" diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index f6c94060054e48..9937ea28daccd3 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -93,8 +93,13 @@ def clear_cache(): """Clear internal performance caches. Undocumented; some tests want it.""" + global _STR_QUOTER_SLASH, _STR_QUOTER_EMPTY, _STR_QUOTER_PLUS_EMPTY urlsplit.cache_clear() _byte_quoter_factory.cache_clear() + _str_quoter_table.cache_clear() + _STR_QUOTER_SLASH = None + _STR_QUOTER_EMPTY = None + _STR_QUOTER_PLUS_EMPTY = None # Helpers for bytes handling # We deliberately require applications that @@ -1018,6 +1023,29 @@ def unquote_plus(string, encoding='utf-8', errors='replace'): _ALWAYS_SAFE_BYTES = bytes(_ALWAYS_SAFE) +_ALWAYS_SAFE_STR = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789' + '_.-~') +_ALWAYS_SAFE_SLASH_STR = _ALWAYS_SAFE_STR + '/' +_ASCII_ENCODINGS = frozenset(('utf-8', 'ascii', 'latin-1')) + +def _make_str_quoter_table(safe, plus=False): + safe_set = set(_ALWAYS_SAFE_STR) | set(safe) + table = {b: f'%{b:02X}' for b in range(128) if chr(b) not in safe_set} + if plus: + table[32] = '+' + return table + +_STR_QUOTER_SLASH = None +_STR_QUOTER_EMPTY = None +_STR_QUOTER_PLUS_EMPTY = None + +@functools.lru_cache +def _str_quoter_table(safe, plus=False): + return _make_str_quoter_table(safe, plus) + + class _Quoter(dict): """A mapping from bytes numbers (in range(0,256)) to strings. @@ -1078,7 +1106,33 @@ def quote(string, safe='/', encoding=None, errors=None): By default, encoding='utf-8' (characters are encoded with UTF-8), and errors='strict' (unsupported characters raise a UnicodeEncodeError). """ - if isinstance(string, str): + global _STR_QUOTER_SLASH, _STR_QUOTER_EMPTY + if type(string) is str: + if not string: + return string + if (string.isascii() and type(safe) is str and + (encoding is None or + (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and + (errors is None or isinstance(errors, str))): + if safe == '/': + if not string.rstrip(_ALWAYS_SAFE_SLASH_STR): + return string + if (table := _STR_QUOTER_SLASH) is None: + table = _STR_QUOTER_SLASH = _make_str_quoter_table('/', False) + return string.translate(table) + if safe == '': + if not string.rstrip(_ALWAYS_SAFE_STR): + return string + if (table := _STR_QUOTER_EMPTY) is None: + table = _STR_QUOTER_EMPTY = _make_str_quoter_table('', False) + return string.translate(table) + return string.translate(_str_quoter_table(safe, False)) + if encoding is None: + encoding = 'utf-8' + if errors is None: + errors = 'strict' + string = string.encode(encoding, errors) + elif isinstance(string, str): if not string: return string if encoding is None: @@ -1098,6 +1152,30 @@ def quote_plus(string, safe='', encoding=None, errors=None): HTML form values. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'. """ + global _STR_QUOTER_PLUS_EMPTY + if type(string) is str: + if not string: + return string + if (string.isascii() and type(safe) is str and + (encoding is None or + (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and + (errors is None or isinstance(errors, str))): + if safe == '': + if not string.rstrip(_ALWAYS_SAFE_STR): + return string + if (table := _STR_QUOTER_PLUS_EMPTY) is None: + table = _STR_QUOTER_PLUS_EMPTY = _make_str_quoter_table('', True) + return string.translate(table) + return string.translate(_str_quoter_table(safe, True)) + has_space = ' ' in string + if has_space: + safe = safe + (' ' if isinstance(safe, str) else b' ') + if encoding is None: + encoding = 'utf-8' + if errors is None: + errors = 'strict' + res = quote_from_bytes(string.encode(encoding, errors), safe) + return res.replace(' ', '+') if has_space else res # Check if ' ' in string, where string may either be a str or bytes. If # there are no spaces, the regular quote will produce the right answer. if ((isinstance(string, str) and ' ' not in string) or diff --git a/Misc/NEWS.d/next/Library/2026-12-03-21-55-54.gh-issue-163115.idXJuQ.rst b/Misc/NEWS.d/next/Library/2026-12-03-21-55-54.gh-issue-163115.idXJuQ.rst new file mode 100644 index 00000000000000..82d15f1ecea446 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-03-21-55-54.gh-issue-163115.idXJuQ.rst @@ -0,0 +1,2 @@ +Speed up :func:`urllib.parse.quote`, :func:`urllib.parse.quote_plus`, and +:func:`urllib.parse.urlencode` for ASCII :class:`str` inputs. From 53e2305584c58cca79fbd0b818ad956ff4da5bcf Mon Sep 17 00:00:00 2001 From: gateway12 Date: Thu, 3 Dec 2026 17:06:58 -0600 Subject: [PATCH 2/3] Simplify ASCII fast path: drop lazy globals, use single lru_cache helper --- Lib/test/test_urlparse.py | 7 +--- Lib/urllib/parse.py | 84 ++++++++++----------------------------- 2 files changed, 23 insertions(+), 68 deletions(-) diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index 1d19326ccae0e8..5e099322f6bcaf 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1829,12 +1829,9 @@ def test_clear_cache_for_code_coverage(self): urllib.parse.quote('a b/c', safe=':') urllib.parse.quote_plus('a b/c') urllib.parse.quote_plus('a b/c', safe=':') - self.assertGreater(urllib.parse._str_quoter_table.cache_info().currsize, 0) + self.assertGreater(urllib.parse._str_quoter.cache_info().currsize, 0) urllib.parse.clear_cache() - self.assertEqual(urllib.parse._str_quoter_table.cache_info().currsize, 0) - self.assertIsNone(urllib.parse._STR_QUOTER_SLASH) - self.assertIsNone(urllib.parse._STR_QUOTER_EMPTY) - self.assertIsNone(urllib.parse._STR_QUOTER_PLUS_EMPTY) + self.assertEqual(urllib.parse._str_quoter.cache_info().currsize, 0) # Re-populating after clear_cache works identically self.assertEqual(urllib.parse.quote('a b/c'), 'a%20b/c') self.assertEqual(urllib.parse.quote_plus('a b/c'), 'a+b%2Fc') diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 9937ea28daccd3..3c381d00b042c1 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -93,13 +93,9 @@ def clear_cache(): """Clear internal performance caches. Undocumented; some tests want it.""" - global _STR_QUOTER_SLASH, _STR_QUOTER_EMPTY, _STR_QUOTER_PLUS_EMPTY urlsplit.cache_clear() _byte_quoter_factory.cache_clear() - _str_quoter_table.cache_clear() - _STR_QUOTER_SLASH = None - _STR_QUOTER_EMPTY = None - _STR_QUOTER_PLUS_EMPTY = None + _str_quoter.cache_clear() # Helpers for bytes handling # We deliberately require applications that @@ -1027,23 +1023,18 @@ def unquote_plus(string, encoding='utf-8', errors='replace'): 'abcdefghijklmnopqrstuvwxyz' '0123456789' '_.-~') -_ALWAYS_SAFE_SLASH_STR = _ALWAYS_SAFE_STR + '/' _ASCII_ENCODINGS = frozenset(('utf-8', 'ascii', 'latin-1')) -def _make_str_quoter_table(safe, plus=False): +@functools.lru_cache +def _str_quoter(safe, plus=False): safe_set = set(_ALWAYS_SAFE_STR) | set(safe) + if plus: + safe_set.discard(' ') table = {b: f'%{b:02X}' for b in range(128) if chr(b) not in safe_set} if plus: table[32] = '+' - return table - -_STR_QUOTER_SLASH = None -_STR_QUOTER_EMPTY = None -_STR_QUOTER_PLUS_EMPTY = None - -@functools.lru_cache -def _str_quoter_table(safe, plus=False): - return _make_str_quoter_table(safe, plus) + safe_chars = ''.join(c for c in safe_set if ord(c) < 128) + return safe_chars, table class _Quoter(dict): @@ -1106,35 +1097,17 @@ def quote(string, safe='/', encoding=None, errors=None): By default, encoding='utf-8' (characters are encoded with UTF-8), and errors='strict' (unsupported characters raise a UnicodeEncodeError). """ - global _STR_QUOTER_SLASH, _STR_QUOTER_EMPTY - if type(string) is str: + if isinstance(string, str): if not string: return string - if (string.isascii() and type(safe) is str and + if (type(string) is str and string.isascii() and type(safe) is str and (encoding is None or (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and (errors is None or isinstance(errors, str))): - if safe == '/': - if not string.rstrip(_ALWAYS_SAFE_SLASH_STR): - return string - if (table := _STR_QUOTER_SLASH) is None: - table = _STR_QUOTER_SLASH = _make_str_quoter_table('/', False) - return string.translate(table) - if safe == '': - if not string.rstrip(_ALWAYS_SAFE_STR): - return string - if (table := _STR_QUOTER_EMPTY) is None: - table = _STR_QUOTER_EMPTY = _make_str_quoter_table('', False) - return string.translate(table) - return string.translate(_str_quoter_table(safe, False)) - if encoding is None: - encoding = 'utf-8' - if errors is None: - errors = 'strict' - string = string.encode(encoding, errors) - elif isinstance(string, str): - if not string: - return string + safe_chars, table = _str_quoter(safe, False) + if not string.rstrip(safe_chars): + return string + return string.translate(table) if encoding is None: encoding = 'utf-8' if errors is None: @@ -1152,30 +1125,15 @@ def quote_plus(string, safe='', encoding=None, errors=None): HTML form values. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'. """ - global _STR_QUOTER_PLUS_EMPTY - if type(string) is str: - if not string: + if (type(string) is str and string and string.isascii() and + type(safe) is str and + (encoding is None or + (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and + (errors is None or isinstance(errors, str))): + safe_chars, table = _str_quoter(safe, True) + if not string.rstrip(safe_chars): return string - if (string.isascii() and type(safe) is str and - (encoding is None or - (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and - (errors is None or isinstance(errors, str))): - if safe == '': - if not string.rstrip(_ALWAYS_SAFE_STR): - return string - if (table := _STR_QUOTER_PLUS_EMPTY) is None: - table = _STR_QUOTER_PLUS_EMPTY = _make_str_quoter_table('', True) - return string.translate(table) - return string.translate(_str_quoter_table(safe, True)) - has_space = ' ' in string - if has_space: - safe = safe + (' ' if isinstance(safe, str) else b' ') - if encoding is None: - encoding = 'utf-8' - if errors is None: - errors = 'strict' - res = quote_from_bytes(string.encode(encoding, errors), safe) - return res.replace(' ', '+') if has_space else res + return string.translate(table) # Check if ' ' in string, where string may either be a str or bytes. If # there are no spaces, the regular quote will produce the right answer. if ((isinstance(string, str) and ' ' not in string) or From 232bca0ad87b1118b8d3ea52f907e428b74c3ee0 Mon Sep 17 00:00:00 2001 From: gateway12 Date: Thu, 3 Dec 2026 17:33:10 -0600 Subject: [PATCH 3/3] Address review: extract _quote_ascii helper and simplify type checks --- Lib/test/test_urlparse.py | 52 +++++---------------------------------- Lib/urllib/parse.py | 32 +++++++++++------------- 2 files changed, 20 insertions(+), 64 deletions(-) diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index 5e099322f6bcaf..af820c34447a59 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1589,8 +1589,6 @@ def test_quote_errors(self): self.assertRaises(TypeError, urllib.parse.quote, b'foo', encoding='utf-8') self.assertRaises(TypeError, urllib.parse.quote, b'foo', errors='strict') - self.assertRaises(TypeError, urllib.parse.quote, 'abc', errors=123) - self.assertRaises(TypeError, urllib.parse.quote_plus, 'abc', errors=123) def test_quote_ascii_fastpath(self): all_ascii = ''.join(chr(i) for i in range(128)) @@ -1654,55 +1652,17 @@ def test_quote_ascii_fastpath(self): self.assertEqual(urllib.parse.quote_plus('a b/c', safe=b'/'), 'a+b/c') def test_quote_str_subclass(self): - class PlainSubStr(str): + class SubStr(str): pass - # Empty str subclass is returned as-is; non-empty returns plain str. - empty = PlainSubStr('') + empty = SubStr('') self.assertIs(urllib.parse.quote(empty), empty) self.assertIs(urllib.parse.quote_plus(empty), empty) - for s in (PlainSubStr('abc'), PlainSubStr('a b/c')): - res_q = urllib.parse.quote(s) - self.assertIs(type(res_q), str) - self.assertEqual(res_q, urllib.parse.quote(str(s))) - res_qp = urllib.parse.quote_plus(s) - self.assertIs(type(res_qp), str) - self.assertEqual(res_qp, urllib.parse.quote_plus(str(s))) - - # str subclass overriding encode/rstrip/translate/isascii must bypass - # the fast path and use encode() as before. - class CustomEncodeStr(str): - def encode(self, encoding='utf-8', errors='strict'): - return b'custom' - def rstrip(self, chars=None): - raise AssertionError('rstrip should not be called on str subclass') - def translate(self, table): - raise AssertionError('translate should not be called on str subclass') - def isascii(self): - raise AssertionError('isascii should not be called on str subclass') - - self.assertEqual(urllib.parse.quote(CustomEncodeStr('a b')), 'custom') - self.assertEqual(urllib.parse.quote_plus(CustomEncodeStr('a b')), 'custom') - - class CustomSafeStr(str): - def encode(self, encoding='utf-8', errors='strict'): - return b'+ ' - def __add__(self, other): - return CustomSafeStr(super().__add__(other)) - - self.assertEqual( - urllib.parse.quote('a+b/c', safe=CustomSafeStr('/')), - 'a+b%2Fc', - ) - self.assertEqual( - urllib.parse.quote_plus('a+b', safe=CustomSafeStr('')), - 'a+b', - ) - self.assertEqual( - urllib.parse.quote_plus('a+b c', safe=CustomSafeStr('')), - 'a+b+c', - ) + for s in (SubStr('abc'), SubStr('a b/c')): + self.assertEqual(urllib.parse.quote(s), urllib.parse.quote(str(s))) + self.assertEqual(urllib.parse.quote_plus(s), + urllib.parse.quote_plus(str(s))) def test_issue14072(self): p1 = urllib.parse.urlsplit('tel:+31-641044153') diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 3c381d00b042c1..55b136c92d9f37 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -1023,7 +1023,7 @@ def unquote_plus(string, encoding='utf-8', errors='replace'): 'abcdefghijklmnopqrstuvwxyz' '0123456789' '_.-~') -_ASCII_ENCODINGS = frozenset(('utf-8', 'ascii', 'latin-1')) +_ASCII_ENCODINGS = (None, 'utf-8', 'ascii', 'latin-1') @functools.lru_cache def _str_quoter(safe, plus=False): @@ -1036,6 +1036,15 @@ def _str_quoter(safe, plus=False): safe_chars = ''.join(c for c in safe_set if ord(c) < 128) return safe_chars, table +def _quote_ascii(string, safe, encoding, plus=False): + if (isinstance(string, str) and string.isascii() and + isinstance(safe, str) and encoding in _ASCII_ENCODINGS): + safe_chars, table = _str_quoter(safe, plus) + if not string.rstrip(safe_chars): + return string + return string.translate(table) + return None + class _Quoter(dict): """A mapping from bytes numbers (in range(0,256)) to strings. @@ -1100,14 +1109,8 @@ def quote(string, safe='/', encoding=None, errors=None): if isinstance(string, str): if not string: return string - if (type(string) is str and string.isascii() and type(safe) is str and - (encoding is None or - (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and - (errors is None or isinstance(errors, str))): - safe_chars, table = _str_quoter(safe, False) - if not string.rstrip(safe_chars): - return string - return string.translate(table) + if (res := _quote_ascii(string, safe, encoding)) is not None: + return res if encoding is None: encoding = 'utf-8' if errors is None: @@ -1125,15 +1128,8 @@ def quote_plus(string, safe='', encoding=None, errors=None): HTML form values. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'. """ - if (type(string) is str and string and string.isascii() and - type(safe) is str and - (encoding is None or - (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and - (errors is None or isinstance(errors, str))): - safe_chars, table = _str_quoter(safe, True) - if not string.rstrip(safe_chars): - return string - return string.translate(table) + if string and (res := _quote_ascii(string, safe, encoding, plus=True)) is not None: + return res # Check if ' ' in string, where string may either be a str or bytes. If # there are no spaces, the regular quote will produce the right answer. if ((isinstance(string, str) and ' ' not in string) or