Skip to content

Commit 4c136ee

Browse files
committed
Thomas Wouters <thomas@xs4all.net>:
Test case for the pty module.
1 parent 276fa43 commit 4c136ee

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lib/test/output/test_pty

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_pty
2+
I wish to buy a fish license.
3+
For my pet fish, Eric.

Lib/test/test_pty.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pty, os, sys, string
2+
from test_support import verbose, TestFailed
3+
4+
TEST_STRING_1 = "I wish to buy a fish license."
5+
TEST_STRING_2 = "For my pet fish, Eric."
6+
TEST_STRING_3 = "And now for something completely different..."
7+
TEST_STRING_4 = "but you pronounce it throatwobbler mangrove."
8+
9+
if verbose:
10+
def debug(msg):
11+
print msg
12+
else:
13+
def debug(msg):
14+
pass
15+
16+
# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
17+
# because pty code is not too portable.
18+
19+
try:
20+
debug("Calling master_open()")
21+
master_fd, slave_name = pty.master_open()
22+
debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name))
23+
debug("Calling slave_open(%s)"%`slave_name`)
24+
slave_fd = pty.slave_open(slave_name)
25+
debug("Got slave_fd '%d'"%slave_fd)
26+
except OSError:
27+
# " An optional feature could not be imported " ... ?
28+
raise ImportError, "Pseudo-terminals (seemingly) not functional."
29+
30+
## # Please uncomment these if os.isatty() is added.
31+
## if not os.isatty(master_fd):
32+
## raise TestFailed, "master_fd is not a tty"
33+
## if not os.isatty(slave_fd):
34+
## raise TestFailed, "slave_fd is not a tty"
35+
36+
debug("Writing to slave_fd")
37+
os.write(slave_fd, TEST_STRING_1) # should check return value
38+
print os.read(master_fd, 1024)
39+
40+
os.write(slave_fd, TEST_STRING_2[:5])
41+
os.write(slave_fd, TEST_STRING_2[5:])
42+
print os.read(master_fd, 1024)
43+
44+
os.close(slave_fd)
45+
os.close(master_fd)
46+
47+
# basic pty passed.
48+
49+
debug("calling pty.fork()")
50+
pid, master_fd = pty.fork()
51+
if pid == pty.CHILD:
52+
## # Please uncomment these when os.isatty() is added.
53+
## if not os.isatty(1):
54+
## debug("Child's fd 1 is not a tty?!")
55+
## os._exit(3)
56+
try:
57+
debug("In child, calling os.setsid()")
58+
os.setsid()
59+
except OSError:
60+
# Good, we already were session leader
61+
debug("OSError was raised.")
62+
pass
63+
except AttributeError:
64+
# Have pty, but not setsid() ?
65+
debug("AttributeError was raised.")
66+
pass
67+
except:
68+
# We don't want this error to propagate, escape the call to
69+
# os._exit(), and cause very peculiar behaviour in the calling
70+
# regrtest.py !
71+
debug("Some other error was raised.")
72+
os._exit(1)
73+
else:
74+
debug("os.setsid() succeeded! (bad!)")
75+
os._exit(2)
76+
os._exit(4)
77+
else:
78+
debug("Waiting for child (%d) to finish."%pid)
79+
(pid, status) = os.waitpid(pid, 0)
80+
debug("Child (%d) exited with status %d."%(pid, status))
81+
if status / 256 == 1:
82+
raise TestFailed, "Child raised an unexpected exception in os.setsid()"
83+
elif status / 256 == 2:
84+
raise TestFailed, "pty.fork() failed to make child a session leader."
85+
elif status / 256 == 3:
86+
raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
87+
elif status / 256 <> 4:
88+
raise TestFailed, "pty.fork() failed for unknown reasons:"
89+
print os.read(master_fd, 65536)
90+
91+
os.close(master_fd)
92+
93+
# pty.fork() passed.
94+

0 commit comments

Comments
 (0)