2001-02-09 08:00:47 -04:00
|
|
|
import pty, os, sys
|
2001-01-17 17:51:36 -04:00
|
|
|
from test_support import verbose, TestFailed, TestSkipped
|
2000-06-30 20:22:35 -03:00
|
|
|
|
2001-03-22 10:50:24 -04:00
|
|
|
TEST_STRING_1 = "I wish to buy a fish license.\n"
|
|
|
|
TEST_STRING_2 = "For my pet fish, Eric.\n"
|
2000-06-30 20:22:35 -03:00
|
|
|
|
|
|
|
if verbose:
|
|
|
|
def debug(msg):
|
|
|
|
print msg
|
|
|
|
else:
|
|
|
|
def debug(msg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
|
|
|
|
# because pty code is not too portable.
|
|
|
|
|
|
|
|
try:
|
|
|
|
debug("Calling master_open()")
|
|
|
|
master_fd, slave_name = pty.master_open()
|
|
|
|
debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name))
|
|
|
|
debug("Calling slave_open(%s)"%`slave_name`)
|
|
|
|
slave_fd = pty.slave_open(slave_name)
|
|
|
|
debug("Got slave_fd '%d'"%slave_fd)
|
|
|
|
except OSError:
|
|
|
|
# " An optional feature could not be imported " ... ?
|
2000-08-04 10:34:43 -03:00
|
|
|
raise TestSkipped, "Pseudo-terminals (seemingly) not functional."
|
2000-06-30 20:22:35 -03:00
|
|
|
|
2000-07-19 11:51:54 -03:00
|
|
|
if not os.isatty(slave_fd):
|
|
|
|
raise TestFailed, "slave_fd is not a tty"
|
2000-06-30 20:22:35 -03:00
|
|
|
|
2001-03-22 10:50:24 -04:00
|
|
|
# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
|
|
|
|
# differences (like extra whitespace, trailing garbage, etc.)
|
|
|
|
|
2000-06-30 20:22:35 -03:00
|
|
|
debug("Writing to slave_fd")
|
2001-03-22 10:50:24 -04:00
|
|
|
os.write(slave_fd, TEST_STRING_1)
|
|
|
|
s1 = os.read(master_fd, 1024)
|
|
|
|
if s1[-2:] == "\r\n":
|
|
|
|
s1 = s1[:-2] + "\n"
|
|
|
|
sys.stdout.write(s1)
|
2000-06-30 20:22:35 -03:00
|
|
|
|
2001-03-22 10:50:24 -04:00
|
|
|
debug("Writing chunked output")
|
2000-06-30 20:22:35 -03:00
|
|
|
os.write(slave_fd, TEST_STRING_2[:5])
|
|
|
|
os.write(slave_fd, TEST_STRING_2[5:])
|
2001-03-22 10:50:24 -04:00
|
|
|
s2 = os.read(master_fd, 1024)
|
|
|
|
if s2[-2:] == "\r\n":
|
|
|
|
s2 = s2[:-2] + "\n"
|
|
|
|
sys.stdout.write(s2)
|
2000-06-30 20:22:35 -03:00
|
|
|
|
|
|
|
os.close(slave_fd)
|
|
|
|
os.close(master_fd)
|
|
|
|
|
|
|
|
# basic pty passed.
|
|
|
|
|
|
|
|
debug("calling pty.fork()")
|
|
|
|
pid, master_fd = pty.fork()
|
|
|
|
if pid == pty.CHILD:
|
2001-03-22 10:50:24 -04:00
|
|
|
# stdout should be connected to a tty.
|
|
|
|
if not os.isatty(1):
|
|
|
|
debug("Child's fd 1 is not a tty?!")
|
|
|
|
os._exit(3)
|
|
|
|
|
|
|
|
# After pty.fork(), the child should already be a session leader.
|
2001-03-29 00:36:09 -04:00
|
|
|
# (on those systems that have that concept.)
|
2001-03-22 10:50:24 -04:00
|
|
|
debug("In child, calling os.setsid()")
|
2000-06-30 20:22:35 -03:00
|
|
|
try:
|
|
|
|
os.setsid()
|
|
|
|
except OSError:
|
|
|
|
# Good, we already were session leader
|
2001-03-22 10:50:24 -04:00
|
|
|
debug("Good: OSError was raised.")
|
2000-06-30 20:22:35 -03:00
|
|
|
pass
|
|
|
|
except AttributeError:
|
|
|
|
# Have pty, but not setsid() ?
|
2001-03-22 10:50:24 -04:00
|
|
|
debug("No setsid() available ?")
|
2000-06-30 20:22:35 -03:00
|
|
|
pass
|
|
|
|
except:
|
2001-03-22 10:50:24 -04:00
|
|
|
# We don't want this error to propagate, escaping the call to
|
|
|
|
# os._exit() and causing very peculiar behavior in the calling
|
2000-06-30 20:22:35 -03:00
|
|
|
# regrtest.py !
|
2001-03-22 10:50:24 -04:00
|
|
|
# Note: could add traceback printing here.
|
|
|
|
debug("An unexpected error was raised.")
|
2000-06-30 20:22:35 -03:00
|
|
|
os._exit(1)
|
|
|
|
else:
|
|
|
|
debug("os.setsid() succeeded! (bad!)")
|
|
|
|
os._exit(2)
|
|
|
|
os._exit(4)
|
|
|
|
else:
|
|
|
|
debug("Waiting for child (%d) to finish."%pid)
|
|
|
|
(pid, status) = os.waitpid(pid, 0)
|
2001-03-22 10:50:24 -04:00
|
|
|
res = status / 256
|
|
|
|
debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
|
|
|
|
if res == 1:
|
2000-06-30 20:22:35 -03:00
|
|
|
raise TestFailed, "Child raised an unexpected exception in os.setsid()"
|
2001-03-22 10:50:24 -04:00
|
|
|
elif res == 2:
|
2000-06-30 20:22:35 -03:00
|
|
|
raise TestFailed, "pty.fork() failed to make child a session leader."
|
2001-03-22 10:50:24 -04:00
|
|
|
elif res == 3:
|
2000-06-30 20:22:35 -03:00
|
|
|
raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
|
2001-03-22 10:50:24 -04:00
|
|
|
elif res != 4:
|
|
|
|
raise TestFailed, "pty.fork() failed for unknown reasons."
|
2000-06-30 20:22:35 -03:00
|
|
|
|
|
|
|
os.close(master_fd)
|
|
|
|
|
|
|
|
# pty.fork() passed.
|