diff options
Diffstat (limited to 'Lib/test/test_pty.py')
-rw-r--r-- | Lib/test/test_pty.py | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/Lib/test/test_pty.py b/Lib/test/test_pty.py index bec38c45456..4d471d534ee 100644 --- a/Lib/test/test_pty.py +++ b/Lib/test/test_pty.py @@ -1,4 +1,4 @@ -from test.test_support import verbose, run_unittest, import_module +from test.support import verbose, run_unittest, import_module, reap_children #Skip these tests if either fcntl or termios is not available fcntl = import_module('fcntl') @@ -13,12 +13,12 @@ import signal import socket import unittest -TEST_STRING_1 = "I wish to buy a fish license.\n" -TEST_STRING_2 = "For my pet fish, Eric.\n" +TEST_STRING_1 = b"I wish to buy a fish license.\n" +TEST_STRING_2 = b"For my pet fish, Eric.\n" if verbose: def debug(msg): - print msg + print(msg) else: def debug(msg): pass @@ -36,12 +36,12 @@ def normalize_output(data): # from someone more knowledgable. # OSF/1 (Tru64) apparently turns \n into \r\r\n. - if data.endswith('\r\r\n'): - return data.replace('\r\r\n', '\n') + if data.endswith(b'\r\r\n'): + return data.replace(b'\r\r\n', b'\n') # IRIX apparently turns \n into \r\n. - if data.endswith('\r\n'): - return data.replace('\r\n', '\n') + if data.endswith(b'\r\n'): + return data.replace(b'\r\n', b'\n') return data @@ -75,7 +75,7 @@ class PtyTest(unittest.TestCase): debug("Got slave_fd '%d'" % slave_fd) except OSError: # " An optional feature could not be imported " ... ? - raise unittest.SkipTest, "Pseudo-terminals (seemingly) not functional." + raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.") self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty') @@ -88,8 +88,8 @@ class PtyTest(unittest.TestCase): fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK) try: s1 = os.read(master_fd, 1024) - self.assertEqual('', s1) - except OSError, e: + self.assertEqual(b'', s1) + except OSError as e: if e.errno != errno.EAGAIN: raise # Restore the original flags. @@ -98,14 +98,14 @@ class PtyTest(unittest.TestCase): debug("Writing to slave_fd") os.write(slave_fd, TEST_STRING_1) s1 = os.read(master_fd, 1024) - self.assertEqual('I wish to buy a fish license.\n', + self.assertEqual(b'I wish to buy a fish license.\n', normalize_output(s1)) debug("Writing chunked output") os.write(slave_fd, TEST_STRING_2[:5]) os.write(slave_fd, TEST_STRING_2[5:]) s2 = os.read(master_fd, 1024) - self.assertEqual('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(master_fd) @@ -163,7 +163,8 @@ class PtyTest(unittest.TestCase): break if not data: break - sys.stdout.write(data.replace('\r\n', '\n')) + sys.stdout.write(str(data.replace(b'\r\n', b'\n'), + encoding='ascii')) ##line = os.read(master_fd, 80) ##lines = line.replace('\r\n', '\n').split('\n') @@ -214,7 +215,7 @@ class SmallPtyTests(unittest.TestCase): for fd in self.fds: try: os.close(fd) - except: + except OSError: pass def _pipe(self): @@ -234,8 +235,9 @@ class SmallPtyTests(unittest.TestCase): mock_stdin_fd, write_to_stdin_fd = self._pipe() pty.STDIN_FILENO = mock_stdin_fd socketpair = socket.socketpair() + for s in socketpair: + self.addCleanup(s.close) masters = [s.fileno() for s in socketpair] - self.fds.extend(masters) # Feed data. Smaller than PIPEBUF. These writes will not block. os.write(masters[1], b'from master') @@ -263,8 +265,9 @@ class SmallPtyTests(unittest.TestCase): mock_stdin_fd, write_to_stdin_fd = self._pipe() pty.STDIN_FILENO = mock_stdin_fd socketpair = socket.socketpair() + for s in socketpair: + self.addCleanup(s.close) masters = [s.fileno() for s in socketpair] - self.fds.extend(masters) os.close(masters[1]) socketpair[1].close() @@ -283,7 +286,10 @@ class SmallPtyTests(unittest.TestCase): def test_main(verbose=None): - run_unittest(SmallPtyTests, PtyTest) + try: + run_unittest(SmallPtyTests, PtyTest) + finally: + reap_children() if __name__ == "__main__": test_main() |