summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2005-09-10 06:49:04 (GMT)
committerRaymond Hettinger <python@rcn.com>2005-09-10 06:49:04 (GMT)
commit803ce801ab84c2e11a99dc160512cd905c0b6520 (patch)
tree6308187710891ca6d554fe0549de874e21e46d21 /Lib
parent957b1266492877d97a790a5698a9492512e5b8cd (diff)
downloadcpython-803ce801ab84c2e11a99dc160512cd905c0b6520.zip
cpython-803ce801ab84c2e11a99dc160512cd905c0b6520.tar.gz
cpython-803ce801ab84c2e11a99dc160512cd905c0b6520.tar.bz2
Simplify and speed-up unquote().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/urllib.py25
1 files changed, 10 insertions, 15 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index 2889b3d..113b828 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -1049,23 +1049,18 @@ def splitgophertype(selector):
return selector[1], selector[2:]
return None, selector
+_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'."""
- mychr = chr
- myatoi = int
- list = s.split('%')
- res = [list[0]]
- myappend = res.append
- del list[0]
- for item in list:
- if item[1:2]:
- try:
- myappend(mychr(myatoi(item[:2], 16))
- + item[2:])
- except ValueError:
- myappend('%' + item)
- else:
- myappend('%' + item)
+ 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
return "".join(res)
def unquote_plus(s):