Skip to content

Commit 1069a46

Browse files
gh-116764: Fix regressions in urllib.parse.parse_qsl() (GH-116801)
* Restore support of None and other false values. * Raise TypeError for non-zero integers and non-empty sequences. The regressions were introduced in gh-74668 (bdba8ef).
1 parent 269051d commit 1069a46

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

Lib/test/test_urlparse.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,30 @@ def test_parse_qsl_separator(self):
10721072
result_bytes = urllib.parse.parse_qsl(orig, separator=b';')
10731073
self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)
10741074

1075+
def test_parse_qsl_bytes(self):
1076+
self.assertEqual(urllib.parse.parse_qsl(b'a=b'), [(b'a', b'b')])
1077+
self.assertEqual(urllib.parse.parse_qsl(bytearray(b'a=b')), [(b'a', b'b')])
1078+
self.assertEqual(urllib.parse.parse_qsl(memoryview(b'a=b')), [(b'a', b'b')])
1079+
1080+
def test_parse_qsl_false_value(self):
1081+
kwargs = dict(keep_blank_values=True, strict_parsing=True)
1082+
for x in '', b'', None, 0, 0.0, [], {}, memoryview(b''):
1083+
self.assertEqual(urllib.parse.parse_qsl(x, **kwargs), [])
1084+
self.assertRaises(ValueError, urllib.parse.parse_qsl, x, separator=1)
1085+
1086+
def test_parse_qsl_errors(self):
1087+
self.assertRaises(TypeError, urllib.parse.parse_qsl, list(b'a=b'))
1088+
self.assertRaises(TypeError, urllib.parse.parse_qsl, iter(b'a=b'))
1089+
self.assertRaises(TypeError, urllib.parse.parse_qsl, 1)
1090+
self.assertRaises(TypeError, urllib.parse.parse_qsl, object())
1091+
1092+
for separator in '', b'', None, 0, 1, 0.0, 1.5:
1093+
with self.assertRaises(ValueError):
1094+
urllib.parse.parse_qsl('a=b', separator=separator)
1095+
with self.assertRaises(UnicodeEncodeError):
1096+
urllib.parse.parse_qsl(b'a=b', separator='\xa6')
1097+
with self.assertRaises(UnicodeDecodeError):
1098+
urllib.parse.parse_qsl('a=b', separator=b'\xa6')
10751099

10761100
def test_urlencode_sequences(self):
10771101
# Other tests incidentally urlencode things; test non-covered cases:

Lib/urllib/parse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,11 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
773773
def _unquote(s):
774774
return unquote_plus(s, encoding=encoding, errors=errors)
775775
else:
776-
qs = bytes(qs)
776+
if not qs:
777+
return []
778+
# Use memoryview() to reject integers and iterables,
779+
# acceptable by the bytes constructor.
780+
qs = bytes(memoryview(qs))
777781
if isinstance(separator, str):
778782
separator = bytes(separator, 'ascii')
779783
eq = b'='
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Restore support of ``None`` and other false values in :mod:`urllib.parse`
2+
functions :func:`~urllib.parse.parse_qs` and
3+
:func:`~urllib.parse.parse_qsl`. Also, they now raise a TypeError for
4+
non-zero integers and non-empty sequences.

0 commit comments

Comments
 (0)