re-opening stdin in raw (binary) mode?

Randall Hopper aa8vb at yahoo.com
Tue Jan 18 12:31:43 EST 2000


Erno Kuusela:
 |>>>>> "Samuel" == Samuel A Falvo <kc5tja at garnet.armored.net> writes:
 |
 |    Samuel> under Linux, you can perform an ioctl() on file descriptor
 |    Samuel> 1 (or is it 0?) to set a binary-mode flag.
 |
 |There is no binary mode in unix/linux, it's a windoze (and mac?) thing.

Sure there is.  It just happens to be a no-op on UNIX. :-)

Yeah, for MSWin/DOS in C, you need something like:

    _setmode(fileno(stdin),O_BINARY);

if you want to keep the current stdin stream (if you want to reopen stdin
as binary to some other file/device, just freopen it).

A quick search of the Python newsgroup archive for _setmode and a grep of
the Python tree turns up the likely answer:

------------------------------------------------------------------------------
1)  Invoke python with the -u option to kick in a BINARY _setmode under the
    hood for both stdin and stdout.
------------------------------------------------------------------------------

Now, having it be unbuffered too may kill your performance (...maybe
there's a Pythonic way to setvbuf an open stream in Python code).

Also, there's also a grep hit in the msvcrt MSWin module.  Check it out.
In particular:

------------------------------------------------------------------------------
2)  http://www.python.org/doc/current/lib/msvcrt-files.html

    setmode (fd, flags) - Set the line-end translation mode for the file
    descriptor fd. To set it to text mode, flags should be os.O_TEXT; for
    binary, it should be os.O_BINARY.
------------------------------------------------------------------------------

Randall




More information about the Python-list mailing list