blob: 02165bc131c7588b2125777b7b8e4cc2b24008a7 [file] [log] [blame]
Eric S. Raymond2846b0a2001-02-09 12:00:47 +00001import pty, os, sys
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
14# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
15# because pty code is not too portable.
16
17try:
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)
24except OSError:
25 # " An optional feature could not be imported " ... ?
Thomas Woutersb9fa0a82000-08-04 13:34:43 +000026 raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
Fred Drake4c136ee2000-06-30 23:22:35 +000027
Neal Norwitz0aae2b02002-12-31 18:21:11 +000028# this hangs on HPUX 11, comment out for now until we can determine cause
29##if not os.isatty(slave_fd):
30## raise TestFailed, "slave_fd is not a tty"
Fred Drake4c136ee2000-06-30 23:22:35 +000031
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000032# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
33# differences (like extra whitespace, trailing garbage, etc.)
Fred Drake4c136ee2000-06-30 23:22:35 +000034
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000035debug("Writing to slave_fd")
36os.write(slave_fd, TEST_STRING_1)
37s1 = os.read(master_fd, 1024)
Guido van Rossumdc795b82001-09-11 14:24:35 +000038sys.stdout.write(s1.replace("\r\n", "\n"))
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000039
40debug("Writing chunked output")
Fred Drake4c136ee2000-06-30 23:22:35 +000041os.write(slave_fd, TEST_STRING_2[:5])
42os.write(slave_fd, TEST_STRING_2[5:])
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000043s2 = os.read(master_fd, 1024)
Guido van Rossumdc795b82001-09-11 14:24:35 +000044sys.stdout.write(s2.replace("\r\n", "\n"))
Fred Drake4c136ee2000-06-30 23:22:35 +000045
46os.close(slave_fd)
47os.close(master_fd)
48
49# basic pty passed.
50
51debug("calling pty.fork()")
52pid, master_fd = pty.fork()
53if pid == pty.CHILD:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000054 # stdout should be connected to a tty.
55 if not os.isatty(1):
56 debug("Child's fd 1 is not a tty?!")
57 os._exit(3)
58
59 # After pty.fork(), the child should already be a session leader.
Tim Petersa19a1682001-03-29 04:36:09 +000060 # (on those systems that have that concept.)
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000061 debug("In child, calling os.setsid()")
Fred Drake4c136ee2000-06-30 23:22:35 +000062 try:
Fred Drake4c136ee2000-06-30 23:22:35 +000063 os.setsid()
64 except OSError:
65 # Good, we already were session leader
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000066 debug("Good: OSError was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +000067 pass
68 except AttributeError:
69 # Have pty, but not setsid() ?
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000070 debug("No setsid() available ?")
Fred Drake4c136ee2000-06-30 23:22:35 +000071 pass
72 except:
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000073 # We don't want this error to propagate, escaping the call to
74 # os._exit() and causing very peculiar behavior in the calling
Fred Drake4c136ee2000-06-30 23:22:35 +000075 # regrtest.py !
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000076 # Note: could add traceback printing here.
77 debug("An unexpected error was raised.")
Fred Drake4c136ee2000-06-30 23:22:35 +000078 os._exit(1)
79 else:
80 debug("os.setsid() succeeded! (bad!)")
81 os._exit(2)
82 os._exit(4)
83else:
84 debug("Waiting for child (%d) to finish."%pid)
85 (pid, status) = os.waitpid(pid, 0)
Guido van Rossum54e54c62001-09-04 19:14:14 +000086 res = status >> 8
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000087 debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
88 if res == 1:
Fred Drake4c136ee2000-06-30 23:22:35 +000089 raise TestFailed, "Child raised an unexpected exception in os.setsid()"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000090 elif res == 2:
Fred Drake4c136ee2000-06-30 23:22:35 +000091 raise TestFailed, "pty.fork() failed to make child a session leader."
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000092 elif res == 3:
Fred Drake4c136ee2000-06-30 23:22:35 +000093 raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
Thomas Woutersb0dbeef2001-03-22 14:50:24 +000094 elif res != 4:
95 raise TestFailed, "pty.fork() failed for unknown reasons."
Fred Drake4c136ee2000-06-30 23:22:35 +000096
97os.close(master_fd)
98
99# pty.fork() passed.