"""Pseudo terminal utilities."""
# Bugs: No signal handling. Doesn't set slave termios and window size.
# See: W. Richard Stevens. 1992. Advanced Programming in the
# UNIX Environment. Chapter 19.
# Author: Steen Lumholt -- with additions by Guido.
from select import select
__all__ = ["openpty","fork","spawn"]
"""openpty() -> (master_fd, slave_fd)
Open a pty master/slave pair, using os.openpty() if possible."""
except (AttributeError, OSError):
master_fd, slave_name = _open_terminal()
slave_fd = slave_open(slave_name)
return master_fd, slave_fd
"""master_open() -> (master_fd, slave_name)
Open a pty master and return the fd, and the filename of the slave end.
Deprecated, use openpty() instead."""
master_fd, slave_fd = os.openpty()
except (AttributeError, OSError):
slave_name = os.ttyname(slave_fd)
return master_fd, slave_name
"""Open pty master and return (master_fd, tty_name)."""
for x in 'pqrstuvwxyzPQRST':
for y in '0123456789abcdef':
pty_name = '/dev/pty' + x + y
fd = os.open(pty_name, os.O_RDWR)
return (fd, '/dev/tty' + x + y)
raise OSError('out of pty devices')
def slave_open(tty_name):
"""slave_open(tty_name) -> slave_fd
Open the pty slave and acquire the controlling terminal, returning
Deprecated, use openpty() instead."""
result = os.open(tty_name, os.O_RDWR)
from fcntl import ioctl, I_PUSH
ioctl(result, I_PUSH, "ptem")
ioctl(result, I_PUSH, "ldterm")
"""fork() -> (pid, master_fd)
Fork and make the child a session leader with a controlling terminal."""
except (AttributeError, OSError):
# os.forkpty() already set us session leader
master_fd, slave_fd = openpty()
# Establish a new session.
# Slave becomes stdin/stdout/stderr of child.
os.dup2(slave_fd, STDIN_FILENO)
os.dup2(slave_fd, STDOUT_FILENO)
os.dup2(slave_fd, STDERR_FILENO)
if (slave_fd > STDERR_FILENO):
# Explicitly open the tty to make it become a controlling tty.
tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
# Parent and child process.
"""Write all the data to a descriptor."""
"""Default read function."""
def _copy(master_fd, master_read=_read, stdin_read=_read):
pty master -> standard output (master_read)
standard input -> pty master (stdin_read)"""
fds = [master_fd, STDIN_FILENO]
rfds, wfds, xfds = select(fds, [], [])
data = master_read(master_fd)
if not data: # Reached EOF.
os.write(STDOUT_FILENO, data)
data = stdin_read(STDIN_FILENO)
def spawn(argv, master_read=_read, stdin_read=_read):
"""Create a spawned process."""
if type(argv) == type(''):
sys.audit('pty.spawn', argv)
os.execlp(argv[0], *argv)
mode = tty.tcgetattr(STDIN_FILENO)
except tty.error: # This is the same as termios.error
_copy(master_fd, master_read, stdin_read)
tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
return os.waitpid(pid, 0)[1]