[Python-checkins] bpo-42967: coerce bytes separator to string in urllib.parse_qs(l) (GH-24818) (#25345)

orsenthil webhook-mailer at python.org
Fri Apr 16 13:07:49 EDT 2021


https://github.com/python/cpython/commit/d5b80eb11b4812b4a579ce129ba4a10c5f5d27f6
commit: d5b80eb11b4812b4a579ce129ba4a10c5f5d27f6
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: orsenthil <skumaran at gatech.edu>
date: 2021-04-16T10:07:31-07:00
summary:

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 b38601d49675d90e1ee6faa47f7adaeca992d02d)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner at users.noreply.github.com>

Co-authored-by: Ken Jin <28750310+Fidget-Spinner at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2021-03-11-00-31-41.bpo-42967.2PeQRw.rst
M Lib/test/test_urlparse.py
M Lib/urllib/parse.py

diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
index 90c8d6922629e..d2ec0dadbcb07 100644
--- a/Lib/test/test_urlparse.py
+++ b/Lib/test/test_urlparse.py
@@ -891,6 +891,8 @@ def test_parse_qs_separator(self):
             with self.subTest(f"Original: {orig!r}, Expected: {expect!r}"):
                 result = urllib.parse.parse_qs(orig, separator=';')
                 self.assertEqual(result, expect, "Error parsing %r" % orig)
+                result_bytes = urllib.parse.parse_qs(orig, separator=b';')
+                self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)
 
 
     def test_parse_qsl_separator(self):
@@ -910,6 +912,8 @@ def test_parse_qsl_separator(self):
             with self.subTest(f"Original: {orig!r}, Expected: {expect!r}"):
                 result = urllib.parse.parse_qsl(orig, separator=';')
                 self.assertEqual(result, expect, "Error parsing %r" % orig)
+                result_bytes = urllib.parse.parse_qsl(orig, separator=b';')
+                self.assertEqual(result_bytes, expect, "Error parsing %r" % orig)
 
 
     def test_urlencode_sequences(self):
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
index 0c1c94f5fc986..36fd8fe2803e2 100644
--- a/Lib/urllib/parse.py
+++ b/Lib/urllib/parse.py
@@ -721,6 +721,7 @@ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
         Returns a list, as G-d intended.
     """
     qs, _coerce_result = _coerce_args(qs)
+    separator, _ = _coerce_args(separator)
 
     if not separator or (not isinstance(separator, (str, bytes))):
         raise ValueError("Separator must be of type string or bytes.")
diff --git a/Misc/NEWS.d/next/Library/2021-03-11-00-31-41.bpo-42967.2PeQRw.rst b/Misc/NEWS.d/next/Library/2021-03-11-00-31-41.bpo-42967.2PeQRw.rst
new file mode 100644
index 0000000000000..f8ad3eaaedd78
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-03-11-00-31-41.bpo-42967.2PeQRw.rst
@@ -0,0 +1,3 @@
+Allow :class:`bytes` ``separator`` argument in ``urllib.parse.parse_qs`` and
+``urllib.parse.parse_qsl`` when parsing :class:`str` query strings.  Previously,
+this raised a ``TypeError``.



More information about the Python-checkins mailing list