FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-41541: Make pty.spawn set window size by 8vasu · Pull Request #21861 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
60 changes: 46 additions & 14 deletions Lib/pty.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make pty.spawn set window size.

Back | FazBrowse Home | New Git URL