Skip to content

Commit b7eb9e8

Browse files
committed
Instead of the big indent, use self.addCleanup
1 parent c24e78d commit b7eb9e8

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

Lib/test/test_pty.py

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

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

228227
def test_fork(self):
229228
debug("calling pty.fork()")

0 commit comments

Comments
 (0)