summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorStein Karlsen <karlsen.stein@gmail.com>2019-10-14 10:36:29 (GMT)
committerTal Einat <taleinat+github@gmail.com>2019-10-14 10:36:29 (GMT)
commitaad2ee01561f260c69af1951c0d6fcaf75c4d41b (patch)
tree6ffb53582b94065c4eb04012aaffa49a1af5422f /Lib
parent9cb51f4e20033f5fd4fed46036e347f263bb6d5b (diff)
downloadcpython-aad2ee01561f260c69af1951c0d6fcaf75c4d41b.zip
cpython-aad2ee01561f260c69af1951c0d6fcaf75c4d41b.tar.gz
cpython-aad2ee01561f260c69af1951c0d6fcaf75c4d41b.tar.bz2
bpo-32498: urllib.parse.unquote also accepts bytes (GH-7768)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_urllib.py25
-rw-r--r--Lib/urllib/parse.py2
2 files changed, 25 insertions, 2 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 9a6b5f6..3f59c66 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -1049,8 +1049,6 @@ class UnquotingTests(unittest.TestCase):
"%s" % result)
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
- with support.check_warnings(('', BytesWarning), quiet=True):
- self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')
def test_unquoting_badpercent(self):
# Test unquoting on bad percent-escapes
@@ -1210,6 +1208,29 @@ class UnquotingTests(unittest.TestCase):
self.assertEqual(expect, result,
"using unquote(): %r != %r" % (expect, result))
+ def test_unquoting_with_bytes_input(self):
+ # ASCII characters decoded to a string
+ given = b'blueberryjam'
+ expect = 'blueberryjam'
+ result = urllib.parse.unquote(given)
+ self.assertEqual(expect, result,
+ "using unquote(): %r != %r" % (expect, result))
+
+ # A mix of non-ASCII hex-encoded characters and ASCII characters
+ given = b'bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y'
+ expect = 'bl\u00e5b\u00e6rsyltet\u00f8y'
+ result = urllib.parse.unquote(given)
+ self.assertEqual(expect, result,
+ "using unquote(): %r != %r" % (expect, result))
+
+ # A mix of non-ASCII percent-encoded characters and ASCII characters
+ given = b'bl%c3%a5b%c3%a6rsyltet%c3%b8j'
+ expect = 'bl\u00e5b\u00e6rsyltet\u00f8j'
+ result = urllib.parse.unquote(given)
+ self.assertEqual(expect, result,
+ "using unquote(): %r != %r" % (expect, result))
+
+
class urlencode_Tests(unittest.TestCase):
"""Tests for urlencode()"""
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
index b660878..3a38dc1 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):
+ return unquote_to_bytes(string).decode(encoding, errors)
if '%' not in string:
string.split
return string