Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 1 | import pty, os, sys, string |
| 2 | from test_support import verbose, TestFailed |
| 3 | |
| 4 | TEST_STRING_1 = "I wish to buy a fish license." |
| 5 | TEST_STRING_2 = "For my pet fish, Eric." |
| 6 | TEST_STRING_3 = "And now for something completely different..." |
| 7 | TEST_STRING_4 = "but you pronounce it throatwobbler mangrove." |
| 8 | |
| 9 | if verbose: |
| 10 | def debug(msg): |
| 11 | print msg |
| 12 | else: |
| 13 | def debug(msg): |
| 14 | pass |
| 15 | |
| 16 | # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing |
| 17 | # because pty code is not too portable. |
| 18 | |
| 19 | try: |
| 20 | debug("Calling master_open()") |
| 21 | master_fd, slave_name = pty.master_open() |
| 22 | debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name)) |
| 23 | debug("Calling slave_open(%s)"%`slave_name`) |
| 24 | slave_fd = pty.slave_open(slave_name) |
| 25 | debug("Got slave_fd '%d'"%slave_fd) |
| 26 | except OSError: |
| 27 | # " An optional feature could not be imported " ... ? |
| 28 | raise ImportError, "Pseudo-terminals (seemingly) not functional." |
| 29 | |
Thomas Wouters | baf2663 | 2000-07-19 14:51:54 +0000 | [diff] [blame] | 30 | if not os.isatty(master_fd): |
| 31 | raise TestFailed, "master_fd is not a tty" |
| 32 | if not os.isatty(slave_fd): |
| 33 | raise TestFailed, "slave_fd is not a tty" |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 34 | |
| 35 | debug("Writing to slave_fd") |
| 36 | os.write(slave_fd, TEST_STRING_1) # should check return value |
| 37 | print os.read(master_fd, 1024) |
| 38 | |
| 39 | os.write(slave_fd, TEST_STRING_2[:5]) |
| 40 | os.write(slave_fd, TEST_STRING_2[5:]) |
| 41 | print os.read(master_fd, 1024) |
| 42 | |
| 43 | os.close(slave_fd) |
| 44 | os.close(master_fd) |
| 45 | |
| 46 | # basic pty passed. |
| 47 | |
| 48 | debug("calling pty.fork()") |
| 49 | pid, master_fd = pty.fork() |
| 50 | if pid == pty.CHILD: |
| 51 | ## # Please uncomment these when os.isatty() is added. |
| 52 | ## if not os.isatty(1): |
| 53 | ## debug("Child's fd 1 is not a tty?!") |
| 54 | ## os._exit(3) |
| 55 | try: |
| 56 | debug("In child, calling os.setsid()") |
| 57 | os.setsid() |
| 58 | except OSError: |
| 59 | # Good, we already were session leader |
| 60 | debug("OSError was raised.") |
| 61 | pass |
| 62 | except AttributeError: |
| 63 | # Have pty, but not setsid() ? |
| 64 | debug("AttributeError was raised.") |
| 65 | pass |
| 66 | except: |
| 67 | # We don't want this error to propagate, escape the call to |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 68 | # os._exit(), and cause very peculiar behavior in the calling |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 69 | # regrtest.py ! |
| 70 | debug("Some other error was raised.") |
| 71 | os._exit(1) |
| 72 | else: |
| 73 | debug("os.setsid() succeeded! (bad!)") |
| 74 | os._exit(2) |
| 75 | os._exit(4) |
| 76 | else: |
| 77 | debug("Waiting for child (%d) to finish."%pid) |
| 78 | (pid, status) = os.waitpid(pid, 0) |
| 79 | debug("Child (%d) exited with status %d."%(pid, status)) |
| 80 | if status / 256 == 1: |
| 81 | raise TestFailed, "Child raised an unexpected exception in os.setsid()" |
| 82 | elif status / 256 == 2: |
| 83 | raise TestFailed, "pty.fork() failed to make child a session leader." |
| 84 | elif status / 256 == 3: |
| 85 | raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout" |
| 86 | elif status / 256 <> 4: |
| 87 | raise TestFailed, "pty.fork() failed for unknown reasons:" |
| 88 | print os.read(master_fd, 65536) |
| 89 | |
| 90 | os.close(master_fd) |
| 91 | |
| 92 | # pty.fork() passed. |
| 93 | |