bpo-38547: Fix test_pty if the process is the session leader (GH-17519) · python/cpython@d08fd29
@@ -66,16 +66,27 @@ def _readline(fd):
6666# XXX(nnorwitz): these tests leak fds when there is an error.
6767class PtyTest(unittest.TestCase):
6868def 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.
7169old_alarm = signal.signal(signal.SIGALRM, self.handle_sig)
7270self.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.
7377self.addCleanup(signal.alarm, 0)
7478signal.alarm(10)
75797680def handle_sig(self, sig, frame):
7781self.fail("isatty hung")
788283+@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+7990def test_basic(self):
8091try:
8192debug("Calling master_open()")
@@ -122,9 +133,11 @@ def test_basic(self):
122133self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
123134124135os.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.
125139os.close(master_fd)
126140127-128141def test_fork(self):
129142debug("calling pty.fork()")
130143pid, master_fd = pty.fork()