Eric S. Raymond | 2846b0a | 2001-02-09 12:00:47 +0000 | [diff] [blame] | 1 | import pty, os, sys |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 2 | from 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 | |
| 14 | # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing |
| 15 | # because pty code is not too portable. |
| 16 | |
| 17 | try: |
| 18 | debug("Calling master_open()") |
| 19 | master_fd, slave_name = pty.master_open() |
| 20 | debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name)) |
| 21 | debug("Calling slave_open(%s)"%`slave_name`) |
| 22 | slave_fd = pty.slave_open(slave_name) |
| 23 | debug("Got slave_fd '%d'"%slave_fd) |
| 24 | except OSError: |
| 25 | # " An optional feature could not be imported " ... ? |
Thomas Wouters | b9fa0a8 | 2000-08-04 13:34:43 +0000 | [diff] [blame] | 26 | raise TestSkipped, "Pseudo-terminals (seemingly) not functional." |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 27 | |
Thomas Wouters | baf2663 | 2000-07-19 14:51:54 +0000 | [diff] [blame] | 28 | if not os.isatty(slave_fd): |
| 29 | raise TestFailed, "slave_fd is not a tty" |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 30 | |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 31 | # IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other |
| 32 | # differences (like extra whitespace, trailing garbage, etc.) |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 34 | debug("Writing to slave_fd") |
| 35 | os.write(slave_fd, TEST_STRING_1) |
| 36 | s1 = os.read(master_fd, 1024) |
Guido van Rossum | dc795b8 | 2001-09-11 14:24:35 +0000 | [diff] [blame^] | 37 | sys.stdout.write(s1.replace("\r\n", "\n")) |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 38 | |
| 39 | debug("Writing chunked output") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 40 | os.write(slave_fd, TEST_STRING_2[:5]) |
| 41 | os.write(slave_fd, TEST_STRING_2[5:]) |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 42 | s2 = os.read(master_fd, 1024) |
Guido van Rossum | dc795b8 | 2001-09-11 14:24:35 +0000 | [diff] [blame^] | 43 | sys.stdout.write(s2.replace("\r\n", "\n")) |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 44 | |
| 45 | os.close(slave_fd) |
| 46 | os.close(master_fd) |
| 47 | |
| 48 | # basic pty passed. |
| 49 | |
| 50 | debug("calling pty.fork()") |
| 51 | pid, master_fd = pty.fork() |
| 52 | if pid == pty.CHILD: |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 53 | # stdout should be connected to a tty. |
| 54 | if not os.isatty(1): |
| 55 | debug("Child's fd 1 is not a tty?!") |
| 56 | os._exit(3) |
| 57 | |
| 58 | # After pty.fork(), the child should already be a session leader. |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 59 | # (on those systems that have that concept.) |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 60 | debug("In child, calling os.setsid()") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 61 | try: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 62 | os.setsid() |
| 63 | except OSError: |
| 64 | # Good, we already were session leader |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 65 | debug("Good: OSError was raised.") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 66 | pass |
| 67 | except AttributeError: |
| 68 | # Have pty, but not setsid() ? |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 69 | debug("No setsid() available ?") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 70 | pass |
| 71 | except: |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 72 | # We don't want this error to propagate, escaping the call to |
| 73 | # os._exit() and causing very peculiar behavior in the calling |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 74 | # regrtest.py ! |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 75 | # Note: could add traceback printing here. |
| 76 | debug("An unexpected error was raised.") |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 77 | os._exit(1) |
| 78 | else: |
| 79 | debug("os.setsid() succeeded! (bad!)") |
| 80 | os._exit(2) |
| 81 | os._exit(4) |
| 82 | else: |
| 83 | debug("Waiting for child (%d) to finish."%pid) |
| 84 | (pid, status) = os.waitpid(pid, 0) |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 85 | res = status >> 8 |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 86 | debug("Child (%d) exited with status %d (%d)."%(pid, res, status)) |
| 87 | if res == 1: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 88 | raise TestFailed, "Child raised an unexpected exception in os.setsid()" |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 89 | elif res == 2: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 90 | raise TestFailed, "pty.fork() failed to make child a session leader." |
Thomas Wouters | b0dbeef | 2001-03-22 14:50:24 +0000 | [diff] [blame] | 91 | elif res == 3: |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 92 | 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] | 93 | elif res != 4: |
| 94 | raise TestFailed, "pty.fork() failed for unknown reasons." |
Fred Drake | 4c136ee | 2000-06-30 23:22:35 +0000 | [diff] [blame] | 95 | |
| 96 | os.close(master_fd) |
| 97 | |
| 98 | # pty.fork() passed. |