Skip to content

Commit 232bca0

Browse files
Address review: extract _quote_ascii helper and simplify type checks
1 parent 53e2305 commit 232bca0

2 files changed

Lines changed: 20 additions & 64 deletions

File tree

‎Lib/test/test_urlparse.py‎

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,8 +1589,6 @@ def test_quote_errors(self):
15891589
self.assertRaises(TypeError, urllib.parse.quote, b'foo',
15901590
encoding='utf-8')
15911591
self.assertRaises(TypeError, urllib.parse.quote, b'foo', errors='strict')
1592-
self.assertRaises(TypeError, urllib.parse.quote, 'abc', errors=123)
1593-
self.assertRaises(TypeError, urllib.parse.quote_plus, 'abc', errors=123)
15941592

15951593
def test_quote_ascii_fastpath(self):
15961594
all_ascii = ''.join(chr(i) for i in range(128))
@@ -1654,55 +1652,17 @@ def test_quote_ascii_fastpath(self):
16541652
self.assertEqual(urllib.parse.quote_plus('a b/c', safe=b'/'), 'a+b/c')
16551653

16561654
def test_quote_str_subclass(self):
1657-
class PlainSubStr(str):
1655+
class SubStr(str):
16581656
pass
16591657

1660-
# Empty str subclass is returned as-is; non-empty returns plain str.
1661-
empty = PlainSubStr('')
1658+
empty = SubStr('')
16621659
self.assertIs(urllib.parse.quote(empty), empty)
16631660
self.assertIs(urllib.parse.quote_plus(empty), empty)
16641661

1665-
for s in (PlainSubStr('abc'), PlainSubStr('a b/c')):
1666-
res_q = urllib.parse.quote(s)
1667-
self.assertIs(type(res_q), str)
1668-
self.assertEqual(res_q, urllib.parse.quote(str(s)))
1669-
res_qp = urllib.parse.quote_plus(s)
1670-
self.assertIs(type(res_qp), str)
1671-
self.assertEqual(res_qp, urllib.parse.quote_plus(str(s)))
1672-
1673-
# str subclass overriding encode/rstrip/translate/isascii must bypass
1674-
# the fast path and use encode() as before.
1675-
class CustomEncodeStr(str):
1676-
def encode(self, encoding='utf-8', errors='strict'):
1677-
return b'custom'
1678-
def rstrip(self, chars=None):
1679-
raise AssertionError('rstrip should not be called on str subclass')
1680-
def translate(self, table):
1681-
raise AssertionError('translate should not be called on str subclass')
1682-
def isascii(self):
1683-
raise AssertionError('isascii should not be called on str subclass')
1684-
1685-
self.assertEqual(urllib.parse.quote(CustomEncodeStr('a b')), 'custom')
1686-
self.assertEqual(urllib.parse.quote_plus(CustomEncodeStr('a b')), 'custom')
1687-
1688-
class CustomSafeStr(str):
1689-
def encode(self, encoding='utf-8', errors='strict'):
1690-
return b'+ '
1691-
def __add__(self, other):
1692-
return CustomSafeStr(super().__add__(other))
1693-
1694-
self.assertEqual(
1695-
urllib.parse.quote('a+b/c', safe=CustomSafeStr('/')),
1696-
'a+b%2Fc',
1697-
)
1698-
self.assertEqual(
1699-
urllib.parse.quote_plus('a+b', safe=CustomSafeStr('')),
1700-
'a+b',
1701-
)
1702-
self.assertEqual(
1703-
urllib.parse.quote_plus('a+b c', safe=CustomSafeStr('')),
1704-
'a+b+c',
1705-
)
1662+
for s in (SubStr('abc'), SubStr('a b/c')):
1663+
self.assertEqual(urllib.parse.quote(s), urllib.parse.quote(str(s)))
1664+
self.assertEqual(urllib.parse.quote_plus(s),
1665+
urllib.parse.quote_plus(str(s)))
17061666

17071667
def test_issue14072(self):
17081668
p1 = urllib.parse.urlsplit('tel:+31-641044153')

‎Lib/urllib/parse.py‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ def unquote_plus(string, encoding='utf-8', errors='replace'):
10231023
'abcdefghijklmnopqrstuvwxyz'
10241024
'0123456789'
10251025
'_.-~')
1026-
_ASCII_ENCODINGS = frozenset(('utf-8', 'ascii', 'latin-1'))
1026+
_ASCII_ENCODINGS = (None, 'utf-8', 'ascii', 'latin-1')
10271027

10281028
@functools.lru_cache
10291029
def _str_quoter(safe, plus=False):
@@ -1036,6 +1036,15 @@ def _str_quoter(safe, plus=False):
10361036
safe_chars = ''.join(c for c in safe_set if ord(c) < 128)
10371037
return safe_chars, table
10381038

1039+
def _quote_ascii(string, safe, encoding, plus=False):
1040+
if (isinstance(string, str) and string.isascii() and
1041+
isinstance(safe, str) and encoding in _ASCII_ENCODINGS):
1042+
safe_chars, table = _str_quoter(safe, plus)
1043+
if not string.rstrip(safe_chars):
1044+
return string
1045+
return string.translate(table)
1046+
return None
1047+
10391048

10401049
class _Quoter(dict):
10411050
"""A mapping from bytes numbers (in range(0,256)) to strings.
@@ -1100,14 +1109,8 @@ def quote(string, safe='/', encoding=None, errors=None):
11001109
if isinstance(string, str):
11011110
if not string:
11021111
return string
1103-
if (type(string) is str and string.isascii() and type(safe) is str and
1104-
(encoding is None or
1105-
(type(encoding) is str and encoding in _ASCII_ENCODINGS)) and
1106-
(errors is None or isinstance(errors, str))):
1107-
safe_chars, table = _str_quoter(safe, False)
1108-
if not string.rstrip(safe_chars):
1109-
return string
1110-
return string.translate(table)
1112+
if (res := _quote_ascii(string, safe, encoding)) is not None:
1113+
return res
11111114
if encoding is None:
11121115
encoding = 'utf-8'
11131116
if errors is None:
@@ -1125,15 +1128,8 @@ def quote_plus(string, safe='', encoding=None, errors=None):
11251128
HTML form values. Plus signs in the original string are escaped unless
11261129
they are included in safe. It also does not have safe default to '/'.
11271130
"""
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):
1135-
return string
1136-
return string.translate(table)
1131+
if string and (res := _quote_ascii(string, safe, encoding, plus=True)) is not None:
1132+
return res
11371133
# Check if ' ' in string, where string may either be a str or bytes. If
11381134
# there are no spaces, the regular quote will produce the right answer.
11391135
if ((isinstance(string, str) and ' ' not in string) or

0 commit comments

Comments
 (0)