[3.8] bpo-32498: Improve exception message on passing bytes to urllib… · python/cpython@1a3f7c0 (original) (raw)
3 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1245,6 +1245,12 @@ def test_unquote_with_unicode(self): | ||
1245 | 1245 | self.assertEqual(expect, result, |
1246 | 1246 | "using unquote(): %r != %r" % (expect, result)) |
1247 | 1247 | |
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 | + | |
1248 | 1254 | class urlencode_Tests(unittest.TestCase): |
1249 | 1255 | """Tests for urlencode()""" |
1250 | 1256 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -631,6 +631,8 @@ def unquote(string, encoding='utf-8', errors='replace'): | ||
631 | 631 | |
632 | 632 | unquote('abc%20def') -> 'abc def'. |
633 | 633 | """ |
634 | +if isinstance(string, bytes): | |
635 | +raise TypeError('Expected str, got bytes') | |
634 | 636 | if '%' not in string: |
635 | 637 | string.split |
636 | 638 | return string |
Original file line number | Diff line number | Diff 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. |