Skip to content

Commit 65cf1ad

Browse files
authored
bpo-41818: Close file descriptors in test_openpty (#GH-24119)
When stdin is a TTY, the test added in commit c13d899 is expected to fail. However, when it failed, it did not close its file descriptors. This is flagged by the refleak tests (but only when stdin is a TTY, which doesn't seem to be the case on CI).
1 parent bc450f9 commit 65cf1ad

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Lib/test/test_pty.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def _set_term_winsz(fd, winsz):
9191

9292
# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
9393
# because pty code is not too portable.
94-
# XXX(nnorwitz): these tests leak fds when there is an error.
9594
class PtyTest(unittest.TestCase):
9695
def setUp(self):
9796
old_alarm = signal.signal(signal.SIGALRM, self.handle_sig)
@@ -176,6 +175,12 @@ def test_openpty(self):
176175
# " An optional feature could not be imported " ... ?
177176
raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.")
178177

178+
# closing master_fd can raise a SIGHUP if the process is
179+
# the session leader: we installed a SIGHUP signal handler
180+
# to ignore this signal.
181+
self.addCleanup(os.close, master_fd)
182+
self.addCleanup(os.close, slave_fd)
183+
179184
self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")
180185

181186
if mode:
@@ -218,15 +223,10 @@ def test_openpty(self):
218223
s2 = _readline(master_fd)
219224
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
220225

221-
os.close(slave_fd)
222-
# closing master_fd can raise a SIGHUP if the process is
223-
# the session leader: we installed a SIGHUP signal handler
224-
# to ignore this signal.
225-
os.close(master_fd)
226-
227226
def test_fork(self):
228227
debug("calling pty.fork()")
229228
pid, master_fd = pty.fork()
229+
self.addCleanup(os.close, master_fd)
230230
if pid == pty.CHILD:
231231
# stdout should be connected to a tty.
232232
if not os.isatty(1):
@@ -305,13 +305,14 @@ def test_fork(self):
305305
##else:
306306
## raise TestFailed("Read from master_fd did not raise exception")
307307

308-
os.close(master_fd)
309-
310308
def test_master_read(self):
309+
# XXX(nnorwitz): this test leaks fds when there is an error.
311310
debug("Calling pty.openpty()")
312311
master_fd, slave_fd = pty.openpty()
313312
debug(f"Got master_fd '{master_fd}', slave_fd '{slave_fd}'")
314313

314+
self.addCleanup(os.close, master_fd)
315+
315316
debug("Closing slave_fd")
316317
os.close(slave_fd)
317318

@@ -321,7 +322,6 @@ def test_master_read(self):
321322
except OSError: # Linux
322323
data = b""
323324

324-
os.close(master_fd)
325325
self.assertEqual(data, b"")
326326

327327
class SmallPtyTests(unittest.TestCase):

0 commit comments

Comments
 (0)