import pty
, os
, sys
, signal
from test
.test_support
import verbose
, TestFailed
, TestSkipped
TEST_STRING_1
= "I wish to buy a fish license.\n"
TEST_STRING_2
= "For my pet fish, Eric.\n"
# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
# because pty code is not too portable.
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(%r)"%(slave_name
,))
slave_fd
= pty
.slave_open(slave_name
)
debug("Got slave_fd '%d'"%slave
_fd
)
# " An optional feature could not be imported " ... ?
raise TestSkipped
, "Pseudo-terminals (seemingly) not functional."
if not os
.isatty(slave_fd
):
raise TestFailed
, "slave_fd is not a tty"
# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
# differences (like extra whitespace, trailing garbage, etc.)
debug("Writing to slave_fd")
os
.write(slave_fd
, TEST_STRING_1
)
s1
= os
.read(master_fd
, 1024)
sys
.stdout
.write(s1
.replace("\r\n", "\n"))
debug("Writing chunked output")
os
.write(slave_fd
, TEST_STRING_2
[:5])
os
.write(slave_fd
, TEST_STRING_2
[5:])
s2
= os
.read(master_fd
, 1024)
sys
.stdout
.write(s2
.replace("\r\n", "\n"))
def handle_sig(sig
, frame
):
raise TestFailed
, "isatty hung"
# isatty() and close() can hang on some platforms
# set an alarm before running the test to make sure we don't hang forever
old_alarm
= signal
.signal(signal
.SIGALRM
, handle_sig
)
# remove alarm, restore old alarm handler
signal
.signal(signal
.SIGALRM
, old_alarm
)
debug("calling pty.fork()")
pid
, master_fd
= pty
.fork()
# stdout should be connected to a tty.
debug("Child's fd 1 is not a tty?!")
# After pty.fork(), the child should already be a session leader.
# (on those systems that have that concept.)
debug("In child, calling os.setsid()")
# Good, we already were session leader
debug("Good: OSError was raised.")
# Have pty, but not setsid() ?
debug("No setsid() available ?")
# We don't want this error to propagate, escaping the call to
# os._exit() and causing very peculiar behavior in the calling
# Note: could add traceback printing here.
debug("An unexpected error was raised.")
debug("os.setsid() succeeded! (bad!)")
debug("Waiting for child (%d) to finish."%pid
)
(pid
, status
) = os
.waitpid(pid
, 0)
debug("Child (%d) exited with status %d (%d)."%(pid
, res
, status
))
raise TestFailed
, "Child raised an unexpected exception in os.setsid()"
raise TestFailed
, "pty.fork() failed to make child a session leader."
raise TestFailed
, "Child spawned by pty.fork() did not have a tty as stdout"
raise TestFailed
, "pty.fork() failed for unknown reasons."