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.

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)

75797680

def handle_sig(self, sig, frame):

7781

self.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+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))

123134124135

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)

126140127-128141

def test_fork(self):

129142

debug("calling pty.fork()")

130143

pid, master_fd = pty.fork()