Windows binary stdin goes EOF after \x1a character

Neil Cerutti neilc at norwich.edu
Fri Oct 15 22:42:33 CEST 2010


On 2010-10-15, MRAB <python at mrabarnett.plus.com> wrote:
>> I wrote an equivalent program in C++ using the win32
>> ReadFile() call, and it read all 255 bytes just fine.  What am
>> I doing wrong with the python code?
>>
>>
>> I am using Erlang to launch the Python program as a subprocess.  The
>> Erlang fragment that launches this and sends the data is:
>>
>>      Port=open_port( {spawn_executable, "c:/Python31/pythonw.exe"},
>> 		    [{args, ["c:/iotest/main.pyw"]}]),
>>      Port ! {self(),{command,lists:seq(1,255)}},
>
> By default, Python opens stdin in buffered text mode in which
> '\x1A' marks the end of the text. Try adding the "-u" option
> ("unbuffered") to Python's command line:
>
>      pythonw.exe -u main.pyw

To expound a bit, using a windows-only module you can switch the
mode of stdin.

import sys
import msvcrt

msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

-- 
Neil Cerutti



More information about the Python-list mailing list