[3.11] gh-116764: Fix regressions in urllib.parse.parse_qsl() (GH-116801) (GH-116895)

* 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
(bdba8ef42b).
(cherry picked from commit 1069a462f6)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-03-16 12:04:31 +01:00 committed by GitHub
parent d16519ab7b
commit eddfdb3e50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 1 deletions

View file

@ -1067,6 +1067,30 @@ class UrlParseTestCase(unittest.TestCase):
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: