Unicode, stdout, and stderr

Frank Millman frank at chagford.com
Tue Jul 22 05:29:29 EDT 2014


"Peter Otten" <__peter__ at web.de> wrote in message 
news:lql9oi$hlt$1 at ger.gmane.org...
> Frank Millman wrote:
>
[...]
>
>> Out of interest, does the same thing happen when writing to sys.stderr?
>
> If you are asking about the fallback mechanism, that is specific to
> sys.displayhook in the interactive interpreter.
>
> But stdout and stderr do handle errors differently:
>
>>>> import sys
>>>> sys.stdout.errors
> 'strict'
>>>> sys.stderr.errors
> 'backslashreplace'
>
> So a codepoint written to stdout that cannot be encoded with 
> stdout.encoding
> raises an error while a codepoint written to stderr that cannot be encoded
> with stderr.encoding is escaped.
>
> Another way to make stdout more forgiving:
>
>>>> import sys
>>>> print("\u2119")
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/usr/local/lib/python3.4/encodings/cp437.py", line 19, in encode
>    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u2119' in
> position 0: character maps to <undefined>
>>>> sys.stdout = open(1, mode="w", errors="xmlcharrefreplace",
> encoding=sys.stdout.encoding, closefd=False)
>>>> print("\u2119")
>>

That's a lot of very useful information.

Thanks very much

Frank






More information about the Python-list mailing list