Unicode, stdout, and stderr

Lele Gaifax lele at metapensiero.it
Tue Jul 22 03:36:10 EDT 2014


"Frank Millman" <frank at chagford.com> writes:

> "Steven D'Aprano" <steve at pearwood.info> wrote in message 
> news:53ce0b96$0$29897$c3e8da3$5496439d at news.astraweb.com...
>> I would be surprised if that were the case, but I don't have a Windows
>> box to test it. Try this:
>>
>>
>> import sys
>> print(x, file=sys.stderr)  # I expect this will fail
>
> It does not fail.

Effectively it does not, but for some reason it actually print the
repr() of the string.

>> print(repr(x), file=sys.stdout)  # I expect this will succeed
>>
>
> It fails.

This surprises me as well, why does it fail here?

>>> repr('\u2119')
"'\u2119'"
>>> print(repr('\u2119'))
Traceback ... UnicodeEncodeError ...

On GNU/Linux, I get:

>>> repr('\u2119')
"'ℙ'"
>>> print(repr('\u2119'))
'ℙ'

Uhm, it must be related to the fact that on Py3 the repr() of something
is a unicode object too, so the output machinery tries to encode it to
the output encoding.... Still, I miss the difference between stdout and
stderr (both are cp437, accordingly to sys.xxx.encoding).

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list