Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 1 | import pty, os, sys, signal |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 2 | from test.test_support import verbose, TestFailed, TestSkipped |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 3 | |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 4 | TEST_STRING_1 = "I wish to buy a fish license.\n" |
| 5 | TEST_STRING_2 = "For my pet fish, Eric.\n" |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 6 | |
| 7 | if verbose: |
| 8 | def debug(msg): |
| 9 | print msg |
| 10 | else: |
| 11 | def debug(msg): |
| 12 | pass |
| 13 | |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 14 | def normalize_output(data): |
| 15 | # Some operating systems do conversions on newline. We could possibly |
| 16 | # fix that by doing the appropriate termios.tcsetattr()s. I couldn't |
| 17 | # figure out the right combo on Tru64 and I don't have an IRIX box. |
Anthony Baxter | a2a26b9 | 2006-04-05 17:30:38 +0000 | [diff] [blame] | 18 | # So just normalize the output and doc the problem O/Ses by allowing |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 19 | # certain combinations for some platforms, but avoid allowing other |
| 20 | # differences (like extra whitespace, trailing garbage, etc.) |
| 21 | |
| 22 | # This is about the best we can do without getting some feedback |
| 23 | # from someone more knowledgable. |
| 24 | |
| 25 | # OSF/1 (Tru64) apparently turns \n into \r\r\n. |
| 26 | if data.endswith('\r\r\n'): |
Neal Norwitz | 9cc3b1c | 2006-04-26 06:26:12 +0000 | [diff] [blame] | 27 | return data.replace('\r\r\n', '\n') |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 28 | |
| 29 | # IRIX apparently turns \n into \r\n. |
| 30 | if data.endswith('\r\n'): |
Neal Norwitz | 9cc3b1c | 2006-04-26 06:26:12 +0000 | [diff] [blame] | 31 | return data.replace('\r\n', '\n') |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 32 | |
| 33 | return data |
| 34 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 35 | # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing |
| 36 | # because pty code is not too portable. |
| 37 | |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 38 | def test_basic_pty(): |
| 39 | try: |
| 40 | debug("Calling master_open()") |
| 41 | master_fd, slave_name = pty.master_open() |
| 42 | debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name)) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 43 | debug("Calling slave_open(%r)"%(slave_name,)) |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 44 | slave_fd = pty.slave_open(slave_name) |
| 45 | debug("Got slave_fd '%d'"%slave_fd) |
| 46 | except OSError: |
| 47 | # " An optional feature could not be imported " ... ? |
| 48 | raise TestSkipped, "Pseudo-terminals (seemingly) not functional." |
| 49 | |
Thomas Wouters | 6dbff33 | 2006-04-25 13:53:23 +0000 | [diff] [blame] | 50 | if not os.isatty(slave_fd): |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 51 | raise TestFailed, "slave_fd is not a tty" |
| 52 | |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 53 | debug("Writing to slave_fd") |
| 54 | os.write(slave_fd, TEST_STRING_1) |
| 55 | s1 = os.read(master_fd, 1024) |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 56 | sys.stdout.write(normalize_output(s1)) |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 57 | |
| 58 | debug("Writing chunked output") |
| 59 | os.write(slave_fd, TEST_STRING_2[:5]) |
| 60 | os.write(slave_fd, TEST_STRING_2[5:]) |
| 61 | s2 = os.read(master_fd, 1024) |
Neal Norwitz | 84c95b9 | 2006-04-03 05:28:31 +0000 | [diff] [blame] | 62 | sys.stdout.write(normalize_output(s2)) |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 63 | |
| 64 | os.close(slave_fd) |
| 65 | os.close(master_fd) |
| 66 | |
| 67 | def handle_sig(sig, frame): |
| 68 | raise TestFailed, "isatty hung" |
| 69 | |
| 70 | # isatty() and close() can hang on some platforms |
| 71 | # set an alarm before running the test to make sure we don't hang forever |
| 72 | old_alarm = signal.signal(signal.SIGALRM, handle_sig) |
| 73 | signal.alarm(10) |
| 74 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 75 | try: |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 76 | test_basic_pty() |
| 77 | finally: |
| 78 | # remove alarm, restore old alarm handler |
| 79 | signal.alarm(0) |
| 80 | signal.signal(signal.SIGALRM, old_alarm) |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 81 | |
| 82 | # basic pty passed. |
| 83 | |
| 84 | debug("calling pty.fork()") |
| 85 | pid, master_fd = pty.fork() |
| 86 | if pid == pty.CHILD: |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 87 | # stdout should be connected to a tty. |
| 88 | if not os.isatty(1): |
| 89 | debug("Child's fd 1 is not a tty?!") |
| 90 | os._exit(3) |
| 91 | |
| 92 | # After pty.fork(), the child should already be a session leader. |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 93 | # (on those systems that have that concept.) |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 94 | debug("In child, calling os.setsid()") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 95 | try: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 96 | os.setsid() |
| 97 | except OSError: |
| 98 | # Good, we already were session leader |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 99 | debug("Good: OSError was raised.") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 100 | pass |
| 101 | except AttributeError: |
| 102 | # Have pty, but not setsid() ? |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 103 | debug("No setsid() available ?") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 104 | pass |
| 105 | except: |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 106 | # We don't want this error to propagate, escaping the call to |
| 107 | # os._exit() and causing very peculiar behavior in the calling |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 108 | # regrtest.py ! |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 109 | # Note: could add traceback printing here. |
| 110 | debug("An unexpected error was raised.") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 111 | os._exit(1) |
| 112 | else: |
| 113 | debug("os.setsid() succeeded! (bad!)") |
| 114 | os._exit(2) |
| 115 | os._exit(4) |
| 116 | else: |
| 117 | debug("Waiting for child (%d) to finish."%pid) |
Andrew M. Kuchling | f2881e8 | 2006-12-22 19:21:27 +0000 | [diff] [blame] | 118 | ##line = os.read(master_fd, 80) |
| 119 | ##lines = line.replace('\r\n', '\n').split('\n') |
| 120 | ##if False and lines != ['In child, calling os.setsid()', |
| 121 | ## 'Good: OSError was raised.', '']: |
| 122 | ## raise TestFailed("Unexpected output from child: %r" % line) |
Andrew M. Kuchling | ee0e6d1 | 2006-12-22 18:41:42 +0000 | [diff] [blame] | 123 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 124 | (pid, status) = os.waitpid(pid, 0) |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 125 | res = status >> 8 |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 126 | debug("Child (%d) exited with status %d (%d)."%(pid, res, status)) |
| 127 | if res == 1: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 128 | raise TestFailed, "Child raised an unexpected exception in os.setsid()" |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 129 | elif res == 2: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 130 | raise TestFailed, "pty.fork() failed to make child a session leader." |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 131 | elif res == 3: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 132 | raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout" |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 133 | elif res != 4: |
| 134 | raise TestFailed, "pty.fork() failed for unknown reasons." |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 135 | |
Andrew M. Kuchling | 55c54a2 | 2006-12-22 21:48:19 +0000 | [diff] [blame^] | 136 | ##debug("Reading from master_fd now that the child has exited") |
| 137 | ##try: |
| 138 | ## s1 = os.read(master_fd, 1024) |
| 139 | ##except os.error: |
| 140 | ## pass |
| 141 | ##else: |
| 142 | ## raise TestFailed("Read from master_fd did not raise exception") |
Andrew M. Kuchling | ee0e6d1 | 2006-12-22 18:41:42 +0000 | [diff] [blame] | 143 | |
| 144 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 145 | os.close(master_fd) |
| 146 | |
| 147 | # pty.fork() passed. |