Skip to content

Commit c24e78d

Browse files
committed
bpo-41818: Close file descriptors in test_openpty
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 ee9f98d commit c24e78d

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

Lib/test/test_pty.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -176,53 +176,54 @@ def test_openpty(self):
176176
# " An optional feature could not be imported " ... ?
177177
raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.")
178178

179-
self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")
180-
181-
if mode:
182-
self.assertEqual(tty.tcgetattr(slave_fd), mode,
183-
"openpty() failed to set slave termios")
184-
if new_stdin_winsz:
185-
self.assertEqual(_get_term_winsz(slave_fd), new_stdin_winsz,
186-
"openpty() failed to set slave window size")
187-
188-
# Solaris requires reading the fd before anything is returned.
189-
# My guess is that since we open and close the slave fd
190-
# in master_open(), we need to read the EOF.
191-
#
192-
# NOTE: the above comment is from an older version of the test;
193-
# master_open() is not being used anymore.
194-
195-
# Ensure the fd is non-blocking in case there's nothing to read.
196-
blocking = os.get_blocking(master_fd)
197179
try:
198-
os.set_blocking(master_fd, False)
180+
self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")
181+
182+
if mode:
183+
self.assertEqual(tty.tcgetattr(slave_fd), mode,
184+
"openpty() failed to set slave termios")
185+
if new_stdin_winsz:
186+
self.assertEqual(_get_term_winsz(slave_fd), new_stdin_winsz,
187+
"openpty() failed to set slave window size")
188+
189+
# Solaris requires reading the fd before anything is returned.
190+
# My guess is that since we open and close the slave fd
191+
# in master_open(), we need to read the EOF.
192+
#
193+
# NOTE: the above comment is from an older version of the test;
194+
# master_open() is not being used anymore.
195+
196+
# Ensure the fd is non-blocking in case there's nothing to read.
197+
blocking = os.get_blocking(master_fd)
199198
try:
200-
s1 = os.read(master_fd, 1024)
201-
self.assertEqual(b'', s1)
202-
except OSError as e:
203-
if e.errno != errno.EAGAIN:
204-
raise
199+
os.set_blocking(master_fd, False)
200+
try:
201+
s1 = os.read(master_fd, 1024)
202+
self.assertEqual(b'', s1)
203+
except OSError as e:
204+
if e.errno != errno.EAGAIN:
205+
raise
206+
finally:
207+
# Restore the original flags.
208+
os.set_blocking(master_fd, blocking)
209+
210+
debug("Writing to slave_fd")
211+
os.write(slave_fd, TEST_STRING_1)
212+
s1 = _readline(master_fd)
213+
self.assertEqual(b'I wish to buy a fish license.\n',
214+
normalize_output(s1))
215+
216+
debug("Writing chunked output")
217+
os.write(slave_fd, TEST_STRING_2[:5])
218+
os.write(slave_fd, TEST_STRING_2[5:])
219+
s2 = _readline(master_fd)
220+
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
205221
finally:
206-
# Restore the original flags.
207-
os.set_blocking(master_fd, blocking)
208-
209-
debug("Writing to slave_fd")
210-
os.write(slave_fd, TEST_STRING_1)
211-
s1 = _readline(master_fd)
212-
self.assertEqual(b'I wish to buy a fish license.\n',
213-
normalize_output(s1))
214-
215-
debug("Writing chunked output")
216-
os.write(slave_fd, TEST_STRING_2[:5])
217-
os.write(slave_fd, TEST_STRING_2[5:])
218-
s2 = _readline(master_fd)
219-
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
220-
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)
222+
os.close(slave_fd)
223+
# closing master_fd can raise a SIGHUP if the process is
224+
# the session leader: we installed a SIGHUP signal handler
225+
# to ignore this signal.
226+
os.close(master_fd)
226227

227228
def test_fork(self):
228229
debug("calling pty.fork()")

0 commit comments

Comments
 (0)