Skip to content

Commit 6ec2fb4

Browse files
bpo-42967: coerce bytes separator to string in urllib.parse_qs(l) (GH-24818)
* coerce bytes separator to string * Add news * Update Misc/NEWS.d/next/Library/2021-03-11-00-31-41.bpo-42967.2PeQRw.rst (cherry picked from commit b38601d) Co-authored-by: Ken Jin <[email protected]>
1 parent dbc0e19 commit 6ec2fb4

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

Lib/test/test_urlparse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,8 @@ def test_parse_qs_separator(self):
893893
with self.subTest(f"Original: {orig!r}, Expected: {expect!r}"):
894894
result = urllib.parse.parse_qs(orig, separator=';')
895895
self.assertEqual(result, expect, "Error parsing %r" % orig)
896+
result_bytes = urllib.parse.parse_qs(orig, separator=b';')
897+
self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)
896898

897899

898900
def test_parse_qsl_separator(self):
@@ -912,6 +914,8 @@ def test_parse_qsl_separator(self):
912914
with self.subTest(f"Original: {orig!r}, Expected: {expect!r}"):
913915
result = urllib.parse.parse_qsl(orig, separator=';')
914916
self.assertEqual(result, expect, "Error parsing %r" % orig)
917+
result_bytes = urllib.parse.parse_qsl(orig, separator=b';')
918+
self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)
915919

916920

917921
def test_urlencode_sequences(self):

Lib/urllib/parse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
733733
Returns a list, as G-d intended.
734734
"""
735735
qs, _coerce_result = _coerce_args(qs)
736+
separator, _ = _coerce_args(separator)
736737

737738
if not separator or (not isinstance(separator, (str, bytes))):
738739
raise ValueError("Separator must be of type string or bytes.")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Allow :class:`bytes` ``separator`` argument in ``urllib.parse.parse_qs`` and
2+
``urllib.parse.parse_qsl`` when parsing :class:`str` query strings. Previously,
3+
this raised a ``TypeError``.

0 commit comments

Comments
 (0)