Skip to content

bpo-29070: Integration tests for pty module with patch from bpo-26228 #2932

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 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
703c1d1
migrating issue to github
diekmann Apr 10, 2017
78c165a
using subprocess.run to run pty.spawn
diekmann Apr 14, 2017
28e918e
move _mock_stdin_stdout to the actual tests
diekmann Apr 14, 2017
6e7f587
Get os.forkpty disabled again.
diekmann Apr 14, 2017
fd61cad
Yes, way less monkey patching.
diekmann Apr 14, 2017
e9b0b8b
Decoupling PtySpawnTestBase and PtyMockingTestBase
diekmann Apr 14, 2017
a42755b
Move PtyMockingTestBase closer to PtyCopyTests
diekmann Apr 14, 2017
7451f97
Tuned class hierarchy
diekmann Apr 14, 2017
b604d83
fix line length and error path of base classes
diekmann Apr 14, 2017
cbe7d14
clarify difference to alternatives
diekmann Apr 14, 2017
8afd1e7
move disable-os.forkpty-code closer to the test
diekmann Apr 14, 2017
2c3498f
cosmetic changes
diekmann Apr 14, 2017
ec58a62
polish
diekmann Apr 14, 2017
0be6c54
let exceptions out and fail the tests
diekmann Apr 14, 2017
250ae50
Pull related code for select mocking tests together
diekmann Apr 14, 2017
7ced6d8
Tune PtyCopyTests
diekmann Apr 14, 2017
528fc5d
unify code comments in PtySpawnTestBase
diekmann Apr 15, 2017
aeb6585
cosmetic changes
diekmann Apr 15, 2017
57876df
Cleanup of terminal setup code
diekmann Apr 15, 2017
2309d69
removed test_eof_echoctl
diekmann Apr 15, 2017
f93de8d
fixed echoing race
diekmann Apr 15, 2017
9cd9425
cleaned OS X workaround comments
diekmann Apr 15, 2017
a7e4753
Removed doc string from test methods.
diekmann Apr 15, 2017
5944764
Fixed comment
diekmann Apr 15, 2017
851c261
[CONTRIBUTING.rst] Added myself to Misc/ACKS
diekmann Jul 28, 2017
5dbc249
added Chris Torek to ACKS
diekmann Jul 28, 2017
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
31 changes: 23 additions & 8 deletions Doc/library/pty.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
========================================

.. module:: pty
:platform: Linux
:synopsis: Pseudo-Terminal Handling for Linux.
:platform: Unix
:synopsis: Pseudo-Terminal Handling for Unix.

.. moduleauthor:: Steen Lumholt
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
Expand All @@ -16,9 +16,9 @@ The :mod:`pty` module defines operations for handling the pseudo-terminal
concept: starting another process and being able to write to and read from its
controlling terminal programmatically.

Because pseudo-terminal handling is highly platform dependent, there is code to
do it only for Linux. (The Linux code is supposed to work on other platforms,
but hasn't been tested yet.)
Pseudo-terminal handling is highly platform dependent. This code is mainly
tested on Linux, FreeBSD, and OS X (it is supposed to work on other POSIX
platforms).

The :mod:`pty` module defines the following functions:

Expand All @@ -41,9 +41,13 @@ The :mod:`pty` module defines the following functions:

.. function:: spawn(argv[, master_read[, stdin_read]])

Spawn a process, and connect its controlling terminal with the current
process's standard io. This is often used to baffle programs which insist on
reading from the controlling terminal.
Spawn a child process, and connect its controlling terminal with the
current process's standard io. This is often used to baffle programs which
insist on reading from the controlling terminal.

A loop copies STDIN of the current process to the child and data received
from the child to STDOUT of the current process. It is not signaled to the
child if STDIN of the current process closes down.

The functions *master_read* and *stdin_read* should be functions which read from
a file descriptor. The defaults try to read 1024 bytes each time they are
Expand Down Expand Up @@ -91,3 +95,14 @@ pseudo-terminal to record all input and output of a terminal session in a

script.write(('Script done on %s\n' % time.asctime()).encode())
print('Script done, file is', filename)

Caveats
-------

.. sectionauthor:: Cornelius Diekmann

Be aware that processes started by :func:`spawn` do not receive any information
about STDIN of their parent shutting down. For example, if run on a terminal on
a Linux system, ``/bin/sh < /dev/null`` closes immediately. However,
``./python -c 'import pty; pty.spawn("/bin/sh")' < /dev/null`` does not close
because the spawned child shell is not notified that STDIN is closed.
25 changes: 22 additions & 3 deletions Lib/pty.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Pseudo terminal utilities."""

# Bugs: No signal handling. Doesn't set slave termios and window size.
# Only tested on Linux.
# Only tested on Linux, FreeBSD, and OS X.
# 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 sys
import tty

__all__ = ["openpty","fork","spawn"]
Expand Down Expand Up @@ -133,11 +134,16 @@ def _copy(master_fd, master_read=_read, stdin_read=_read):
standard input -> pty master (stdin_read)"""
fds = [master_fd, STDIN_FILENO]
while True:
# The expected path to leave this infinite loop is that the
# child exits and its slave_fd is destroyed. In this case,
# master_fd will become ready in select() and reading from
# master_fd either raises an OSError (Input/output error) on
# Linux or returns EOF on BSD.
rfds, wfds, xfds = select(fds, [], [])
if master_fd in rfds:
data = master_read(master_fd)
if not data: # Reached EOF.
fds.remove(master_fd)
return
else:
os.write(STDOUT_FILENO, data)
if STDIN_FILENO in rfds:
Expand All @@ -153,7 +159,16 @@ def spawn(argv, master_read=_read, stdin_read=_read):
argv = (argv,)
pid, master_fd = fork()
if pid == CHILD:
os.execlp(argv[0], *argv)
try:
#XXX issue17824 still open
os.execlp(argv[0], *argv)
except:
# If we wanted to be really clever, we would use
# the same method as subprocess() to pass the error
# back to the parent. For now just dump stack trace.
sys.excepthook(*sys.exc_info())
finally:
os._exit(1)
try:
mode = tty.tcgetattr(STDIN_FILENO)
tty.setraw(STDIN_FILENO)
Expand All @@ -163,6 +178,10 @@ def spawn(argv, master_read=_read, stdin_read=_read):
try:
_copy(master_fd, master_read, stdin_read)
except OSError:
# Some OSes never return an EOF on pty, just raise
# an error instead.
pass
finally:
if restore:
tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)

Expand Down
Loading