[pygtk] Default encoding altered by pygtk?

Fredrik Corneliusson fredrik.corneliusson at gmail.com
Thu Nov 16 17:57:41 WST 2006


Hi,
I have a question related to GTK and encoding.
I'm working on a pygtk app that's connecting to a server and uses
urlencode, if I just try to encode strings with unicode characters I
get UnicodeEncodeError from urlencode by default. But if I import gtk
before doing the conversion it looks like the string is encoded to
UTF-8 on the fly and the conversion works. Does gtk set the default
encoding to UTF-8 and how can I do it myself without importing gtk?

I'm using Python 2.4.2 and pygtk 2.8.6 on windows.

See example below:

>>> from urllib import urlencode
>>> urlencode({'test'+unichr(0xA5):'test'})
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\Python24\lib\urllib.py", line 1151, in urlencode
    k = quote_plus(str(k))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa5' in
position 4: ordinal not in range(128)

>>> import gtk
>>> urlencode({'test'+unichr(0xA5):'test'})
'test%C2%A5=test'

Thanks for all your hard work,
Fredrik


More information about the pygtk mailing list