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


Messages In This Thread
How can I use GNU readline when the value of sys.stdin has been changed? - by thePhysicist8 - Dec-29-2016, 04:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial Readline() conversion to array bprosman 1 1,960 Apr-11-2023, 12:44 AM
Last Post: deanhystad
  Greek letters with .readline() and tkinter KinkgOfKeks 7 1,811 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,195 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,041 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,563 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  The behavior of tune model has changed Led_Zeppelin 5 4,506 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  STDIN not working H84Gabor 3 3,651 Sep-06-2021, 08:10 AM
Last Post: H84Gabor
  readline inside and outside functions paul18fr 2 2,083 May-20-2021, 01:15 PM
Last Post: csr
  how can a variable change if I haven't changed it? niminim 5 3,139 Apr-07-2021, 06:57 PM
Last Post: niminim
  TypeError: file must have 'read' and 'readline' attributes hobbyist 6 11,086 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