blob: 9fe038ab8b8e09b63ecdae6c46df02f88d2cf82b [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
Thomas Wouters35f34f82006-01-28 12:05:54 +00007# Solaris (at least 2.9 and 2.10) seem to have a ficke isatty(). The first
8# 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
21# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
22# because pty code is not too portable.
23
Neal Norwitz7d814522003-03-21 01:39:14 +000024def test_basic_pty():
25 try:
26 debug("Calling master_open()")
27 master_fd, slave_name = pty.master_open()
28 debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name))
Walter Dörwald70a6b492004-02-12 17:35:32 +000029 debug("Calling slave_open(%r)"%(slave_name,))
Neal Norwitz7d814522003-03-21 01:39:14 +000030 slave_fd = pty.slave_open(slave_name)
31 debug("Got slave_fd '%d'"%slave_fd)
32 except OSError:
33 # " An optional feature could not be imported " ... ?
34 raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
35
Thomas Wouters35f34f82006-01-28 12:05:54 +000036 if not os.isatty(slave_fd) and sys.platform not in fickle_isatty:
Neal Norwitz7d814522003-03-21 01:39:14 +000037 raise TestFailed, "slave_fd is not a tty"
38
39 # IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
40 # differences (like extra whitespace, trailing garbage, etc.)
41
42 debug("Writing to slave_fd")
43 os.write(slave_fd, TEST_STRING_1)
44 s1 = os.read(master_fd, 1024)
45 sys.stdout.write(s1.replace("\r\n", "\n"))
46
47 debug("Writing chunked output")
48 os.write(slave_fd, TEST_STRING_2[:5])
49 os.write(slave_fd, TEST_STRING_2[5:])
50 s2 = os.read(master_fd, 1024)
51 sys.stdout.write(s2.replace("\r\n", "\n"))
52
53 os.close(slave_fd)
54 os.close(master_fd)
55
56def handle_sig(sig, frame):
57 raise TestFailed, "isatty hung"
58
59# isatty() and close() can hang on some platforms
60# set an alarm before running the test to make sure we don't hang forever
61old_alarm = signal.signal(signal.SIGALRM, handle_sig)
62signal.alarm(10)
63
Fred Drake4c136ee2000-06-30 23:22:35 +000064try:
Neal Norwitz7d814522003-03-21 01:39:14 +000065 test_basic_pty()
66finally:
67 # remove alarm, restore old alarm handler
68 signal.alarm(0)
69 signal.signal(signal.SIGALRM, old_alarm)
Fred Drake4c136ee2000-06-30 23:22:35 +000070
71# basic pty passed.
72
73debug("calling pty.fork()")
74pid, master_fd = pty.fork()
75if pid == pty.CHILD:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000076 # stdout should be connected to a tty.
77 if not os.isatty(1):
78 debug("Child's fd 1 is not a tty?!")
79 os._exit(3)
80
81 # After pty.fork(), the child should already be a session leader.
Tim Petersa19a1682001-03-29 04:36:09 +000082 # (on those systems that have that concept.)
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000083 debug("In child, calling os.setsid()")
Fred Drake4c136ee2000-06-30 23:22:35 +000084 try:
Fred Drake4c136ee2000-06-30 23:22:35 +000085 os.setsid()
86 except OSError:
87 # Good, we already were session leader
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000088 debug("Good: OSError was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +000089 pass
90 except AttributeError:
91 # Have pty, but not setsid() ?
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000092 debug("No setsid() available ?")
Fred Drake4c136ee2000-06-30 23:22:35 +000093 pass
94 except:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000095 # We don't want this error to propagate, escaping the call to
96 # os._exit() and causing very peculiar behavior in the calling
Fred Drake4c136ee2000-06-30 23:22:35 +000097 # regrtest.py !
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000098 # Note: could add traceback printing here.
99 debug("An unexpected error was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +0000100 os._exit(1)
101 else:
102 debug("os.setsid() succeeded! (bad!)")
103 os._exit(2)
104 os._exit(4)
105else:
106 debug("Waiting for child (%d) to finish."%pid)
107 (pid, status) = os.waitpid(pid, 0)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000108 res = status >> 8
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000109 debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
110 if res == 1:
Fred Drake4c136ee2000-06-30 23:22:35 +0000111 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000112 elif res == 2:
Fred Drake4c136ee2000-06-30 23:22:35 +0000113 raise TestFailed, "pty.fork() failed to make child a session leader."
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000114 elif res == 3:
Fred Drake4c136ee2000-06-30 23:22:35 +0000115 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +0000116 elif res != 4:
117 raise TestFailed, "pty.fork() failed for unknown reasons."
Fred Drake4c136ee2000-06-30 23:22:35 +0000118
119os.close(master_fd)
120
121# pty.fork() passed.