blob: 59e51627ddce24c4cb5687380ea654be38a0b042 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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.
18 # So just normalize the output and doc the problem O/Ses by allowing
19 # 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'):
Thomas Wouters477c8d52006-05-27 19:21:47 +000027 return data.replace('\r\r\n', '\n')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028
29 # IRIX apparently turns \n into \r\n.
30 if data.endswith('\r\n'):
Thomas Wouters477c8d52006-05-27 19:21:47 +000031 return data.replace('\r\n', '\n')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 Wouters477c8d52006-05-27 19:21:47 +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)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)
118 (pid, status) = os.waitpid(pid, 0)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000119 res = status >> 8
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000120 debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
121 if res == 1:
Fred Drake4c136ee2000-06-30 23:22:35 +0000122 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000123 elif res == 2:
Fred Drake4c136ee2000-06-30 23:22:35 +0000124 raise TestFailed, "pty.fork() failed to make child a session leader."
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000125 elif res == 3:
Fred Drake4c136ee2000-06-30 23:22:35 +0000126 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000127 elif res != 4:
128 raise TestFailed, "pty.fork() failed for unknown reasons."
Fred Drake4c136ee2000-06-30 23:22:35 +0000129
130os.close(master_fd)
131
132# pty.fork() passed.