blob: bd0a285232505aceb9b4c147b4afd224bbf49628 [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
7if verbose:
8 def debug(msg):
9 print msg
10else:
11 def debug(msg):
12 pass
13
Neal Norwitz84c95b92006-04-03 05:28:31 +000014def normalize_output(data):
15 # Some operating systems do conversions on newline. We could possibly
16 # fix that by doing the appropriate termios.tcsetattr()s. I couldn't
17 # figure out the right combo on Tru64 and I don't have an IRIX box.
Anthony Baxtera2a26b92006-04-05 17:30:38 +000018 # So just normalize the output and doc the problem O/Ses by allowing
Neal Norwitz84c95b92006-04-03 05:28:31 +000019 # certain combinations for some platforms, but avoid allowing other
20 # differences (like extra whitespace, trailing garbage, etc.)
21
22 # This is about the best we can do without getting some feedback
23 # from someone more knowledgable.
24
25 # OSF/1 (Tru64) apparently turns \n into \r\r\n.
26 if data.endswith('\r\r\n'):
Neal Norwitz9cc3b1c2006-04-26 06:26:12 +000027 return data.replace('\r\r\n', '\n')
Neal Norwitz84c95b92006-04-03 05:28:31 +000028
29 # IRIX apparently turns \n into \r\n.
30 if data.endswith('\r\n'):
Neal Norwitz9cc3b1c2006-04-26 06:26:12 +000031 return data.replace('\r\n', '\n')
Neal Norwitz84c95b92006-04-03 05:28:31 +000032
33 return data
34
Fred Drake4c136ee2000-06-30 23:22:35 +000035# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
36# because pty code is not too portable.
37
Neal Norwitz7d814522003-03-21 01:39:14 +000038def test_basic_pty():
39 try:
40 debug("Calling master_open()")
41 master_fd, slave_name = pty.master_open()
42 debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name))
Walter Dörwald70a6b492004-02-12 17:35:32 +000043 debug("Calling slave_open(%r)"%(slave_name,))
Neal Norwitz7d814522003-03-21 01:39:14 +000044 slave_fd = pty.slave_open(slave_name)
45 debug("Got slave_fd '%d'"%slave_fd)
46 except OSError:
47 # " An optional feature could not be imported " ... ?
48 raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
49
Thomas Wouters6dbff332006-04-25 13:53:23 +000050 if not os.isatty(slave_fd):
Neal Norwitz7d814522003-03-21 01:39:14 +000051 raise TestFailed, "slave_fd is not a tty"
52
Neal Norwitz7d814522003-03-21 01:39:14 +000053 debug("Writing to slave_fd")
54 os.write(slave_fd, TEST_STRING_1)
55 s1 = os.read(master_fd, 1024)
Neal Norwitz84c95b92006-04-03 05:28:31 +000056 sys.stdout.write(normalize_output(s1))
Neal Norwitz7d814522003-03-21 01:39:14 +000057
58 debug("Writing chunked output")
59 os.write(slave_fd, TEST_STRING_2[:5])
60 os.write(slave_fd, TEST_STRING_2[5:])
61 s2 = os.read(master_fd, 1024)
Neal Norwitz84c95b92006-04-03 05:28:31 +000062 sys.stdout.write(normalize_output(s2))
Neal Norwitz7d814522003-03-21 01:39:14 +000063
64 os.close(slave_fd)
65 os.close(master_fd)
66
67def handle_sig(sig, frame):
68 raise TestFailed, "isatty hung"
69
70# isatty() and close() can hang on some platforms
71# set an alarm before running the test to make sure we don't hang forever
72old_alarm = signal.signal(signal.SIGALRM, handle_sig)
73signal.alarm(10)
74
Fred Drake4c136ee2000-06-30 23:22:35 +000075try:
Neal Norwitz7d814522003-03-21 01:39:14 +000076 test_basic_pty()
77finally:
78 # remove alarm, restore old alarm handler
79 signal.alarm(0)
80 signal.signal(signal.SIGALRM, old_alarm)
Fred Drake4c136ee2000-06-30 23:22:35 +000081
82# basic pty passed.
83
84debug("calling pty.fork()")
85pid, master_fd = pty.fork()
86if pid == pty.CHILD:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000087 # stdout should be connected to a tty.
88 if not os.isatty(1):
89 debug("Child's fd 1 is not a tty?!")
90 os._exit(3)
91
92 # After pty.fork(), the child should already be a session leader.
Tim Petersa19a1682001-03-29 04:36:09 +000093 # (on those systems that have that concept.)
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000094 debug("In child, calling os.setsid()")
Fred Drake4c136ee2000-06-30 23:22:35 +000095 try:
Fred Drake4c136ee2000-06-30 23:22:35 +000096 os.setsid()
97 except OSError:
98 # Good, we already were session leader
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000099 debug("Good: OSError was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +0000100 pass
101 except AttributeError:
102 # Have pty, but not setsid() ?
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000103 debug("No setsid() available ?")
Fred Drake4c136ee2000-06-30 23:22:35 +0000104 pass
105 except:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000106 # We don't want this error to propagate, escaping the call to
107 # os._exit() and causing very peculiar behavior in the calling
Fred Drake4c136ee2000-06-30 23:22:35 +0000108 # regrtest.py !
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000109 # Note: could add traceback printing here.
110 debug("An unexpected error was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +0000111 os._exit(1)
112 else:
113 debug("os.setsid() succeeded! (bad!)")
114 os._exit(2)
115 os._exit(4)
116else:
117 debug("Waiting for child (%d) to finish."%pid)
Andrew M. Kuchlingf2881e82006-12-22 19:21:27 +0000118 ##line = os.read(master_fd, 80)
119 ##lines = line.replace('\r\n', '\n').split('\n')
120 ##if False and lines != ['In child, calling os.setsid()',
121 ## 'Good: OSError was raised.', '']:
122 ## raise TestFailed("Unexpected output from child: %r" % line)
Andrew M. Kuchlingee0e6d12006-12-22 18:41:42 +0000123
Fred Drake4c136ee2000-06-30 23:22:35 +0000124 (pid, status) = os.waitpid(pid, 0)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000125 res = status >> 8
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000126 debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
127 if res == 1:
Fred Drake4c136ee2000-06-30 23:22:35 +0000128 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000129 elif res == 2:
Fred Drake4c136ee2000-06-30 23:22:35 +0000130 raise TestFailed, "pty.fork() failed to make child a session leader."
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000131 elif res == 3:
Fred Drake4c136ee2000-06-30 23:22:35 +0000132 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000133 elif res != 4:
134 raise TestFailed, "pty.fork() failed for unknown reasons."
Fred Drake4c136ee2000-06-30 23:22:35 +0000135
Andrew M. Kuchlingee0e6d12006-12-22 18:41:42 +0000136 debug("Reading from master_fd now that the child has exited")
137 try:
138 s1 = os.read(master_fd, 1024)
139 except os.error:
140 pass
141 else:
142 raise TestFailed("Read from master_fd did not raise exception")
143
144
Fred Drake4c136ee2000-06-30 23:22:35 +0000145os.close(master_fd)
146
147# pty.fork() passed.