Skip to content

bpo-41541: Make pty.spawn set window size #21861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
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
60 changes: 46 additions & 14 deletions Lib/pty.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ def slave_open(tty_name):
pass
return result

def login_tty(fd):
"""Makes the calling process a session leader, makes fd its
controlling terminal, stdin, stdout, and stderr. Closes fd."""
# Establish a new session.
os.setsid()

# Make fd the controlling terminal.
try:
from fcntl import ioctl
ioctl(fd, tty.TIOCSCTTY)
except (ImportError, AttributeError, OSError):
tmp_fd = os.open(os.ttyname(fd), os.O_RDWR)
os.close(tmp_fd)

# fd becomes stdin/stdout/stderr.
os.dup2(fd, STDIN_FILENO)
os.dup2(fd, STDOUT_FILENO)
os.dup2(fd, STDERR_FILENO)
if (fd > STDERR_FILENO):
os.close(fd)

def fork():
"""fork() -> (pid, master_fd)
Fork and make the child a session leader with a controlling terminal."""
Expand All @@ -97,20 +118,8 @@ def fork():
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)
login_tty(slave_fd)
else:
os.close(slave_fd)

Expand Down Expand Up @@ -148,14 +157,37 @@ def _copy(master_fd, master_read=_read, stdin_read=_read):
else:
_writen(master_fd, data)

def _setwinsz(fd):
"""Sets window size.
If both stdin and fd are terminals,
then set fd's window size to be the
same as that of stdin's."""
try:
from struct import pack
from fcntl import ioctl
except ImportError:
return

strct = pack('HHHH', 0, 0, 0, 0)
try:
winsz = ioctl(STDIN_FILENO, tty.TIOCGWINSZ, strct)
ioctl(fd, tty.TIOCSWINSZ, winsz)
except (AttributeError, OSError):
pass

def spawn(argv, master_read=_read, stdin_read=_read):
"""Create a spawned process."""
if type(argv) == type(''):
argv = (argv,)
sys.audit('pty.spawn', argv)
pid, master_fd = fork()
master_fd, slave_fd = openpty()
_setwinsz(slave_fd)
pid = os.fork()
if pid == CHILD:
os.close(master_fd)
login_tty(slave_fd)
os.execlp(argv[0], *argv)
os.close(slave_fd)
try:
mode = tty.tcgetattr(STDIN_FILENO)
tty.setraw(STDIN_FILENO)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make pty.spawn set window size.