Skip to content

[3.8] bpo-32498: Improve exception message on passing bytes to urllib.parse.unquote #22746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,12 @@ def test_unquote_with_unicode(self):
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()"""

Expand Down
2 changes: 2 additions & 0 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.