Skip to content

Commit d5b80eb

Browse files
bpo-42967: coerce bytes separator to string in urllib.parse_qs(l) (GH-24818) (#25345)
* 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]> Co-authored-by: Ken Jin <[email protected]>
1 parent 816da33 commit d5b80eb

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
@@ -891,6 +891,8 @@ def test_parse_qs_separator(self):
891891
with self.subTest(f"Original: {orig!r}, Expected: {expect!r}"):
892892
result = urllib.parse.parse_qs(orig, separator=';')
893893
self.assertEqual(result, expect, "Error parsing %r" % orig)
894+
result_bytes = urllib.parse.parse_qs(orig, separator=b';')
895+
self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)
894896

895897

896898
def test_parse_qsl_separator(self):
@@ -910,6 +912,8 @@ def test_parse_qsl_separator(self):
910912
with self.subTest(f"Original: {orig!r}, Expected: {expect!r}"):
911913
result = urllib.parse.parse_qsl(orig, separator=';')
912914
self.assertEqual(result, expect, "Error parsing %r" % orig)
915+
result_bytes = urllib.parse.parse_qsl(orig, separator=b';')
916+
self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)
913917

914918

915919
def test_urlencode_sequences(self):

Lib/urllib/parse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
721721
Returns a list, as G-d intended.
722722
"""
723723
qs, _coerce_result = _coerce_args(qs)
724+
separator, _ = _coerce_args(separator)
724725

725726
if not separator or (not isinstance(separator, (str, bytes))):
726727
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)