Unicode problem with exec

Diez B. Roggisch deets at nospam.web.de
Fri Jun 23 15:07:13 CEST 2006


Thomas Heller schrieb:
> I'm using code.Interactive console but it doesn't work correctly
> with non-ascii characters.  I think it boils down to this problem:
> 
> Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on 
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> print u"ä"
> ä
>>>> exec 'print u"ä"'
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "<string>", line 1, in ?
>  File "c:\python24\lib\encodings\cp850.py", line 18, in encode
>    return codecs.charmap_encode(input,errors,encoding_map)
> UnicodeEncodeError: 'charmap' codec can't encode character u'\x84' in 
> position 0: character maps to <undefined>
>>>> ^Z
> 
> Why does the exec call fail, and is there a workaround?

Most probably because you failed to encode the snippet as whole - so the 
embedded unicode literal isn't encoded properly.

As your exec-encoding seems to be cp850, maybe

exec u"print u'ä'".encode("cp850")

works.

Diez



More information about the Python-list mailing list