From 79e17f6f666e6b41320b7355c34d8a292fa5b586 Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Mon, 19 Jul 2010 18:17:19 +0000 Subject: Fix Issue9301 - urllib.parse.unquote and unquote_to_byte to raise TypeError for None. --- Lib/test/test_urllib.py | 3 +++ Lib/urllib/parse.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index e293cf0..44574c9 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -554,6 +554,7 @@ class UnquotingTests(unittest.TestCase): self.assertEqual(result.count('%'), 1, "using unquote(): not all characters escaped: " "%s" % result) + self.assertRaises(TypeError, urllib.parse.unquote, None) def test_unquoting_badpercent(self): # Test unquoting on bad percent-escapes @@ -589,6 +590,8 @@ class UnquotingTests(unittest.TestCase): self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r" % (expect, result)) + self.assertRaises(TypeError, urllib.parse.unquote_to_bytes, None) + def test_unquoting_mixed_case(self): # Test unquoting on mixed-case hex digits in the percent-escapes given = '%Ab%eA' diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 82edea1..691c004 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -314,6 +314,8 @@ def unquote_to_bytes(string): # Note: strings are encoded as UTF-8. This is only an issue if it contains # unescaped non-ASCII characters, which URIs should not. if not string: + if string is None: + raise TypeError('None object is invalid for unquote_to_bytes()') return b'' if isinstance(string, str): string = string.encode('utf-8') @@ -339,6 +341,8 @@ def unquote(string, encoding='utf-8', errors='replace'): unquote('abc%20def') -> 'abc def'. """ if not string: + if string is None: + raise TypeError('None object is invalid for unquote() function.') return string res = string.split('%') if len(res) == 1: -- cgit v0.12