When an exception is printed and a Unicode message is sent, Python will try to encode it using the encoding returned by sys.getdefaultencoding() . If this fails, the encoding error is suppressed, and you get a strange output.
In a print situation, a Unicode string is encoded using sys.stdout.encoding . Yes, it would be better if excepthook used sys.stderr.encoding rather than sys.getdefaultencoding() .
Please note that the following works.
raise LookupError(u"symbol: \u0411".encode(your_encoding))
You can also change the default encoding in sitecustomize or usercustomize by calling sys.setdefaultencoding(your_encoding) . Your system must be configured so that the default encoding is sys.stderr.encoding (and the encoding of other standard streams).
Also, this problem does not exist in Python 3.
source share