Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3....
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
for x in 'pqrstuvwxyzPQRST':
[50] Fix | Delete
for y in '0123456789abcdef':
[51] Fix | Delete
pty_name = '/dev/pty' + x + y
[52] Fix | Delete
try:
[53] Fix | Delete
fd = os.open(pty_name, os.O_RDWR)
[54] Fix | Delete
except OSError:
[55] Fix | Delete
continue
[56] Fix | Delete
return (fd, '/dev/tty' + x + y)
[57] Fix | Delete
raise OSError('out of pty devices')
[58] Fix | Delete
[59] Fix | Delete
def slave_open(tty_name):
[60] Fix | Delete
"""slave_open(tty_name) -> slave_fd
[61] Fix | Delete
Open the pty slave and acquire the controlling terminal, returning
[62] Fix | Delete
opened filedescriptor.
[63] Fix | Delete
Deprecated, use openpty() instead."""
[64] Fix | Delete
[65] Fix | Delete
result = os.open(tty_name, os.O_RDWR)
[66] Fix | Delete
try:
[67] Fix | Delete
from fcntl import ioctl, I_PUSH
[68] Fix | Delete
except ImportError:
[69] Fix | Delete
return result
[70] Fix | Delete
try:
[71] Fix | Delete
ioctl(result, I_PUSH, "ptem")
[72] Fix | Delete
ioctl(result, I_PUSH, "ldterm")
[73] Fix | Delete
except OSError:
[74] Fix | Delete
pass
[75] Fix | Delete
return result
[76] Fix | Delete
[77] Fix | Delete
def fork():
[78] Fix | Delete
"""fork() -> (pid, master_fd)
[79] Fix | Delete
Fork and make the child a session leader with a controlling terminal."""
[80] Fix | Delete
[81] Fix | Delete
try:
[82] Fix | Delete
pid, fd = os.forkpty()
[83] Fix | Delete
except (AttributeError, OSError):
[84] Fix | Delete
pass
[85] Fix | Delete
else:
[86] Fix | Delete
if pid == CHILD:
[87] Fix | Delete
try:
[88] Fix | Delete
os.setsid()
[89] Fix | Delete
except OSError:
[90] Fix | Delete
# os.forkpty() already set us session leader
[91] Fix | Delete
pass
[92] Fix | Delete
return pid, fd
[93] Fix | Delete
[94] Fix | Delete
master_fd, slave_fd = openpty()
[95] Fix | Delete
pid = os.fork()
[96] Fix | Delete
if pid == CHILD:
[97] Fix | Delete
# Establish a new session.
[98] Fix | Delete
os.setsid()
[99] Fix | Delete
os.close(master_fd)
[100] Fix | Delete
[101] Fix | Delete
# Slave becomes stdin/stdout/stderr of child.
[102] Fix | Delete
os.dup2(slave_fd, STDIN_FILENO)
[103] Fix | Delete
os.dup2(slave_fd, STDOUT_FILENO)
[104] Fix | Delete
os.dup2(slave_fd, STDERR_FILENO)
[105] Fix | Delete
if (slave_fd > STDERR_FILENO):
[106] Fix | Delete
os.close (slave_fd)
[107] Fix | Delete
[108] Fix | Delete
# Explicitly open the tty to make it become a controlling tty.
[109] Fix | Delete
tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
[110] Fix | Delete
os.close(tmp_fd)
[111] Fix | Delete
else:
[112] Fix | Delete
os.close(slave_fd)
[113] Fix | Delete
[114] Fix | Delete
# Parent and child process.
[115] Fix | Delete
return pid, master_fd
[116] Fix | Delete
[117] Fix | Delete
def _writen(fd, data):
[118] Fix | Delete
"""Write all the data to a descriptor."""
[119] Fix | Delete
while data:
[120] Fix | Delete
n = os.write(fd, data)
[121] Fix | Delete
data = data[n:]
[122] Fix | Delete
[123] Fix | Delete
def _read(fd):
[124] Fix | Delete
"""Default read function."""
[125] Fix | Delete
return os.read(fd, 1024)
[126] Fix | Delete
[127] Fix | Delete
def _copy(master_fd, master_read=_read, stdin_read=_read):
[128] Fix | Delete
"""Parent copy loop.
[129] Fix | Delete
Copies
[130] Fix | Delete
pty master -> standard output (master_read)
[131] Fix | Delete
standard input -> pty master (stdin_read)"""
[132] Fix | Delete
fds = [master_fd, STDIN_FILENO]
[133] Fix | Delete
while True:
[134] Fix | Delete
rfds, wfds, xfds = select(fds, [], [])
[135] Fix | Delete
if master_fd in rfds:
[136] Fix | Delete
data = master_read(master_fd)
[137] Fix | Delete
if not data: # Reached EOF.
[138] Fix | Delete
fds.remove(master_fd)
[139] Fix | Delete
else:
[140] Fix | Delete
os.write(STDOUT_FILENO, data)
[141] Fix | Delete
if STDIN_FILENO in rfds:
[142] Fix | Delete
data = stdin_read(STDIN_FILENO)
[143] Fix | Delete
if not data:
[144] Fix | Delete
fds.remove(STDIN_FILENO)
[145] Fix | Delete
else:
[146] Fix | Delete
_writen(master_fd, data)
[147] Fix | Delete
[148] Fix | Delete
def spawn(argv, master_read=_read, stdin_read=_read):
[149] Fix | Delete
"""Create a spawned process."""
[150] Fix | Delete
if type(argv) == type(''):
[151] Fix | Delete
argv = (argv,)
[152] Fix | Delete
pid, master_fd = fork()
[153] Fix | Delete
if pid == CHILD:
[154] Fix | Delete
os.execlp(argv[0], *argv)
[155] Fix | Delete
try:
[156] Fix | Delete
mode = tty.tcgetattr(STDIN_FILENO)
[157] Fix | Delete
tty.setraw(STDIN_FILENO)
[158] Fix | Delete
restore = 1
[159] Fix | Delete
except tty.error: # This is the same as termios.error
[160] Fix | Delete
restore = 0
[161] Fix | Delete
try:
[162] Fix | Delete
_copy(master_fd, master_read, stdin_read)
[163] Fix | Delete
except OSError:
[164] Fix | Delete
if restore:
[165] Fix | Delete
tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
[166] Fix | Delete
[167] Fix | Delete
os.close(master_fd)
[168] Fix | Delete
return os.waitpid(pid, 0)[1]
[169] Fix | Delete
[170] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function