This commit was manufactured by cvs2svn to create tag 'r211c1'.
[python/dscho.git] / Lib / test / test_pty.py
blobb119f6275fa199f36de320284952e75ad24433c4
1 import pty, os, sys
2 from test_support import verbose, TestFailed, TestSkipped
4 TEST_STRING_1 = "I wish to buy a fish license.\n"
5 TEST_STRING_2 = "For my pet fish, Eric.\n"
7 if verbose:
8 def debug(msg):
9 print msg
10 else:
11 def debug(msg):
12 pass
14 # Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
15 # because pty code is not too portable.
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 " ... ?
26 raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
28 if not os.isatty(slave_fd):
29 raise TestFailed, "slave_fd is not a tty"
31 # IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
32 # differences (like extra whitespace, trailing garbage, etc.)
34 debug("Writing to slave_fd")
35 os.write(slave_fd, TEST_STRING_1)
36 s1 = os.read(master_fd, 1024)
37 if s1[-2:] == "\r\n":
38 s1 = s1[:-2] + "\n"
39 sys.stdout.write(s1)
41 debug("Writing chunked output")
42 os.write(slave_fd, TEST_STRING_2[:5])
43 os.write(slave_fd, TEST_STRING_2[5:])
44 s2 = os.read(master_fd, 1024)
45 if s2[-2:] == "\r\n":
46 s2 = s2[:-2] + "\n"
47 sys.stdout.write(s2)
49 os.close(slave_fd)
50 os.close(master_fd)
52 # basic pty passed.
54 debug("calling pty.fork()")
55 pid, master_fd = pty.fork()
56 if pid == pty.CHILD:
57 # stdout should be connected to a tty.
58 if not os.isatty(1):
59 debug("Child's fd 1 is not a tty?!")
60 os._exit(3)
62 # After pty.fork(), the child should already be a session leader.
63 # (on those systems that have that concept.)
64 debug("In child, calling os.setsid()")
65 try:
66 os.setsid()
67 except OSError:
68 # Good, we already were session leader
69 debug("Good: OSError was raised.")
70 pass
71 except AttributeError:
72 # Have pty, but not setsid() ?
73 debug("No setsid() available ?")
74 pass
75 except:
76 # We don't want this error to propagate, escaping the call to
77 # os._exit() and causing very peculiar behavior in the calling
78 # regrtest.py !
79 # Note: could add traceback printing here.
80 debug("An unexpected error was raised.")
81 os._exit(1)
82 else:
83 debug("os.setsid() succeeded! (bad!)")
84 os._exit(2)
85 os._exit(4)
86 else:
87 debug("Waiting for child (%d) to finish."%pid)
88 (pid, status) = os.waitpid(pid, 0)
89 res = status / 256
90 debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
91 if res == 1:
92 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
93 elif res == 2:
94 raise TestFailed, "pty.fork() failed to make child a session leader."
95 elif res == 3:
96 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
97 elif res != 4:
98 raise TestFailed, "pty.fork() failed for unknown reasons."
100 os.close(master_fd)
102 # pty.fork() passed.