Back to index

python3.2  3.2.2
Functions | Variables
pty Namespace Reference

Functions

def openpty
def master_open
def _open_terminal
def slave_open
def fork
def _writen
def _read
def _copy
def spawn

Variables

list __all__ = ["openpty","fork","spawn"]
int STDIN_FILENO = 0
int STDOUT_FILENO = 1
int STDERR_FILENO = 2
int CHILD = 0

Detailed Description

Pseudo terminal utilities.

Function Documentation

def pty._copy (   master_fd,
  master_read = _read,
  stdin_read = _read 
) [private]
Parent copy loop.
Copies
        pty master -> standard output   (master_read)
        standard input -> pty master    (stdin_read)

Definition at line 140 of file pty.py.

00140 
00141 def _copy(master_fd, master_read=_read, stdin_read=_read):
00142     """Parent copy loop.
00143     Copies
00144             pty master -> standard output   (master_read)
00145             standard input -> pty master    (stdin_read)"""
00146     while 1:
00147         rfds, wfds, xfds = select(
00148                 [master_fd, STDIN_FILENO], [], [])
00149         if master_fd in rfds:
00150             data = master_read(master_fd)
00151             os.write(STDOUT_FILENO, data)
00152         if STDIN_FILENO in rfds:
00153             data = stdin_read(STDIN_FILENO)
00154             _writen(master_fd, data)

Here is the call graph for this function:

Here is the caller graph for this function:

def pty._open_terminal ( ) [private]
Open pty master and return (master_fd, tty_name).
SGI and generic BSD version, for when openpty() fails.

Definition at line 49 of file pty.py.

00049 
00050 def _open_terminal():
00051     """Open pty master and return (master_fd, tty_name).
00052     SGI and generic BSD version, for when openpty() fails."""
00053     try:
00054         import sgi
00055     except ImportError:
00056         pass
00057     else:
00058         try:
00059             tty_name, master_fd = sgi._getpty(os.O_RDWR, 0o666, 0)
00060         except IOError as msg:
00061             raise os.error(msg)
00062         return master_fd, tty_name
00063     for x in 'pqrstuvwxyzPQRST':
00064         for y in '0123456789abcdef':
00065             pty_name = '/dev/pty' + x + y
00066             try:
00067                 fd = os.open(pty_name, os.O_RDWR)
00068             except os.error:
00069                 continue
00070             return (fd, '/dev/tty' + x + y)
00071     raise os.error('out of pty devices')

Here is the caller graph for this function:

def pty._read (   fd) [private]
Default read function.

Definition at line 136 of file pty.py.

00136 
00137 def _read(fd):
00138     """Default read function."""
00139     return os.read(fd, 1024)

Here is the caller graph for this function:

def pty._writen (   fd,
  data 
) [private]
Write all the data to a descriptor.

Definition at line 130 of file pty.py.

00130 
00131 def _writen(fd, data):
00132     """Write all the data to a descriptor."""
00133     while data:
00134         n = os.write(fd, data)
00135         data = data[n:]

Here is the caller graph for this function:

def pty.fork ( )
fork() -> (pid, master_fd)
Fork and make the child a session leader with a controlling terminal.

Definition at line 90 of file pty.py.

00090 
00091 def fork():
00092     """fork() -> (pid, master_fd)
00093     Fork and make the child a session leader with a controlling terminal."""
00094 
00095     try:
00096         pid, fd = os.forkpty()
00097     except (AttributeError, OSError):
00098         pass
00099     else:
00100         if pid == CHILD:
00101             try:
00102                 os.setsid()
00103             except OSError:
00104                 # os.forkpty() already set us session leader
00105                 pass
00106         return pid, fd
00107 
00108     master_fd, slave_fd = openpty()
00109     pid = os.fork()
00110     if pid == CHILD:
00111         # Establish a new session.
00112         os.setsid()
00113         os.close(master_fd)
00114 
00115         # Slave becomes stdin/stdout/stderr of child.
00116         os.dup2(slave_fd, STDIN_FILENO)
00117         os.dup2(slave_fd, STDOUT_FILENO)
00118         os.dup2(slave_fd, STDERR_FILENO)
00119         if (slave_fd > STDERR_FILENO):
00120             os.close (slave_fd)
00121 
00122         # Explicitly open the tty to make it become a controlling tty.
00123         tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
00124         os.close(tmp_fd)
00125     else:
00126         os.close(slave_fd)
00127 
00128     # Parent and child process.
00129     return pid, master_fd

