blob: 204ff56922a69bdb8b3fa7b71102b4daf3e9e495 [file] [log] [blame]
Eric S. Raymond2846b0a2001-02-09 12:00:47 +00001import pty, os, sys
Fredrik Lundhf7850422001-01-17 21:51:36 +00002from test_support import verbose, TestFailed, TestSkipped
Fred Drake4c136ee2000-06-30 23:22:35 +00003
Thomas Woutersb0dbeef2001-03-22 14:50:24 +00004TEST_STRING_1 = "I wish to buy a fish license.\n"
5TEST_STRING_2 = "For my pet fish, Eric.\n"
Fred Drake4c136ee2000-06-30 23:22:35 +00006
7if verbose:
8 def debug(msg):
9 print msg
10else:
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
17try:
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)
24except OSError:
25 # " An optional feature could not be imported " ... ?
Thomas Woutersb9fa0a82000-08-04 13:34:43 +000026 raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
Fred Drake4c136ee2000-06-30 23:22:35 +000027
Thomas Woutersbaf26632000-07-19 14:51:54 +000028if not os.isatty(slave_fd):
29 raise TestFailed, "slave_fd is not a tty"
Fred Drake4c136ee2000-06-30 23:22:35 +000030
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000031# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
32# differences (like extra whitespace, trailing garbage, etc.)
Fred Drake4c136ee2000-06-30 23:22:35 +000033
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000034debug("Writing to slave_fd")
35os.write(slave_fd, TEST_STRING_1)
36s1 = os.read(master_fd, 1024)
Guido van Rossumdc795b82001-09-11 14:24:35 +000037sys.stdout.write(s1.replace("\r\n", "\n"))
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000038
39debug("Writing chunked output")
Fred Drake4c136ee2000-06-30 23:22:35 +000040os.write(slave_fd, TEST_STRING_2[:5])
41os.write(slave_fd, TEST_STRING_2[5:])
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000042s2 = os.read(master_fd, 1024)
Guido van Rossumdc795b82001-09-11 14:24:35 +000043sys.stdout.write(s2.replace("\r\n", "\n"))
Fred Drake4c136ee2000-06-30 23:22:35 +000044
45os.close(slave_fd)
46os.close(master_fd)
47
48# basic pty passed.
49
50debug("calling pty.fork()")
51pid, master_fd = pty.fork()
52if pid == pty.CHILD:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000053 # 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 Petersa19a1682001-03-29 04:36:09 +000059 # (on those systems that have that concept.)
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000060 debug("In child, calling os.setsid()")
Fred Drake4c136ee2000-06-30 23:22:35 +000061 try:
Fred Drake4c136ee2000-06-30 23:22:35 +000062 os.setsid()
63 except OSError:
64 # Good, we already were session leader
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000065 debug("Good: OSError was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +000066 pass
67 except AttributeError:
68 # Have pty, but not setsid() ?
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000069 debug("No setsid() available ?")
Fred Drake4c136ee2000-06-30 23:22:35 +000070 pass
71 except:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000072 # We don't want this error to propagate, escaping the call to
73 # os._exit() and causing very peculiar behavior in the calling
Fred Drake4c136ee2000-06-30 23:22:35 +000074 # regrtest.py !
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000075 # Note: could add traceback printing here.
76 debug("An unexpected error was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +000077 os._exit(1)
78 else:
79 debug("os.setsid() succeeded! (bad!)")
80 os._exit(2)
81 os._exit(4)
82else:
83 debug("Waiting for child (%d) to finish."%pid)
84 (pid, status) = os.waitpid(pid, 0)
Guido van Rossum54e54c62001-09-04 19:14:14 +000085 res = status >> 8
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000086 debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
87 if res == 1:
Fred Drake4c136ee2000-06-30 23:22:35 +000088 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000089 elif res == 2:
Fred Drake4c136ee2000-06-30 23:22:35 +000090 raise TestFailed, "pty.fork() failed to make child a session leader."
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000091 elif res == 3:
Fred Drake4c136ee2000-06-30 23:22:35 +000092 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000093 elif res != 4:
94 raise TestFailed, "pty.fork() failed for unknown reasons."
Fred Drake4c136ee2000-06-30 23:22:35 +000095
96os.close(master_fd)
97
98# pty.fork() passed.