Skip to content

Commit d08fd29

Browse files
bpo-38547: Fix test_pty if the process is the session leader (GH-17519)
Fix test_pty: if the process is the session leader, closing the master file descriptor raises a SIGHUP signal: simply ignore SIGHUP when running the tests. (cherry picked from commit a1838ec) Co-authored-by: Victor Stinner <[email protected]>
1 parent 4594565 commit d08fd29

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Lib/test/test_pty.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,27 @@ def _readline(fd):
6666
# XXX(nnorwitz): these tests leak fds when there is an error.
6767
class PtyTest(unittest.TestCase):
6868
def setUp(self):
69-
# isatty() and close() can hang on some platforms. Set an alarm
70-
# before running the test to make sure we don't hang forever.
7169
old_alarm = signal.signal(signal.SIGALRM, self.handle_sig)
7270
self.addCleanup(signal.signal, signal.SIGALRM, old_alarm)
71+
72+
old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
73+
self.addCleanup(signal.signal, signal.SIGHUP, old_alarm)
74+
75+
# isatty() and close() can hang on some platforms. Set an alarm
76+
# before running the test to make sure we don't hang forever.
7377
self.addCleanup(signal.alarm, 0)
7478
signal.alarm(10)
7579

7680
def handle_sig(self, sig, frame):
7781
self.fail("isatty hung")
7882

83+
@staticmethod
84+
def handle_sighup(sig, frame):
85+
# if the process is the session leader, os.close(master_fd)
86+
# of "master_fd, slave_name = pty.master_open()" raises SIGHUP
87+
# signal: just ignore the signal.
88+
pass
89+
7990
def test_basic(self):
8091
try:
8192
debug("Calling master_open()")
@@ -122,9 +133,11 @@ def test_basic(self):
122133
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
123134

124135
os.close(slave_fd)
136+
# closing master_fd can raise a SIGHUP if the process is
137+
# the session leader: we installed a SIGHUP signal handler
138+
# to ignore this signal.
125139
os.close(master_fd)
126140

127-
128141
def test_fork(self):
129142
debug("calling pty.fork()")
130143
pid, master_fd = pty.fork()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix test_pty: if the process is the session leader, closing the master file
2+
descriptor raises a SIGHUP signal: simply ignore SIGHUP when running the
3+
tests.

0 commit comments

Comments
 (0)