blob: 99e01b67bb067b109dfe6d05d160cf36427b699e [file] [log] [blame]
Neal Norwitz7d814522003-03-21 01:39:14 +00001import pty, os, sys, signal
Barry Warsaw04f357c2002-07-23 19:04:11 +00002from test.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
Neal Norwitzfaa26df2006-02-04 03:26:20 +00007# Solaris (at least 2.9 and 2.10) seem to have a fickle isatty(). The first
Thomas Wouters35f34f82006-01-28 12:05:54 +00008# test below, testing the result of os.openpty() for tty-ness, sometimes
9# (but not always) fails. The second isatty test, in the sub-process, always
10# works. Allow that fickle first test to fail on these platforms, since it
11# doesn't actually affect functionality.
12fickle_isatty = ["sunos5"]
13
Fred Drake4c136ee2000-06-30 23:22:35 +000014if verbose:
15 def debug(msg):
16 print msg
17else:
18 def debug(msg):
19 pass
20
Neal Norwitz84c95b92006-04-03 05:28:31 +000021def normalize_output(data):
22 # Some operating systems do conversions on newline. We could possibly
23 # fix that by doing the appropriate termios.tcsetattr()s. I couldn't
24 # figure out the right combo on Tru64 and I don't have an IRIX box.
Anthony Baxtera2a26b92006-04-05 17:30:38 +000025 # So just normalize the output and doc the problem O/Ses by allowing
Neal Norwitz84c95b92006-04-03 05:28:31 +000026 # certain combinations for some platforms, but avoid allowing other
27 # differences (like extra whitespace, trailing garbage, etc.)
28
29 # This is about the best we can do without getting some feedback
30 # from someone more knowledgable.
31
32 # OSF/1 (Tru64) apparently turns \n into \r\r\n.
33 if data.endswith('\r\r\n'):
34 return data[:-3] + '\n'
35
36 # IRIX apparently turns \n into \r\n.
37 if data.endswith('\r\n'):
38 return data[:-2] + '\n'
39
40 return data
41
Fred Drake4c136ee2000-06-30 23:22:35 +000042# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
43# because pty code is not too portable.
44
Neal Norwitz7d814522003-03-21 01:39:14 +000045def test_basic_pty():
46 try:
47 debug("Calling master_open()")
48 master_fd, slave_name = pty.master_open()
49 debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name))
Walter Dörwald70a6b492004-02-12 17:35:32 +000050 debug("Calling slave_open(%r)"%(slave_name,))
Neal Norwitz7d814522003-03-21 01:39:14 +000051 slave_fd = pty.slave_open(slave_name)
52 debug("Got slave_fd '%d'"%slave_fd)
53 except OSError:
54 # " An optional feature could not be imported " ... ?
55 raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
56
Thomas Wouters35f34f82006-01-28 12:05:54 +000057 if not os.isatty(slave_fd) and sys.platform not in fickle_isatty:
Neal Norwitz7d814522003-03-21 01:39:14 +000058 raise TestFailed, "slave_fd is not a tty"
59
Neal Norwitz7d814522003-03-21 01:39:14 +000060 debug("Writing to slave_fd")
61 os.write(slave_fd, TEST_STRING_1)
62 s1 = os.read(master_fd, 1024)
Neal Norwitz84c95b92006-04-03 05:28:31 +000063 sys.stdout.write(normalize_output(s1))
Neal Norwitz7d814522003-03-21 01:39:14 +000064
65 debug("Writing chunked output")
66 os.write(slave_fd, TEST_STRING_2[:5])
67 os.write(slave_fd, TEST_STRING_2[5:])
68 s2 = os.read(master_fd, 1024)
Neal Norwitz84c95b92006-04-03 05:28:31 +000069 sys.stdout.write(normalize_output(s2))
Neal Norwitz7d814522003-03-21 01:39:14 +000070
71 os.close(slave_fd)
72 os.close(master_fd)
73
74def handle_sig(sig, frame):
75 raise TestFailed, "isatty hung"
76
77# isatty() and close() can hang on some platforms
78# set an alarm before running the test to make sure we don't hang forever
79old_alarm = signal.signal(signal.SIGALRM, handle_sig)
80signal.alarm(10)
81
Fred Drake4c136ee2000-06-30 23:22:35 +000082try:
Neal Norwitz7d814522003-03-21 01:39:14 +000083 test_basic_pty()
84finally:
85 # remove alarm, restore old alarm handler
86 signal.alarm(0)
87 signal.signal(signal.SIGALRM, old_alarm)
Fred Drake4c136ee2000-06-30 23:22:35 +000088
89# basic pty passed.
90
91debug("calling pty.fork()")
92pid, master_fd = pty.fork()
93if pid == pty.CHILD:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000094 # stdout should be connected to a tty.
95 if not os.isatty(1):
96 debug("Child's fd 1 is not a tty?!")
97 os._exit(3)
98
99 # After pty.fork(), the child should already be a session leader.
Tim Petersa19a1682001-03-29 04:36:09 +0000100 # (on those systems that have that concept.)
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000101 debug("In child, calling os.setsid()")
Fred Drake4c136ee2000-06-30 23:22:35 +0000102 try:
Fred Drake4c136ee2000-06-30 23:22:35 +0000103 os.setsid()
104 except OSError:
105 # Good, we already were session leader
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000106 debug("Good: OSError was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +0000107 pass
108 except AttributeError:
109 # Have pty, but not setsid() ?
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000110 debug("No setsid() available ?")
Fred Drake4c136ee2000-06-30 23:22:35 +0000111 pass
112 except:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000113 # We don't want this error to propagate, escaping the call to
114 # os._exit() and causing very peculiar behavior in the calling
Fred Drake4c136ee2000-06-30 23:22:35 +0000115 # regrtest.py !
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000116 # Note: could add traceback printing here.
117 debug("An unexpected error was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +0000118 os._exit(1)
119 else:
120 debug("os.setsid() succeeded! (bad!)")
121 os._exit(2)
122 os._exit(4)
123else:
124 debug("Waiting for child (%d) to finish."%pid)
125 (pid, status) = os.waitpid(pid, 0)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000126 res = status >> 8
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000127 debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
128 if res == 1:
Fred Drake4c136ee2000-06-30 23:22:35 +0000129 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000130 elif res == 2:
Fred Drake4c136ee2000-06-30 23:22:35 +0000131 raise TestFailed, "pty.fork() failed to make child a session leader."
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000132 elif res == 3:
Fred Drake4c136ee2000-06-30 23:22:35 +0000133 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000134 elif res != 4:
135 raise TestFailed, "pty.fork() failed for unknown reasons."
Fred Drake4c136ee2000-06-30 23:22:35 +0000136
137os.close(master_fd)
138
139# pty.fork() passed.