Skip to content

Commit 1a3f7c0

Browse files
authored
[3.8] bpo-32498: Improve exception message on passing bytes to urllib.parse.unquote (GH-22746)
1 parent 1040299 commit 1a3f7c0

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_urllib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,12 @@ def test_unquote_with_unicode(self):
12451245
self.assertEqual(expect, result,
12461246
"using unquote(): %r != %r" % (expect, result))
12471247

1248+
def test_unquoting_with_bytes_input(self):
1249+
# Bytes not supported yet
1250+
with self.assertRaisesRegex(TypeError, 'Expected str, got bytes'):
1251+
given = b'bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y'
1252+
urllib.parse.unquote(given)
1253+
12481254
class urlencode_Tests(unittest.TestCase):
12491255
"""Tests for urlencode()"""
12501256

Lib/urllib/parse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,8 @@ def unquote(string, encoding='utf-8', errors='replace'):
631631
632632
unquote('abc%20def') -> 'abc def'.
633633
"""
634+
if isinstance(string, bytes):
635+
raise TypeError('Expected str, got bytes')
634636
if '%' not in string:
635637
string.split
636638
return string
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Clearer exception message when passing an argument of type bytes to
2+
:func:`urllib.parse.unquote`. This is only for 3.8; in 3.9 and later this
3+
function accepts bytes inputs as well. PR by Irit Katriel.

0 commit comments

Comments
 (0)