pywinpty icon indicating copy to clipboard operation
pywinpty copied to clipboard

Allow stdin pass-through

Open ofek opened this issue 5 months ago • 6 comments

I'm not sure if this is a Windows limitation but when I run something like python -c "print(input('Test: '))" the process hangs. Is there a way to allow input? Here is the code I'm using: https://github.com/DataDog/datadog-agent-dev/blob/main/src/dda/utils/platform/_pty/windows.py

ofek avatar Jul 09 '25 18:07 ofek

@ofek, could you please provide a reproducible example? If I spawn a process locally it works as expected:

>>> from winpty import PtyProcess
>>> p = PtyProcess.spawn(['python.exe', '-c', 'print(input("input:"))'])
>>> p.read()
'\x1b[1t\x1b[c\x1b[?1004h\x1b[?9001hinput:'                                                                                 
>>> p.write('aaaaaa')
0
>>> p.read()
'\x1b[1;7Haaaaaa\x1b[1;13H'
>>> p.write('\r\n')
0
>>> p.read()
'\x1b[1;13H\r\naaaaaa\r\n'

andfoy avatar Jul 10 '25 16:07 andfoy

I want the user to be able to type rather than the code controlling the input.

ofek avatar Jul 10 '25 16:07 ofek

But shouldn't the input provided by the user be "written" into the PTY? At least that's how terminado uses pywinpty: https://github.com/jupyter/terminado/blob/5058f34b0921a574a458d00cc2a78f01c029b98e/terminado/websocket.py#L141

andfoy avatar Jul 10 '25 16:07 andfoy

I think handling stdin is outside of the scope of pywinpty, since infrastructure to do stream redirection should be implemented by downstream users?

andfoy avatar Jul 10 '25 16:07 andfoy

Oh okay, so you're saying that I should have a second thread that reads sys.stdin and writes to the PTY? I'm fine with doing that if that's what you suggest but I didn't know if not taking that over is an option. On Linux I just don't do anything to stdin so that the user can type freely when expected: https://github.com/DataDog/datadog-agent-dev/blob/2e33ce1637386a3988d2e6f754ca188f7d14761b/src/dda/utils/platform/_pty/unix.py#L46-L47

ofek avatar Jul 10 '25 16:07 ofek

The issue with stream redirection in Windows is that underneath new pipes are created to communicate from/to the PTY, I don't know if it would be possible to have an async output pipe with a sync one (CONIN$ in this case)

andfoy avatar Jul 10 '25 17:07 andfoy