0

I'm trying to make a Terminal Emulator and for that I need to handle stdin/stdout/stder to redirect them to a GUI

I already managed to handle stdout and stderr, the problem is for stdin.

I would like to have a function that is called every time stdin is read, so I could request input to the user.

I use

subprocess.Popen("command", shell="True")

I tried using temp file and editing read, readline and readlines of the files but the problem is that if I understood correctly (from the source) subprocess.Popen takes the file and reopen it to use it.

I also tried creating custom file like objects but as I said before subprocess.Popen takes the file and try to reopen it but as my object is a fake file it does not work

How could I create a stdinRedirector?

Something like that

class StdinRediector():
    def read():
        input = gui.ask_input()
        return input

subprocess.Popen(
    "command",
    shell="True",
    stdin=StdinRediector,
)

I don't ask for stdout and stderr as there is already answers here:

but these don't talk about stdin which is my problem.

THIS IS NOT DUPLICATE

rafalou38
  • 576
  • 1
  • 5
  • 16
  • You must not define a `fileno()` method for something that isn't actually an OS-level file - anything that calls that method will expect to be able to pass its return value on to system calls that require a valid file handle. Yes, this means that you won't be able to pass your objects directly to `subprocess` functions. What you need to do is pass `subprocess.PIPE` for all three channels, and write code to transfer data between those pipes and your GUI. – jasonharper Jul 06 '20 at 13:21
  • but how can I overite the `read() ` of `subprocess.PIPE` in stdin – rafalou38 Jul 08 '20 at 07:44
  • *overwrite the `read()` of `subprocess.PIPE` in stdin* -- huh? Why would you do that? Just have a thread that reads from the descriptors the completely normal way and updates the GUI when there's new content (reading a line at a time if that's good enough, or a byte at a time if you need more immediacy and can afford the extra overhead). The cheap/easy approach is just a separate thread for each descriptor, though `select`-style tools let you just have one thread that gets pinged when there's content on any of them. – Charles Duffy Nov 13 '20 at 19:08
  • When you assign `stdin` and `stdout`, that doesn't tell Python what to do when there's input or output available. Instead, it tells Python _how to wire up the stdin and stdout_. It has to be a real OS-level file, because only real files can be attached at the `fdup` layer. Hence, if you want it to be a file that can be read or written by Python, `subprocess.PIPE` is your only choice; how you write the code that reads and writes from that pipe is your choice. – Charles Duffy Nov 13 '20 at 19:16
  • https://gist.github.com/zed/9294978 is a useful example taken from a comment on one of the linked duplicates. – Charles Duffy Nov 13 '20 at 22:22
  • yes but it does not show me how to handle stdin and theses answers wich I'm "duplicate" does not talk about stdin wich is the actual problem – rafalou38 Nov 14 '20 at 08:06

0 Answers0