Index: Lib/urllib.py =================================================================== --- Lib/urllib.py (revision 78945) +++ Lib/urllib.py (working copy) @@ -1158,20 +1158,17 @@ if match: return match.group(1, 2) return attr, None -_hextochr = dict(('%02x' % i, chr(i)) for i in range(256)) -_hextochr.update(('%02X' % i, chr(i)) for i in range(256)) - def unquote(s): """unquote('abc%20def') -> 'abc def'.""" res = s.split('%') for i in xrange(1, len(res)): item = res[i] try: - res[i] = _hextochr[item[:2]] + item[2:] - except KeyError: - res[i] = '%' + item + res[i] = chr(int(item[:2], 16)) + item[2:] except UnicodeDecodeError: res[i] = unichr(int(item[:2], 16)) + item[2:] + except ValueError: + res[i] = '%' + item return "".join(res) def unquote_plus(s): Index: Lib/test/test_urllib.py =================================================================== --- Lib/test/test_urllib.py (revision 78945) +++ Lib/test/test_urllib.py (working copy) @@ -439,6 +439,32 @@ "using unquote(): not all characters escaped: " "%s" % result) + def test_unquoting_badpercent(self): + # Test unquoting on bad percent-escapes + given = '%xab' + expect = given + result = urllib.unquote(given) + self.assertEqual(expect, result, "using unquote(): %r != %r" + % (expect, result)) + given = '%x' + expect = given + result = urllib.unquote(given) + self.assertEqual(expect, result, "using unquote(): %r != %r" + % (expect, result)) + given = '%' + expect = given + result = urllib.unquote(given) + self.assertEqual(expect, result, "using unquote(): %r != %r" + % (expect, result)) + + def test_unquoting_mixed_case(self): + # Test unquoting on mixed-case hex digits in the percent-escapes + given = '%Ab%eA' + expect = '\xab\xea' + result = urllib.unquote(given) + self.assertEqual(expect, result, "using unquote(): %r != %r" + % (expect, result)) + def test_unquoting_parts(self): # Make sure unquoting works when have non-quoted characters # interspersed