Windows binary stdin goes EOF after \x1a character

Discussion in 'Python' started by Dan, Oct 15, 2010.

  1. Dan

    Dan Guest

    I am writing a Windows program in Python 3.1.2 that reads binary data
    from stdin. Whenever it hits a \x1a character, stdin goes EOF and no
    more data can be read. A short program that exhibits this problem is:

    #listing of main.pyw
    import sys
    def go():
    bb=sys.stdin.buffer.raw.read(10000)
    print(bb,file=sys.stderr)
    sys.stderr.flush()
    go()
    go()


    The program is run as "pythonw.exe main.pyw"


    When fed with a block of bytes equivalent to "bytes(range(1,255))",
    the following output is observed...

    b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f
    \x10\x11\x12\x13\x14\x15\x16\x17\x18\x19'
    b''

    ....and according to the documentation, the empty bytes object means
    EOF.

    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)}},


    Thanks,
    Dan.
     
    Dan, Oct 15, 2010
    #1
    1. Advertisements

  2. Dan

    Neil Cerutti Guest

    stdin is not in binary mode. Try invoking with -u option.
     
    Neil Cerutti, Oct 15, 2010
    #2
    1. Advertisements

  3. Dan

    Dan Guest

    That worked, thanks!
    Dan.
     
    Dan, Oct 15, 2010
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.