Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-116764: Fix regressions in urllib.parse.parse_qsl() #116801

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,30 @@ def test_parse_qsl_separator(self):
result_bytes = urllib.parse.parse_qsl(orig, separator=b';')
self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)

def test_parse_qsl_bytes(self):
self.assertEqual(urllib.parse.parse_qsl(b'a=b'), [(b'a', b'b')])
self.assertEqual(urllib.parse.parse_qsl(bytearray(b'a=b')), [(b'a', b'b')])
self.assertEqual(urllib.parse.parse_qsl(memoryview(b'a=b')), [(b'a', b'b')])

def test_parse_qsl_false_value(self):
kwargs = dict(keep_blank_values=True, strict_parsing=True)
for x in '', b'', None, 0, 0.0, [], {}, memoryview(b''):
self.assertEqual(urllib.parse.parse_qsl(x, **kwargs), [])
self.assertRaises(ValueError, urllib.parse.parse_qsl, x, separator=1)

def test_parse_qsl_errors(self):
self.assertRaises(TypeError, urllib.parse.parse_qsl, list(b'a=b'))
self.assertRaises(TypeError, urllib.parse.parse_qsl, iter(b'a=b'))
self.assertRaises(TypeError, urllib.parse.parse_qsl, 1)
self.assertRaises(TypeError, urllib.parse.parse_qsl, object())

for separator in '', b'', None, 0, 1, 0.0, 1.5:
with self.assertRaises(ValueError):
urllib.parse.parse_qsl('a=b', separator=separator)
with self.assertRaises(UnicodeEncodeError):
urllib.parse.parse_qsl(b'a=b', separator='\xa6')
with self.assertRaises(UnicodeDecodeError):
urllib.parse.parse_qsl('a=b', separator=b'\xa6')

def test_urlencode_sequences(self):
# Other tests incidentally urlencode things; test non-covered cases:
Expand Down
6 changes: 5 additions & 1 deletion Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,11 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
def _unquote(s):
return unquote_plus(s, encoding=encoding, errors=errors)
else:
qs = bytes(qs)
if not qs:
return []
# Use memoryview() to reject integers and iterables,
# acceptable by the bytes constructor.
qs = bytes(memoryview(qs))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth having a comment why going through memoryview first is needed (to disallow stuff like bytes(10)?), or is that trick idiomatic in standard library code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not so common. Added a comment.

if isinstance(separator, str):
separator = bytes(separator, 'ascii')
eq = b'='
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Restore support of ``None`` and other false values in :mod:`urllib.parse`
functions :func:`~urllib.parse.parse_qs` and
:func:`~urllib.parse.parse_qsl`. Also, they now raise a TypeError for
non-zero integers and non-empty sequences.
Loading