Python Forum
How can I use GNU readline when the value of sys.stdin has been changed?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I use GNU readline when the value of sys.stdin has been changed?
#1
I'm using Python 3.5.2 on Linux.

I'm trying to get my program to take input from a pipe (i.e. find ~/docs | main.py) and later take input from the terminal. After reading this Stack Overflow post (stackoverflow.com/questions/7141331/pipe-input-to-python-program-and-later-get-input-from-user), I tried opening /dev/tty to replace sys.stdin.
import sys
import readline

def tty_input(prompt):
   with open("/dev/tty") as terminal:
       sys.stdin = terminal
       user_input = input(prompt)
   sys.stdin = sys.__stdin__
   return user_input
The problem with this approach is that GNU readline doesn't work when sys.stdin != sys.__stdin__. I can't use the arrow keys to move the cursor or navigate the history like I can normally. I read of a patch for this issue that was proposed here (bugs.python.org/issue512981), but I don't know that anything came of it.

If there is a way to accept input from both a pipe and the terminal without changing the value of sys.stdin, I'm open to suggestions.

Apologies for the URLs, but I can't post clickable links yet.
Reply
#2
This is just an idea that may or may not have merit.
While sitting here scratching my butt, I thought,
why not create a thread that monitors stdin. Whenever
activity is detected, notify the main thread which could
then kick off an event. (As in interrupt), that redirects stdin.

You may want to watch this:
An Introduction to Python Concurrency - David Beazley

And perhaps scratch you're butt as well!
Reply
#3
Welcome to the Forum by the way
Reply
#4
I wonder why you would need to do that. If you need to feed a few things to your program while entering more data at the terminal there are other ways. If the caller is a bash script you can use expect to feed input to the terminal, for instance.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
As stated

Quote:This is just an idea that may or may not have merit.


I normally think of the more complicated than necessary solution,
then usually see the more obvious (and simple) solution later.
Reply
#6
This is typical for the most of the people including me for the most of the time.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
in a recent project i needed to redirect stdout and stderr.  the way i did it was to use os.open() to open a new POSIX file descriptor then use a couple calls to os.dup2() to change descriptors 1 and 2.  i just made sure i did stdout,flush() ahead of this.  maybe you can do this for stdin (0).  be sure you have empty buffers to avoid mixed data.

Type

stdout.flush()
fd = os.open(ln,os.O_APPEND|os.O_CREAT|os.O_WRONLY)
os.dup2(fd,1) if fd != 1 else None
os.dup2(1,2)
"ln" means "log name".  this is opening log files for several cloud programs.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial Readline() conversion to array bprosman 1 1,899 Apr-11-2023, 12:44 AM
Last Post: deanhystad
  Greek letters with .readline() and tkinter KinkgOfKeks 7 1,732 Mar-24-2023, 05:13 PM
Last Post: deanhystad
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,047 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  When is stdin not connected to a tty, but can still be used interactively? stevendaprano 1 1,006 Sep-24-2022, 05:06 PM
Last Post: Gribouillis
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,520 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  The behavior of tune model has changed Led_Zeppelin 5 4,423 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  STDIN not working H84Gabor 3 3,539 Sep-06-2021, 08:10 AM
Last Post: H84Gabor
  readline inside and outside functions paul18fr 2 2,037 May-20-2021, 01:15 PM
Last Post: csr
  how can a variable change if I haven't changed it? niminim 5 3,043 Apr-07-2021, 06:57 PM
Last Post: niminim
  TypeError: file must have 'read' and 'readline' attributes hobbyist 6 10,864 Jun-12-2020, 05:12 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020