Issue 13024: cgitb uses stdout encoding (original) (raw)

cgitb module writes its output to sys.stdout by default and so encode its HTML document into stdout encoding, whereas it doesn't specify the HTML encoding.

Moreover it uses stdout error handler. If the locale encoding is ASCII whereas a filename in the traceback contains a non-ASCII character, it fails to encode the non-ASCII characters. Another error handler should be used, xmlcharrefreplace is a good candidate.

To reproduce this issue, use the following script with ASCII locale encoding and a non-ASCII character in name of the current directory:

import cgitb cgitb.enable() raise ValueError("Hello World")

Attached patch uses a workaround similar to the one used in Log._log() from distutils.logging.