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 | |
Thomas Wouters | 35f34f8 | 2006-01-28 12:05:54 +0000 | [diff] [blame^] | 7 | # Solaris (at least 2.9 and 2.10) seem to have a ficke isatty(). The first |
| 8 | # test below, testing the result of os.openpty() for tty-ness, sometimes |
| 9 | # (but not always) fails. The second isatty test, in the sub-process, always |
| 10 | # works. Allow that fickle first test to fail on these platforms, since it |
| 11 | # doesn't actually affect functionality. |
| 12 | fickle_isatty = ["sunos5"] |
| 13 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 14 | if verbose: |
| 15 | def debug(msg): |
| 16 | print msg |
| 17 | else: |
| 18 | def debug(msg): |
| 19 | pass |
| 20 | |
| 21 | # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing |
| 22 | # because pty code is not too portable. |
| 23 | |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 24 | def test_basic_pty(): |
| 25 | try: |
| 26 | debug("Calling master_open()") |
| 27 | master_fd, slave_name = pty.master_open() |
| 28 | 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] | 29 | debug("Calling slave_open(%r)"%(slave_name,)) |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 30 | slave_fd = pty.slave_open(slave_name) |
| 31 | debug("Got slave_fd '%d'"%slave_fd) |
| 32 | except OSError: |
| 33 | # " An optional feature could not be imported " ... ? |
| 34 | raise TestSkipped, "Pseudo-terminals (seemingly) not functional." |
| 35 | |
Thomas Wouters | 35f34f8 | 2006-01-28 12:05:54 +0000 | [diff] [blame^] | 36 | if not os.isatty(slave_fd) and sys.platform not in fickle_isatty: |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 37 | raise TestFailed, "slave_fd is not a tty" |
| 38 | |
| 39 | # IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other |
| 40 | # differences (like extra whitespace, trailing garbage, etc.) |
| 41 | |
| 42 | debug("Writing to slave_fd") |
| 43 | os.write(slave_fd, TEST_STRING_1) |
| 44 | s1 = os.read(master_fd, 1024) |
| 45 | sys.stdout.write(s1.replace("\r\n", "\n")) |
| 46 | |
| 47 | debug("Writing chunked output") |
| 48 | os.write(slave_fd, TEST_STRING_2[:5]) |
| 49 | os.write(slave_fd, TEST_STRING_2[5:]) |
| 50 | s2 = os.read(master_fd, 1024) |
| 51 | sys.stdout.write(s2.replace("\r\n", "\n")) |
| 52 | |
| 53 | os.close(slave_fd) |
| 54 | os.close(master_fd) |
| 55 | |
| 56 | def handle_sig(sig, frame): |
| 57 | raise TestFailed, "isatty hung" |
| 58 | |
| 59 | # isatty() and close() can hang on some platforms |
| 60 | # set an alarm before running the test to make sure we don't hang forever |
| 61 | old_alarm = signal.signal(signal.SIGALRM, handle_sig) |
| 62 | signal.alarm(10) |
| 63 | |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 64 | try: |
Neal Norwitz | 7d81452 | 2003-03-21 01:39:14 +0000 | [diff] [blame] | 65 | test_basic_pty() |
| 66 | finally: |
| 67 | # remove alarm, restore old alarm handler |
| 68 | signal.alarm(0) |
| 69 | signal.signal(signal.SIGALRM, old_alarm) |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 70 | |
| 71 | # basic pty passed. |
| 72 | |
| 73 | debug("calling pty.fork()") |
| 74 | pid, master_fd = pty.fork() |
| 75 | if pid == pty.CHILD: |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 76 | # stdout should be connected to a tty. |
| 77 | if not os.isatty(1): |
| 78 | debug("Child's fd 1 is not a tty?!") |
| 79 | os._exit(3) |
| 80 | |
| 81 | # After pty.fork(), the child should already be a session leader. |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 82 | # (on those systems that have that concept.) |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 83 | debug("In child, calling os.setsid()") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 84 | try: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 85 | os.setsid() |
| 86 | except OSError: |
| 87 | # Good, we already were session leader |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 88 | debug("Good: OSError was raised.") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 89 | pass |
| 90 | except AttributeError: |
| 91 | # Have pty, but not setsid() ? |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 92 | debug("No setsid() available ?") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 93 | pass |
| 94 | except: |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 95 | # We don't want this error to propagate, escaping the call to |
| 96 | # os._exit() and causing very peculiar behavior in the calling |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 97 | # regrtest.py ! |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 98 | # Note: could add traceback printing here. |
| 99 | debug("An unexpected error was raised.") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 100 | os._exit(1) |
| 101 | else: |
| 102 | debug("os.setsid() succeeded! (bad!)") |
| 103 | os._exit(2) |
| 104 | os._exit(4) |
| 105 | else: |
| 106 | debug("Waiting for child (%d) to finish."%pid) |
| 107 | (pid, status) = os.waitpid(pid, 0) |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 108 | res = status >> 8 |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 109 | debug("Child (%d) exited with status %d (%d)."%(pid, res, status)) |
| 110 | if res == 1: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 111 | raise TestFailed, "Child raised an unexpected exception in os.setsid()" |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 112 | elif res == 2: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 113 | raise TestFailed, "pty.fork() failed to make child a session leader." |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 114 | elif res == 3: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 115 | 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] | 116 | elif res != 4: |
| 117 | raise TestFailed, "pty.fork() failed for unknown reasons." |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 118 | |
| 119 | os.close(master_fd) |
| 120 | |
| 121 | # pty.fork() passed. |