[Python-ideas] Replacing the standard IO streams (was Re: changing sys.stdout encoding)

MRAB python at mrabarnett.plus.com
Sun Jun 10 21:01:21 CEST 2012


On 10/06/2012 19:34, Paul Moore wrote:
> On 10 June 2012 19:12, MRAB<python at mrabarnett.plus.com>  wrote:
>>  On 10/06/2012 17:41, Stephen J. Turnbull wrote:
>>>  I am a little concerned with MRAB's report that
>>>
>>>       import sys
>>>       print("hello")
>>>       sys.stdout.flush()
>>>       sys.stdout = open(sys.stdout.fileno(), 'w', encoding='utf-8')
>>>       print("hello")
>>>
>>>  doesn't work as expected, though.  (It does work for me on Mac OS X,
>>>  both as above -- of course there are no '\r's in the output -- and
>>>  with 'print("hello", end="\r\n")'.)
>>>
>>  That's actually Python 3.1. From Python 3.2 it's slightly different,
>>  but still not quite right:
>>
>>  Python 3.1:     "hello\r\nhello\r\r\n"
>>  Python 3.2:     "hello\nhello\r\n"
>>  Python 3.3.0a4: "hello\nhello\r\n"
>>
>>  All on Windows.
>
> Not here (Win 7 32-bit):
>
> PS D:\Data>  type t.py
> import sys
> print("Hello!")
> sys.stdout.flush()
>
> sys.stdout = open(sys.stdout.fileno(), 'w', encoding='utf-8')
> print("Hello!")
> PS D:\Data>  py -3.2 t.py | od -c
> 0000000   H   e   l   l   o   !  \r  \n   H   e   l   l   o   !  \r  \n
> 0000020
>
I'm using Windows XP Pro (32-bit), initially sys.stdout.encoding ==
"cp1252".



More information about the Python-ideas mailing list