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

Paul Moore p.f.moore at gmail.com
Sun Jun 10 22:07:00 CEST 2012


On 10 June 2012 20:01, MRAB <python at mrabarnett.plus.com> wrote:
> 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".

PS D:\Data> py -3 -c "import sys; print(sys.stdout.encoding)"
cp850

This is at the console (Powershell) - are you running from within
something like idle, or a GUI environment?

Paul.



More information about the Python-ideas mailing list