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 a1838ec259)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2019-12-09 03:15:23 -08:00 committed by GitHub
parent 4594565b56
commit d08fd298dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -66,16 +66,27 @@ def _readline(fd):
# XXX(nnorwitz): these tests leak fds when there is an error. # XXX(nnorwitz): these tests leak fds when there is an error.
class PtyTest(unittest.TestCase): class PtyTest(unittest.TestCase):
def setUp(self): def setUp(self):
# isatty() and close() can hang on some platforms. Set an alarm
# before running the test to make sure we don't hang forever.
old_alarm = signal.signal(signal.SIGALRM, self.handle_sig) old_alarm = signal.signal(signal.SIGALRM, self.handle_sig)
self.addCleanup(signal.signal, signal.SIGALRM, old_alarm) self.addCleanup(signal.signal, signal.SIGALRM, old_alarm)
old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
self.addCleanup(signal.signal, signal.SIGHUP, old_alarm)
# isatty() and close() can hang on some platforms. Set an alarm
# before running the test to make sure we don't hang forever.
self.addCleanup(signal.alarm, 0) self.addCleanup(signal.alarm, 0)
signal.alarm(10) signal.alarm(10)
def handle_sig(self, sig, frame): def handle_sig(self, sig, frame):
self.fail("isatty hung") self.fail("isatty hung")
@staticmethod
def handle_sighup(sig, frame):
# if the process is the session leader, os.close(master_fd)
# of "master_fd, slave_name = pty.master_open()" raises SIGHUP
# signal: just ignore the signal.
pass
def test_basic(self): def test_basic(self):
try: try:
debug("Calling master_open()") debug("Calling master_open()")
@ -122,9 +133,11 @@ class PtyTest(unittest.TestCase):
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2)) self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
os.close(slave_fd) os.close(slave_fd)
# closing master_fd can raise a SIGHUP if the process is
# the session leader: we installed a SIGHUP signal handler
# to ignore this signal.
os.close(master_fd) os.close(master_fd)
def test_fork(self): def test_fork(self):
debug("calling pty.fork()") debug("calling pty.fork()")
pid, master_fd = pty.fork() pid, master_fd = pty.fork()

View File

@ -0,0 +1,3 @@
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.