Problems with select() and stdin (solved)

Iñigo Serna inigoserna at terra.es
Tue Aug 20 13:10:36 EDT 2002


Hello again,

El dom, 18-08-2002 a las 17:34, jepler at unpythonic.net escribió:
> When you take input from a pipe, sys.stdin corresponds to the program on
> the left of the pipeline, not the terminal where the program is running (if
> any).

Aha!!!
 
> If you open the special device /dev/tty and assign the result to sys.stdin,
> raw_input reads from the terminal again, as in this example:
> 
> $ echo "standard input" | python -c '
> import sys;
> print sys.stdin.read(),;
> sys.stdin = open("/dev/tty");
> print raw_input("? ")'
> standard input
> ? _
> 
> This advice only applies to unix where /dev/tty is supported.

This runs ok with raw_input, but didn't work with my program, which uses
(n)curses. I needed to include an explicit close.

        fd = select([sys.stdin], [], [], 1)[0][0]
        stdin = ''.join(fd.readlines())
        # close stdin (pipe) and open terminal for reading
        os.close(0)
        sys.stdin = open(os.ttyname(1), 'r')

And now everything works ok.

Thanks Jeff,

Iñigo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Esta parte del mensaje esta firmada digitalmente
URL: <http://mail.python.org/pipermail/python-list/attachments/20020820/091f3026/attachment.sig>


More information about the Python-list mailing list