From: Irit Katriel Date: Sun, 18 Oct 2020 21:06:34 +0000 (+0100) Subject: [3.8] bpo-32498: Improve exception message on passing bytes to urllib.parse.unquote... X-Git-Tag: v3.8.7rc1~110 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1a3f7c042a32fb813835243bd7f96e47c665bfdc;p=thirdparty%2FPython%2Fcpython.git [3.8] bpo-32498: Improve exception message on passing bytes to urllib.parse.unquote (GH-22746) --- diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 1c247c5d1543..862668715118 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1245,6 +1245,12 @@ class UnquotingTests(unittest.TestCase): self.assertEqual(expect, result, "using unquote(): %r != %r" % (expect, result)) + def test_unquoting_with_bytes_input(self): + # Bytes not supported yet + with self.assertRaisesRegex(TypeError, 'Expected str, got bytes'): + given = b'bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y' + urllib.parse.unquote(given) + class urlencode_Tests(unittest.TestCase): """Tests for urlencode()""" diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index e2b6f133e1cd..95be7181133b 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -631,6 +631,8 @@ def unquote(string, encoding='utf-8', errors='replace'): unquote('abc%20def') -> 'abc def'. """ + if isinstance(string, bytes): + raise TypeError('Expected str, got bytes') if '%' not in string: string.split return string diff --git a/Misc/NEWS.d/next/Library/2020-10-18-19-22-39.bpo-32498.MoqSgo.rst b/Misc/NEWS.d/next/Library/2020-10-18-19-22-39.bpo-32498.MoqSgo.rst new file mode 100644 index 000000000000..3083ded758a8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-18-19-22-39.bpo-32498.MoqSgo.rst @@ -0,0 +1,3 @@ +Clearer exception message when passing an argument of type bytes to +:func:`urllib.parse.unquote`. This is only for 3.8; in 3.9 and later this +function accepts bytes inputs as well. PR by Irit Katriel.