Unicode, stdout, and stderr

Frank Millman frank at chagford.com
Tue Jul 22 03:15:39 EDT 2014


"Steven D'Aprano" <steve at pearwood.info> wrote in message 
news:53ce0b96$0$29897$c3e8da3$5496439d at news.astraweb.com...
> On Tue, 22 Jul 2014 08:18:08 +0200, Frank Millman wrote:
>
>> Hi all
>>
>> This is not important, but I would appreciate it if someone could
>> explain the following, run from cmd.exe on Windows Server 2003 -
>>
>> C:\>python
>> Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32
>> bit (In
>> tel)] on win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> x = '\u2119'
>>>>> x  # this uses stderr
>> '\u2119'
>
>
> What makes you think it uses stderr? To the best of my knowledge, it uses
> stdout.
>

This is from the docs on sys.stdxxx

sys.stdin
sys.stdout
sys.stderr

File objects used by the interpreter for standard input, output and errors:
  - stdin is used for all interactive input (including calls to input());
  - stdout is used for the output of print() and expression statements and 
for the prompts of input();
  - The interpreter's own prompts and its error messages go to stderr.

>
>> It seems that there is a difference between writing to stdout and
>> writing to stderr.
>
> 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.

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

It fails.

The clue that led me to stderr is that the logging module displays unicode 
strings to the console without a problem. I delved into the source code, and 
found that it writes to stderr. When I changed mine to stderr, it also 
worked.

Frank






More information about the Python-list mailing list