[Patches] [ python-Patches-512981 ] readline /dev/tty problem

noreply@sourceforge.net noreply@sourceforge.net
Mon, 07 Oct 2002 14:32:25 -0700


Patches item #512981, was opened at 2002-02-04 13:42
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=512981&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Daniel Stutzbach (agthorr)
Assigned to: Nobody/Anonymous (nobody)
Summary: readline /dev/tty problem

Initial Comment:
GNU readline doesn't work if the C symbol "stdin" is
not a tty, even if the python symbol sys.stdin is. 
This will happen, for example, if a program initially
receives input from a pipe, but then changes sys.stdin
to read from /dev/tty.  (This is how programs like
"less" work)

Here's a sample program that exhibits this behavior:

------------------------------------------------------------------------
#!/usr/bin/env python

import sys

mail = sys.stdin.read ()

sys.stdin = open ('/dev/tty', 'r')

import readline

foo = raw_input ('add_idea> ')
print foo
------------------------------------------------------------------------

You can test this by saving the above program to a file
(foo.py), piping data to it, then trying to use GNU
readline editing commands at the prompt.

E.g.:

------------------------------------------------------------------------
liberty:~$ cat ideas.html | ./foo.py
add_idea> asdfsdf^Afoo
asdfsdffoo
------------------------------------------------------------------------

The patch attached seems to fix the problem.  You may
want to grep the source for other modules that may have
similar bugs.  Also, this patch assumes that the
readline module is imported *after* sys.stdin is
changed.  This much better than nothing (particularly
if it's documented), but there may be a better solution.

-- Agthorr

----------------------------------------------------------------------

>Comment By: Daniel Stutzbach (agthorr)
Date: 2002-10-07 14:32

Message:
Logged In: YES 
user_id=6324

Yes, I'm interested in pursuing it.  Let me know what I can
do to move this along.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-10-07 14:15

Message:
Logged In: YES 
user_id=21627

Are you interested in pursuing this feature? I cannot promise 
that I will apply it blankly, only that I will review it.

----------------------------------------------------------------------

Comment By: Daniel Stutzbach (agthorr)
Date: 2002-04-25 13:02

Message:
Logged In: YES 
user_id=6324

If I create a patch that operates as described in my
previous followup, will you apply it?  Is there anything I
can do to get this integrated into the main python trunk?  I
don't like having to repatch and rebuild python everytime a
new version comes out that I need for some other reason :>


----------------------------------------------------------------------

Comment By: Daniel Stutzbach (agthorr)
Date: 2002-02-05 10:21

Message:
Logged In: YES 
user_id=6324

1) Well, it lets python treat sys.stdin as a tty even if C
stdin != python sys.stdin.  It still checks to make sure
sys.stdin is a tty using isatty().  If some user changes
sys.stdin to point to a tty, but *wants* Python to treat it
as a non-tty, then this might cause them some grief.  I
can't think of any case where they'd want to do that,
though.  The behavior would be unchanged when sys.stdin
points to a regular file.

2) hmm.. I suppose, ideally, the readline module should
smoothly handle sys.stdin being changed out from under it. 
Readline alters various terminal settings on rl_instream
during initialization, though.  For example, it changes the
terminal to raw or cbreak mode from cooked mode, so that it
can receive input a character at a time instead of a line at
a time.  It may be possible to uninitialized and
reinitialized terminal each time call_readline is called, I
suppose (I believe libreadline provides hooks for this).  It
would also have to check if sys.stdin is a tty, and call
PyFile_GetLine if it is not.

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-02-05 02:35

Message:
Logged In: YES 
user_id=6656

Comments:

1) in what ways does this change existing behaviour?  I can
think of a few, but are there any that will inconvenience
existing users
2) why not do the rl_instream = PySys_GetObject("stdin")
dance in call_readline()?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=512981&group_id=5470