Python's urllib.quote
and urllib.unquote
do not handle Unicode correctly in Python 2.6.5. This is what happens:
In [5]: print urllib.unquote(urllib.quote(u'Cataño'))
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/kkinder/<ipython console> in <module>()
/usr/lib/python2.6/urllib.pyc in quote(s, safe)
1222 safe_map[c] = (c in safe) and c or ('%%%02X' % i)
1223 _safemaps[cachekey] = safe_map
-> 1224 res = map(safe_map.__getitem__, s)
1225 return ''.join(res)
1226
KeyError: u'\xc3'
Encoding the value to UTF8 also does not work:
In [6]: print urllib.unquote(urllib.quote(u'Cataño'.encode('utf8')))
Cataño
It's recognized as a bug and there is a fix, but not for my version of Python.
What I'd like is something similar to urllib.quote/urllib.unquote, but handles unicode variables correctly, such that this code would work:
decode_url(encode_url(u'Cataño')) == u'Cataño'
Any recommendations?