blob: 53769c041a2e9df199750479fb4412985d7f49de [file] [log] [blame]
Fred Drake4c136ee2000-06-30 23:22:35 +00001import pty, os, sys, string
Thomas Woutersb9fa0a82000-08-04 13:34:43 +00002from test_support import verbose, TestFailed, TestSkipped
Fred Drake4c136ee2000-06-30 23:22:35 +00003
4TEST_STRING_1 = "I wish to buy a fish license."
5TEST_STRING_2 = "For my pet fish, Eric."
6TEST_STRING_3 = "And now for something completely different..."
7TEST_STRING_4 = "but you pronounce it throatwobbler mangrove."
8
9if verbose:
10 def debug(msg):
11 print msg
12else:
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
19try:
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)
26except OSError:
27 # " An optional feature could not be imported " ... ?
Thomas Woutersb9fa0a82000-08-04 13:34:43 +000028 raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
Fred Drake4c136ee2000-06-30 23:22:35 +000029
Thomas Woutersbaf26632000-07-19 14:51:54 +000030if not os.isatty(slave_fd):
31 raise TestFailed, "slave_fd is not a tty"
Fred Drake4c136ee2000-06-30 23:22:35 +000032
33debug("Writing to slave_fd")
34os.write(slave_fd, TEST_STRING_1) # should check return value
35print os.read(master_fd, 1024)
36
37os.write(slave_fd, TEST_STRING_2[:5])
38os.write(slave_fd, TEST_STRING_2[5:])
39print os.read(master_fd, 1024)
40
41os.close(slave_fd)
42os.close(master_fd)
43
44# basic pty passed.
45
46debug("calling pty.fork()")
47pid, master_fd = pty.fork()
48if pid == pty.CHILD:
49 ## # Please uncomment these when os.isatty() is added.
50 ## if not os.isatty(1):
51 ## debug("Child's fd 1 is not a tty?!")
52 ## os._exit(3)
53 try:
54 debug("In child, calling os.setsid()")
55 os.setsid()
56 except OSError:
57 # Good, we already were session leader
58 debug("OSError was raised.")
59 pass
60 except AttributeError:
61 # Have pty, but not setsid() ?
62 debug("AttributeError was raised.")
63 pass
64 except:
65 # We don't want this error to propagate, escape the call to
Thomas Wouters7e474022000-07-16 12:04:32 +000066 # os._exit(), and cause very peculiar behavior in the calling
Fred Drake4c136ee2000-06-30 23:22:35 +000067 # regrtest.py !
68 debug("Some other error was raised.")
69 os._exit(1)
70 else:
71 debug("os.setsid() succeeded! (bad!)")
72 os._exit(2)
73 os._exit(4)
74else:
75 debug("Waiting for child (%d) to finish."%pid)
76 (pid, status) = os.waitpid(pid, 0)
77 debug("Child (%d) exited with status %d."%(pid, status))
78 if status / 256 == 1:
79 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
80 elif status / 256 == 2:
81 raise TestFailed, "pty.fork() failed to make child a session leader."
82 elif status / 256 == 3:
83 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
84 elif status / 256 <> 4:
85 raise TestFailed, "pty.fork() failed for unknown reasons:"
86 print os.read(master_fd, 65536)
87
88os.close(master_fd)
89
90# pty.fork() passed.