]> granicus.if.org Git - python/commitdiff
Merged revisions 79047 via svnmerge from
authorSenthil Kumaran <orsenthil@gmail.com>
Mon, 29 Mar 2010 19:30:44 +0000 (19:30 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Mon, 29 Mar 2010 19:30:44 +0000 (19:30 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79047 | senthil.kumaran | 2010-03-18 17:44:15 +0530 (Thu, 18 Mar 2010) | 3 lines

  Fix for Issue8135 - urllib.unquote to support mixed percent escapes
........

Lib/test/test_urllib.py
Lib/urllib.py
Lib/urlparse.py

index d05574cb9983a052719030736e39df8166f266fe..402309ce3e712101217ae6d28c3eec294c896b22 100644 (file)
@@ -440,6 +440,32 @@ class UnquotingTests(unittest.TestCase):
                          "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
index 2fdf927f0ad6e9684dfc07bf5f7d8e5ff7fd8afd..e12d981ad026b62d12b295d1f98c4c102f40cddb 100644 (file)
@@ -1162,8 +1162,8 @@ def splitvalue(attr):
     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))
+_hexdig = '0123456789ABCDEFabcdef'
+_hextochr = dict((a+b, chr(int(a+b,16))) for a in _hexdig for b in _hexdig)
 
 def unquote(s):
     """unquote('abc%20def') -> 'abc def'."""
index c41e25eea145027afc3b24d24d6fc183e62d024e..15a394a0bed3bd16d6432cc13f35abba246322d7 100644 (file)
@@ -272,8 +272,9 @@ def urldefrag(url):
 # Cannot use directly from urllib as it would create circular reference.
 # urllib uses urlparse methods ( urljoin)
 
-_hextochr = dict(('%02x' % i, chr(i)) for i in range(256))
-_hextochr.update(('%02X' % i, chr(i)) for i in range(256))
+
+_hexdig = '0123456789ABCDEFabcdef'
+_hextochr = dict((a+b, chr(int(a+b,16))) for a in _hexdig for b in _hexdig)
 
 def unquote(s):
     """unquote('abc%20def') -> 'abc def'."""