Here is the call graph for this function:

Here is the caller graph for this function:

def pty.master_open ( )
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.

Definition at line 33 of file pty.py.

00033 
00034 def master_open():
00035     """master_open() -> (master_fd, slave_name)
00036     Open a pty master and return the fd, and the filename of the slave end.
00037     Deprecated, use openpty() instead."""
00038 
00039     try:
00040         master_fd, slave_fd = os.openpty()
00041     except (AttributeError, OSError):
00042         pass
00043     else:
00044         slave_name = os.ttyname(slave_fd)
00045         os.close(slave_fd)
00046         return master_fd, slave_name
00047 
00048     return _open_terminal()

Here is the call graph for this function:

Here is the caller graph for this function:

def pty.openpty ( )
openpty() -> (master_fd, slave_fd)
Open a pty master/slave pair, using os.openpty() if possible.

Definition at line 21 of file pty.py.

00021 
00022 def openpty():
00023     """openpty() -> (master_fd, slave_fd)
00024     Open a pty master/slave pair, using os.openpty() if possible."""
00025 
00026     try:
00027         return os.openpty()
00028     except (AttributeError, OSError):
00029         pass
00030     master_fd, slave_name = _open_terminal()
00031     slave_fd = slave_open(slave_name)
00032     return master_fd, slave_fd

Here is the call graph for this function:

Here is the caller graph for this function:

def pty.slave_open (   tty_name)
slave_open(tty_name) -> slave_fd
Open the pty slave and acquire the controlling terminal, returning
opened filedescriptor.
Deprecated, use openpty() instead.

Definition at line 72 of file pty.py.

00072 
00073 def slave_open(tty_name):
00074     """slave_open(tty_name) -> slave_fd
00075     Open the pty slave and acquire the controlling terminal, returning
00076     opened filedescriptor.
00077     Deprecated, use openpty() instead."""
00078 
00079     result = os.open(tty_name, os.O_RDWR)
00080     try:
00081         from fcntl import ioctl, I_PUSH
00082     except ImportError:
00083         return result
00084     try:
00085         ioctl(result, I_PUSH, "ptem")
00086         ioctl(result, I_PUSH, "ldterm")
00087     except IOError:
00088         pass
00089     return result

Here is the caller graph for this function:

def pty.spawn (   argv,
  master_read = _read,
  stdin_read = _read 
)
Create a spawned process.

Definition at line 155 of file pty.py.

00155 
00156 def spawn(argv, master_read=_read, stdin_read=_read):
00157     """Create a spawned process."""
00158     if type(argv) == type(''):
00159         argv = (argv,)
00160     pid, master_fd = fork()
00161     if pid == CHILD:
00162         os.execlp(argv[0], *argv)
00163     try:
00164         mode = tty.tcgetattr(STDIN_FILENO)
00165         tty.setraw(STDIN_FILENO)
00166         restore = 1
00167     except tty.error:    # This is the same as termios.error
00168         restore = 0
00169     try:
00170         _copy(master_fd, master_read, stdin_read)
00171     except (IOError, OSError):
00172         if restore:
00173             tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
00174 
00175     os.close(master_fd)

Here is the call graph for this function:


Variable Documentation

list pty.__all__ = ["openpty","fork","spawn"]

Definition at line 13 of file pty.py.

Definition at line 19 of file pty.py.

Definition at line 17 of file pty.py.

Definition at line 15 of file pty.py.

Definition at line 16 of file pty.py.