Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 132 additions & 2 deletions 134 Lib/pty.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Pseudo terminal utilities."""

# Bugs: No signal handling. Doesn't set slave termios and window size.
# Bugs: No signal handling. Doesn't set slave termios.
# Only tested on Linux.
# See: W. Richard Stevens. 1992. Advanced Programming in the
# UNIX Environment. Chapter 19.
Expand All @@ -10,8 +10,13 @@
import os
import sys
import tty
import signal
import struct
import fcntl
import termios
import time

__all__ = ["openpty","fork","spawn"]
__all__ = ["openpty","fork","spawn","wspawn"]

STDIN_FILENO = 0
STDOUT_FILENO = 1
Expand Down Expand Up @@ -170,3 +175,128 @@ def spawn(argv, master_read=_read, stdin_read=_read):

os.close(master_fd)
return os.waitpid(pid, 0)[1]

@8vasu 8vasu Aug 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding detailed comments for the reviewers. Most manpages referenced are from Linux. However, they should be similar on the BSDs.

Bugs:

  1. Since signal.signal() is being called by wspawn(), we can only call wspawn from the main thread. Even if that was not the case, since wspawn is setting the SIGWINCH handler globally, we should only have one instance of wspawn() running across threads anyway.
  2. wspawn() currently raises an exception if the initial attempt to set window size fails / if the attempt to register the SIGWINCH handler fails. Possible workaround: catch exception and try pty.spawn() instead.
  3. Can calling select with very short timeout result in high CPU usage? Not tested.

# Author: Soumendra Ganguly.
SIGWINCH = signal.SIGWINCH

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def _login_pty(master_fd, slave_fd):
"""Given a pty, makes the calling process a session leader, makes the pty slave
its controlling terminal, stdin, stdout, and stderr. Closes both pty ends."""
# Establish a new session.
os.setsid()
os.close(master_fd)

try:
fcntl.ioctl(slave_fd, termios.TIOCSCTTY) # Make the pty slave the controlling terminal.
except:
os.close(slave_fd)
raise

# Slave becomes stdin/stdout/stderr.
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)

@8vasu 8vasu Aug 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C code equivalent to "_winresz" is

void _winresz(int pty_slave) {
        struct winsize w;
        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &w) == 0)
                ioctl(pty_slave, TIOCSWINSZ, &w);
}

Notice that the above code checks if the TIOCGWINSZ call is a success before
making the TIOCSWINSZ call. Therefore, "_winresz" must always be called
in a try block.

References:

  1. https://www.man7.org/linux/man-pages/man4/tty_ioctl.4.html / https://docs.python.org/3/library/fcntl.html?highlight=ioctl#fcntl.ioctl

def _winresz(pty_slave):
"""Resize window."""
w = struct.pack('HHHH', 0, 0, 0, 0)
s = fcntl.ioctl(STDIN_FILENO, termios.TIOCGWINSZ, w)
fcntl.ioctl(pty_slave, termios.TIOCSWINSZ, s)

@8vasu 8vasu Aug 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SIGWINCH handler depends on the pty slave fd returned by openpty in the body of "wspawn" [ see below ]; while the handler could just have been a function nested inside wspawn, that would make wspawn less readable. That is the reason for writing "_create_hwinch", which simply takes the pty slave fd as an argument and returns the appropriate signal handler function [ for SIGWINCH ].

References:

  1. https://man7.org/linux/man-pages/man3/openpty.3.html / https://docs.python.org/3/library/pty.html?highlight=openpty#pty.openpty

def _create_hwinch(pty_slave):
"""Creates SIGWINCH handler."""
def _hwinch(signum, frame):
try:
_winresz(pty_slave)
except:
pass
return _hwinch

@8vasu 8vasu Aug 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C code equivalent to "_cleanup" would use close(2), tcsetattr(3), and sigaction(2). This performs cleanup such as closing files, resetting tty attributes, and resetting the SIGWINCH signal handler. It is called right before wspawn returns/raises an exception.

References:

  1. https://man7.org/linux/man-pages/man2/close.2.html / https://docs.python.org/3/library/os.html?highlight=os%20close#os.close
  2. https://man7.org/linux/man-pages/man3/termios.3.html / https://docs.python.org/3/library/termios.html
  3. https://man7.org/linux/man-pages/man2/sigaction.2.html / https://docs.python.org/3/library/signal.html#signal.signal

def _cleanup(master_fd, slave_fd, old_hwinch, tty_mode):
"""Performs cleanup in wspawn."""

# Close both pty ends.
os.close(master_fd)
os.close(slave_fd)

# Restore original tty attributes.
if tty_mode != None:
tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, tty_mode)

# Restore original SIGWINCH signal handler.
try:
signal.signal(SIGWINCH, old_hwinch)
except:
pass

@8vasu 8vasu Aug 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph explains "_ekill". If the parent loop [ see "_wcopy" below ] terminates due to an exception, then that exception is caught by wspawn. Then, we call waitpid on spawned child process so that it does not become a zombie. However, right before calling waitpid, the child process is sent SIGTERM. A second (*) after that, the child process, if alive, receives SIGKILL to ensure that it is no longer running.

(*) Will an adjustable sleep be more useful?

References:

  1. https://en.wikipedia.org/wiki/Zombie_process
  2. https://man7.org/linux/man-pages/man2/wait.2.html / https://docs.python.org/3/library/os.html?highlight=waitpid#os.waitpid
  3. https://man7.org/linux/man-pages/man2/kill.2.html / https://docs.python.org/3/library/os.html?highlight=kill#os.kill

def _ekill(child_pid):
"""Kill spawned process due to exception."""
try:
os.kill(child_pid, signal.SIGTERM)
time.sleep(1)
os.kill(child_pid, signal.SIGKILL)
except:
pass
try:
os.waitpid(child_pid, 0)
except:
pass

@8vasu 8vasu Aug 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a modified version of "_copy". The differences are:

  1. select is called with a finite, adjustable timeout value.
  2. Calls waitpid [ with WNOHANG ] on the spawned child process periodically.
  3. While _copy does not return anything, _wcopy cleanly exits and returns the output of os.waitpid.

References:

  1. https://man7.org/linux/man-pages/man2/select.2.html / https://docs.python.org/3/library/select.html?highlight=select#select.select

def _wcopy(master_fd, child_pid, master_read=_read, stdin_read=_read, timeout=0.01):
"""Parent copy loop for wspawn."""
fds = [master_fd, STDIN_FILENO]
ret = (0,0)
while True:
rfds, wfds, xfds = select(fds, [], [], timeout)
if ret == (0,0):
ret = os.waitpid(child_pid, os.WNOHANG)
elif len(rfds) == 0:
break;
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)
return ret

@8vasu 8vasu Aug 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"wspawn" is spawn+the following differences.

  1. It sets window size at the beginning.
  2. It registers a SIGWINCH handler.
  3. It does not depend on OSError to return. It does a clean return instead [ see "_wcopy" above ].

No. 3 above is related to

  1. https://bugs.python.org/issue29070
  2. https://bugs.python.org/issue26228

def wspawn(argv, master_read=_read, stdin_read=_read, timeout=0.01):
"""Create a spawned process with
terminal window resizing enabled."""
if type(argv) == type(''):
argv = (argv,)
sys.audit('pty.wspawn', argv)
bk_hwinch = signal.getsignal(SIGWINCH)
master_fd, slave_fd = openpty()
try:
_winresz(slave_fd)
signal.signal(SIGWINCH, _create_hwinch(slave_fd))
except: # User should handle exception and try spawn instead.
_cleanup(master_fd, slave_fd, bk_hwinch, None)
raise
pid = os.fork()
if pid == CHILD:
_login_pty(master_fd, slave_fd)
os.execlp(argv[0], *argv)
try:
mode = tty.tcgetattr(STDIN_FILENO)
tty.setraw(STDIN_FILENO)
except tty.error: # This is the same as termios.error
mode = None
try:
ret = _wcopy(master_fd, pid, master_read, stdin_read, timeout)[1]
except:
_ekill(pid)
_cleanup(master_fd, slave_fd, bk_hwinch, mode)
raise

_cleanup(master_fd, slave_fd, bk_hwinch, mode)
return ret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adds pty.wspawn, which is like pty.spawn + window resizing support [ handles SIGWINCH ].
Morty Proxy This is a proxified and sanitized view of the page, visit original site.