Pseudo-TTY (pty module) help!

Noah noah at noah.org
Sun Apr 14 14:44:09 EDT 2002


Does anyone have any experience or samples of use of the pty module?

What I would like to do is fork a child that echos back every
character, but converted to upper-case. The parent would write
out characters then read back and print the results on STDOUT.

I tried using pty.fork. Here is the behavior that I observed.
I successfully get a Master (parent) and Slave (child).
The Master can read and write, but everything it writes comes
back in the read. The Slave can write, but blocks on all reads.
1. The Master gets a file descriptor (fd) for communication with the Slave.
2. The Master can write data to the fd.
3. The Slave can write to its STDOUT.
4. The Master can read the data on fd written by the Slave.
5. The Master alread reads all the data on fd that it wrote.
    it appears to be echoing back, but not from the slave.
    Even if I disable the slave from any writes, the Master's
    data still enchos back.
6. The Slave cannot read from STDIN at all. It blocks.

I use os.write and os.read for all file descriptor operations.
For the slave I use os.write(sys.stdout.fileno(), foo) which I
think is the most correct way.

I have been experimenting with pty, but to no avail.
The documentation could only be briefer if it was absent.
The test_pty.py that comes with the Python installation
also does not fully test all the features, so it is hard
to use that as starter code.

Maybe I could try pty.spwan, but it isn't clear how to use it.
I'm not quite sure how to use the master_read and stdin_read
callback functions.

Yours,
Noah






More information about the Python-list mailing list