Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: pty.py
"""Pseudo terminal utilities."""
[0] Fix | Delete
[1] Fix | Delete
# Bugs: No signal handling. Doesn't set slave termios and window size.
[2] Fix | Delete
# Only tested on Linux.
[3] Fix | Delete
# See: W. Richard Stevens. 1992. Advanced Programming in the
[4] Fix | Delete
# UNIX Environment. Chapter 19.
[5] Fix | Delete
# Author: Steen Lumholt -- with additions by Guido.
[6] Fix | Delete
[7] Fix | Delete
from select import select
[8] Fix | Delete
import os
[9] Fix | Delete
import tty
[10] Fix | Delete
[11] Fix | Delete
__all__ = ["openpty","fork","spawn"]
[12] Fix | Delete
[13] Fix | Delete
STDIN_FILENO = 0
[14] Fix | Delete
STDOUT_FILENO = 1
[15] Fix | Delete
STDERR_FILENO = 2
[16] Fix | Delete
[17] Fix | Delete
CHILD = 0
[18] Fix | Delete
[19] Fix | Delete
def openpty():
[20] Fix | Delete
"""openpty() -> (master_fd, slave_fd)
[21] Fix | Delete
Open a pty master/slave pair, using os.openpty() if possible."""
[22] Fix | Delete
[23] Fix | Delete
try:
[24] Fix | Delete
return os.openpty()
[25] Fix | Delete
except (AttributeError, OSError):
[26] Fix | Delete
pass
[27] Fix | Delete
master_fd, slave_name = _open_terminal()
[28] Fix | Delete
slave_fd = slave_open(slave_name)
[29] Fix | Delete
return master_fd, slave_fd
[30] Fix | Delete
[31] Fix | Delete
def master_open():
[32] Fix | Delete
"""master_open() -> (master_fd, slave_name)
[33] Fix | Delete
Open a pty master and return the fd, and the filename of the slave end.
[34] Fix | Delete
Deprecated, use openpty() instead."""
[35] Fix | Delete
[36] Fix | Delete
try:
[37] Fix | Delete
master_fd, slave_fd = os.openpty()
[38] Fix | Delete
except (AttributeError, OSError):
[39] Fix | Delete
pass
[40] Fix | Delete
else:
[41] Fix | Delete
slave_name = os.ttyname(slave_fd)
[42] Fix | Delete
os.close(slave_fd)
[43] Fix | Delete
return master_fd, slave_name
[44] Fix | Delete
[45] Fix | Delete
return _open_terminal()
[46] Fix | Delete
[47] Fix | Delete
def _open_terminal():
[48] Fix | Delete
"""Open pty master and return (master_fd, tty_name).
[49] Fix | Delete
SGI and generic BSD version, for when openpty() fails."""
[50] Fix | Delete
try:
[51] Fix | Delete
import sgi
[52] Fix | Delete
except ImportError:
[53] Fix | Delete
pass
[54] Fix | Delete
else:
[55] Fix | Delete
try:
[56] Fix | Delete
tty_name, master_fd = sgi._getpty(os.O_RDWR, 0666, 0)
[57] Fix | Delete
except IOError, msg:
[58] Fix | Delete
raise os.error, msg
[59] Fix | Delete
return master_fd, tty_name
[60] Fix | Delete
for x in 'pqrstuvwxyzPQRST':
[61] Fix | Delete
for y in '0123456789abcdef':
[62] Fix | Delete
pty_name = '/dev/pty' + x + y
[63] Fix | Delete
try:
[64] Fix | Delete
fd = os.open(pty_name, os.O_RDWR)
[65] Fix | Delete
except os.error:
[66] Fix | Delete
continue
[67] Fix | Delete
return (fd, '/dev/tty' + x + y)
[68] Fix | Delete
raise os.error, 'out of pty devices'
[69] Fix | Delete
[70] Fix | Delete
def slave_open(tty_name):
[71] Fix | Delete
"""slave_open(tty_name) -> slave_fd
[72] Fix | Delete
Open the pty slave and acquire the controlling terminal, returning
[73] Fix | Delete
opened filedescriptor.
[74] Fix | Delete
Deprecated, use openpty() instead."""
[75] Fix | Delete
[76] Fix | Delete
result = os.open(tty_name, os.O_RDWR)
[77] Fix | Delete
try:
[78] Fix | Delete
from fcntl import ioctl, I_PUSH
[79] Fix | Delete
except ImportError:
[80] Fix | Delete
return result
[81] Fix | Delete
try:
[82] Fix | Delete
ioctl(result, I_PUSH, "ptem")
[83] Fix | Delete
ioctl(result, I_PUSH, "ldterm")
[84] Fix | Delete
except IOError:
[85] Fix | Delete
pass
[86] Fix | Delete
return result
[87] Fix | Delete
[88] Fix | Delete
def fork():
[89] Fix | Delete
"""fork() -> (pid, master_fd)
[90] Fix | Delete
Fork and make the child a session leader with a controlling terminal."""
[91] Fix | Delete
[92] Fix | Delete
try:
[93] Fix | Delete
pid, fd = os.forkpty()
[94] Fix | Delete
except (AttributeError, OSError):
[95] Fix | Delete
pass
[96] Fix | Delete
else:
[97] Fix | Delete
if pid == CHILD:
[98] Fix | Delete
try:
[99] Fix | Delete
os.setsid()
[100] Fix | Delete
except OSError:
[101] Fix | Delete
# os.forkpty() already set us session leader
[102] Fix | Delete
pass
[103] Fix | Delete
return pid, fd
[104] Fix | Delete
[105] Fix | Delete
master_fd, slave_fd = openpty()
[106] Fix | Delete
pid = os.fork()
[107] Fix | Delete
if pid == CHILD:
[108] Fix | Delete
# Establish a new session.
[109] Fix | Delete
os.setsid()
[110] Fix | Delete
os.close(master_fd)
[111] Fix | Delete
[112] Fix | Delete
# Slave becomes stdin/stdout/stderr of child.
[113] Fix | Delete
os.dup2(slave_fd, STDIN_FILENO)
[114] Fix | Delete
os.dup2(slave_fd, STDOUT_FILENO)
[115] Fix | Delete
os.dup2(slave_fd, STDERR_FILENO)
[116] Fix | Delete
if (slave_fd > STDERR_FILENO):
[117] Fix | Delete
os.close (slave_fd)
[118] Fix | Delete
[119] Fix | Delete
# Explicitly open the tty to make it become a controlling tty.
[120] Fix | Delete
tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
[121] Fix | Delete
os.close(tmp_fd)
[122] Fix | Delete
else:
[123] Fix | Delete
os.close(slave_fd)
[124] Fix | Delete
[125] Fix | Delete
# Parent and child process.
[126] Fix | Delete
return pid, master_fd
[127] Fix | Delete
[128] Fix | Delete
def _writen(fd, data):
[129] Fix | Delete
"""Write all the data to a descriptor."""
[130] Fix | Delete
while data != '':
[131] Fix | Delete
n = os.write(fd, data)
[132] Fix | Delete
data = data[n:]
[133] Fix | Delete
[134] Fix | Delete
def _read(fd):
[135] Fix | Delete
"""Default read function."""
[136] Fix | Delete
return os.read(fd, 1024)
[137] Fix | Delete
[138] Fix | Delete
def _copy(master_fd, master_read=_read, stdin_read=_read):
[139] Fix | Delete
"""Parent copy loop.
[140] Fix | Delete
Copies
[141] Fix | Delete
pty master -> standard output (master_read)
[142] Fix | Delete
standard input -> pty master (stdin_read)"""
[143] Fix | Delete
fds = [master_fd, STDIN_FILENO]
[144] Fix | Delete
while True:
[145] Fix | Delete
rfds, wfds, xfds = select(fds, [], [])
[146] Fix | Delete
if master_fd in rfds:
[147] Fix | Delete
data = master_read(master_fd)
[148] Fix | Delete
if not data: # Reached EOF.
[149] Fix | Delete
fds.remove(master_fd)
[150] Fix | Delete
else:
[151] Fix | Delete
os.write(STDOUT_FILENO, data)
[152] Fix | Delete
if STDIN_FILENO in rfds:
[153] Fix | Delete
data = stdin_read(STDIN_FILENO)
[154] Fix | Delete
if not data:
[155] Fix | Delete
fds.remove(STDIN_FILENO)
[156] Fix | Delete
else:
[157] Fix | Delete
_writen(master_fd, data)
[158] Fix | Delete
[159] Fix | Delete
def spawn(argv, master_read=_read, stdin_read=_read):
[160] Fix | Delete
"""Create a spawned process."""
[161] Fix | Delete
if type(argv) == type(''):
[162] Fix | Delete
argv = (argv,)
[163] Fix | Delete
pid, master_fd = fork()
[164] Fix | Delete
if pid == CHILD:
[165] Fix | Delete
os.execlp(argv[0], *argv)
[166] Fix | Delete
try:
[167] Fix | Delete
mode = tty.tcgetattr(STDIN_FILENO)
[168] Fix | Delete
tty.setraw(STDIN_FILENO)
[169] Fix | Delete
restore = 1
[170] Fix | Delete
except tty.error: # This is the same as termios.error
[171] Fix | Delete
restore = 0
[172] Fix | Delete
try:
[173] Fix | Delete
_copy(master_fd, master_read, stdin_read)
[174] Fix | Delete
except (IOError, OSError):
[175] Fix | Delete
if restore:
[176] Fix | Delete
tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
[177] Fix | Delete
[178] Fix | Delete
os.close(master_fd)
[179] Fix | Delete
[180] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function