PATH:
usr
/
lib64
/
python3.6
"""Pseudo terminal utilities.""" # Bugs: No signal handling. Doesn't set slave termios and window size. # Only tested on Linux. # See: W. Richard Stevens. 1992. Advanced Programming in the # UNIX Environment. Chapter 19. # Author: Steen Lumholt -- with additions by Guido. from select import select import os import tty __all__ = ["openpty","fork","spawn"] STDIN_FILENO = 0 STDOUT_FILENO = 1 STDERR_FILENO = 2 CHILD = 0 def openpty(): """openpty() -> (master_fd, slave_fd) Open a pty master/slave pair, using os.openpty() if possible.""" try: return os.openpty() except (AttributeError, OSError): pass master_fd, slave_name = _open_terminal() slave_fd = slave_open(slave_name) return master_fd, slave_fd def 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.""" try: master_fd, slave_fd = os.openpty() except (AttributeError, OSError): pass else: slave_name = os.ttyname(slave_fd) os.close(slave_fd) return master_fd, slave_name return _open_terminal() def _open_terminal(): """Open pty master and return (master_fd, tty_name).""" for x in 'pqrstuvwxyzPQRST': for y in '0123456789abcdef': pty_name = '/dev/pty' + x + y try: fd = os.open(pty_name, os.O_RDWR) except OSError: continue 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 opened filedescriptor. Deprecated, use openpty() instead.""" result = os.open(tty_name, os.O_RDWR) try: from fcntl import ioctl, I_PUSH except ImportError: return result try: ioctl(result, I_PUSH, "ptem") ioctl(result, I_PUSH, "ldterm") except OSError: pass return result def fork(): """fork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.""" try: pid, fd = os.forkpty() except (AttributeError, OSError): pass else: if pid == CHILD: try: os.setsid() except OSError: # os.forkpty() already set us session leader pass return pid, fd master_fd, slave_fd = openpty() pid = os.fork() if pid == CHILD: # Establish a new session. os.setsid() os.close(master_fd) # 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): os.close (slave_fd) # Explicitly open the tty to make it become a controlling tty. tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) os.close(tmp_fd) else: os.close(slave_fd) # Parent and child process. return pid, master_fd def _writen(fd, data): """Write all the data to a descriptor.""" while data: n = os.write(fd, data) data = data[n:] def _read(fd): """Default read function.""" return os.read(fd, 1024) def _copy(master_fd, master_read=_read, stdin_read=_read): """Parent copy loop. Copies pty master -> standard output (master_read) standard input -> pty master (stdin_read)""" fds = [master_fd, STDIN_FILENO] while True: rfds, wfds, xfds = select(fds, [], []) if master_fd in rfds: data = master_read(master_fd) if not data: # Reached EOF. fds.remove(master_fd) else: os.write(STDOUT_FILENO, data) if STDIN_FILENO in rfds: data = stdin_read(STDIN_FILENO) if not data: fds.remove(STDIN_FILENO) else: _writen(master_fd, data) def spawn(argv, master_read=_read, stdin_read=_read): """Create a spawned process.""" if type(argv) == type(''): argv = (argv,) pid, master_fd = fork() if pid == CHILD: os.execlp(argv[0], *argv) try: mode = tty.tcgetattr(STDIN_FILENO) tty.setraw(STDIN_FILENO) restore = 1 except tty.error: # This is the same as termios.error restore = 0 try: _copy(master_fd, master_read, stdin_read) except OSError: if restore: tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) os.close(master_fd) return os.waitpid(pid, 0)[1]
[+]
..
[-] __phello__.foo.py
[edit]
[-] zipfile.py
[edit]
[-] _dummy_thread.py
[edit]
[-] signal.py
[edit]
[-] tabnanny.py
[edit]
[-] cmd.py
[edit]
[-] cProfile.py
[edit]
[-] tempfile.py
[edit]
[-] argparse.py
[edit]
[-] platform.py
[edit]
[-] binhex.py
[edit]
[-] antigravity.py
[edit]
[+]
curses
[-] _compat_pickle.py
[edit]
[-] statistics.py
[edit]
[-] inspect.py
[edit]
[-] ipaddress.py
[edit]
[-] sunau.py
[edit]
[-] imghdr.py
[edit]
[-] random.py
[edit]
[-] macpath.py
[edit]
[-] hmac.py
[edit]
[-] base64.py
[edit]
[-] cgitb.py
[edit]
[-] stringprep.py
[edit]
[-] ast.py
[edit]
[-] textwrap.py
[edit]
[-] enum.py
[edit]
[-] _compression.py
[edit]
[-] dis.py
[edit]
[+]
venv
[-] chunk.py
[edit]
[-] this.py
[edit]
[-] py_compile.py
[edit]
[-] fnmatch.py
[edit]
[+]
xml
[-] opcode.py
[edit]
[+]
site-packages
[-] sre_compile.py
[edit]
[-] _pyio.py
[edit]
[-] pty.py
[edit]
[-] datetime.py
[edit]
[-] macurl2path.py
[edit]
[+]
lib2to3
[+]
wsgiref
[-] heapq.py
[edit]
[-] keyword.py
[edit]
[-] codecs.py
[edit]
[+]
ensurepip
[-] pickletools.py
[edit]
[-] compileall.py
[edit]
[-] nturl2path.py
[edit]
[-] warnings.py
[edit]
[-] shutil.py
[edit]
[-] socket.py
[edit]
[-] pdb.py
[edit]
[-] zipapp.py
[edit]
[-] formatter.py
[edit]
[-] _sitebuiltins.py
[edit]
[-] _markupbase.py
[edit]
[-] types.py
[edit]
[-] runpy.py
[edit]
[-] uu.py
[edit]
[-] typing.py
[edit]
[-] pathlib.py
[edit]
[-] _threading_local.py
[edit]
[-] mailcap.py
[edit]
[-] bisect.py
[edit]
[-] linecache.py
[edit]
[-] colorsys.py
[edit]
[-] glob.py
[edit]
[-] sndhdr.py
[edit]
[-] ssl.py
[edit]
[-] _strptime.py
[edit]
[-] _sysconfigdata_m_linux_x86_64-linux-gnu.py
[edit]
[-] imp.py
[edit]
[-] contextlib.py
[edit]
[-] copyreg.py
[edit]
[-] filecmp.py
[edit]
[+]
logging
[-] operator.py
[edit]
[-] token.py
[edit]
[-] gzip.py
[edit]
[-] string.py
[edit]
[-] genericpath.py
[edit]
[-] _osx_support.py
[edit]
[-] stat.py
[edit]
[-] lzma.py
[edit]
[-] __future__.py
[edit]
[+]
distutils
[-] dummy_threading.py
[edit]
[+]
asyncio
[-] webbrowser.py
[edit]
[-] symtable.py
[edit]
[+]
lib-dynload
[-] getopt.py
[edit]
[-] wave.py
[edit]
[-] codeop.py
[edit]
[-] _sysconfigdata_dm_linux_x86_64-linux-gnu.py
[edit]
[-] abc.py
[edit]
[+]
config-3.6m-x86_64-linux-gnu
[+]
pydoc_data
[-] telnetlib.py
[edit]
[+]
unittest
[-] fileinput.py
[edit]
[-] ftplib.py
[edit]
[+]
ctypes
[-] _bootlocale.py
[edit]
[-] pipes.py
[edit]
[-] aifc.py
[edit]
[-] pprint.py
[edit]
[-] queue.py
[edit]
[-] difflib.py
[edit]
[+]
__pycache__
[-] uuid.py
[edit]
[-] reprlib.py
[edit]
[-] threading.py
[edit]
[+]
http
[-] sre_parse.py
[edit]
[-] subprocess.py
[edit]
[+]
importlib
[-] selectors.py
[edit]
[-] getpass.py
[edit]
[-] trace.py
[edit]
[-] struct.py
[edit]
[-] tracemalloc.py
[edit]
[-] plistlib.py
[edit]
[-] asynchat.py
[edit]
[-] fractions.py
[edit]
[-] shlex.py
[edit]
[-] bdb.py
[edit]
[-] locale.py
[edit]
[-] calendar.py
[edit]
[-] poplib.py
[edit]
[-] posixpath.py
[edit]
[-] imaplib.py
[edit]
[+]
concurrent
[-] pickle.py
[edit]
[-] timeit.py
[edit]
[+]
json
[-] traceback.py
[edit]
[+]
multiprocessing
[-] shelve.py
[edit]
[-] csv.py
[edit]
[-] tarfile.py
[edit]
[-] numbers.py
[edit]
[-] optparse.py
[edit]
[-] cgi.py
[edit]
[-] pstats.py
[edit]
[-] crypt.py
[edit]
[+]
collections
[-] site.py
[edit]
[-] pkgutil.py
[edit]
[-] re.py
[edit]
[-] pyclbr.py
[edit]
[-] socketserver.py
[edit]
[-] _pydecimal.py
[edit]
[-] pydoc.py
[edit]
[-] sre_constants.py
[edit]
[-] xdrlib.py
[edit]
[-] nntplib.py
[edit]
[-] mimetypes.py
[edit]
[+]
email
[-] quopri.py
[edit]
[-] doctest.py
[edit]
[-] profile.py
[edit]
[-] sched.py
[edit]
[-] hashlib.py
[edit]
[+]
urllib
[-] secrets.py
[edit]
[-] functools.py
[edit]
[-] smtplib.py
[edit]
[-] copy.py
[edit]
[+]
dbm
[-] tty.py
[edit]
[-] os.py
[edit]
[-] _collections_abc.py
[edit]
[-] configparser.py
[edit]
[-] _weakrefset.py
[edit]
[-] netrc.py
[edit]
[-] code.py
[edit]
[-] asyncore.py
[edit]
[-] tokenize.py
[edit]
[+]
encodings
[-] io.py
[edit]
[-] smtpd.py
[edit]
[-] sysconfig.py
[edit]
[-] decimal.py
[edit]
[+]
sqlite3
[-] rlcompleter.py
[edit]
[-] ntpath.py
[edit]
[+]
test
[-] weakref.py
[edit]
[-] symbol.py
[edit]
[-] modulefinder.py
[edit]
[+]
xmlrpc
[-] bz2.py
[edit]
[-] gettext.py
[edit]
[-] mailbox.py
[edit]
[+]
html