Unicode and exception strings

Rune Froysa rune.froysa at usit.uio.no
Fri Jan 9 13:18:39 CET 2004

Assuming an exception like:

  x = ValueError(u'\xf8')

AFAIK the common way to get a string representation of the exception
as a message is to simply cast it to a string: str(x).  This will
result in an "UnicodeError: ASCII encoding error: ordinal not in

The common way to fix this is with something like
u'\xf8'.encode("ascii", 'replace').  However I can't find any way to
tell ValueErrors __str__ method which encoding to use.

Is it possible to solve this without using sys.setdefaultencoding()
from sitecustomize?

Rune Frøysa

More information about the Python-list mailing list