Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0dbeef

Browse files
committed
Allow the process of reading back what we wrote to a pty to transform
linefeeds into carriagereturn-linefeeds (which is apparently what IRIX does.) Also add some comments, an extra test and reorganize it a bit.
1 parent 053ae35 commit b0dbeef

1 file changed

Lines changed: 36 additions & 24 deletions

File tree

Lib/test/test_pty.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import pty, os, sys
22
from test_support import verbose, TestFailed, TestSkipped
33

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."
4+
TEST_STRING_1 = "I wish to buy a fish license.\n"
5+
TEST_STRING_2 = "For my pet fish, Eric.\n"
86

97
if verbose:
108
def debug(msg):
@@ -30,13 +28,23 @@ def debug(msg):
3028
if not os.isatty(slave_fd):
3129
raise TestFailed, "slave_fd is not a tty"
3230

31+
# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
32+
# differences (like extra whitespace, trailing garbage, etc.)
33+
3334
debug("Writing to slave_fd")
34-
os.write(slave_fd, TEST_STRING_1) # should check return value
35-
print os.read(master_fd, 1024)
35+
os.write(slave_fd, TEST_STRING_1)
36+
s1 = os.read(master_fd, 1024)
37+
if s1[-2:] == "\r\n":
38+
s1 = s1[:-2] + "\n"
39+
sys.stdout.write(s1)
3640

41+
debug("Writing chunked output")
3742
os.write(slave_fd, TEST_STRING_2[:5])
3843
os.write(slave_fd, TEST_STRING_2[5:])
39-
print os.read(master_fd, 1024)
44+
s2 = os.read(master_fd, 1024)
45+
if s2[-2:] == "\r\n":
46+
s2 = s2[:-2] + "\n"
47+
sys.stdout.write(s2)
4048

4149
os.close(slave_fd)
4250
os.close(master_fd)
@@ -46,26 +54,30 @@ def debug(msg):
4654
debug("calling pty.fork()")
4755
pid, master_fd = pty.fork()
4856
if pid == pty.CHILD:
49-
## # Please uncomment these when os.isatty() is added.
50-
## if not os.isatty(1):
51-
## debug("Child's fd 1 is not a tty?!")
52-
## os._exit(3)
57+
# stdout should be connected to a tty.
58+
if not os.isatty(1):
59+
debug("Child's fd 1 is not a tty?!")
60+
os._exit(3)
61+
62+
# After pty.fork(), the child should already be a session leader.
63+
# (on those systems that have that concept.)
64+
debug("In child, calling os.setsid()")
5365
try:
54-
debug("In child, calling os.setsid()")
5566
os.setsid()
5667
except OSError:
5768
# Good, we already were session leader
58-
debug("OSError was raised.")
69+
debug("Good: OSError was raised.")
5970
pass
6071
except AttributeError:
6172
# Have pty, but not setsid() ?
62-
debug("AttributeError was raised.")
73+
debug("No setsid() available ?")
6374
pass
6475
except:
65-
# We don't want this error to propagate, escape the call to
66-
# os._exit(), and cause very peculiar behavior in the calling
76+
# We don't want this error to propagate, escaping the call to
77+
# os._exit() and causing very peculiar behavior in the calling
6778
# regrtest.py !
68-
debug("Some other error was raised.")
79+
# Note: could add traceback printing here.
80+
debug("An unexpected error was raised.")
6981
os._exit(1)
7082
else:
7183
debug("os.setsid() succeeded! (bad!)")
@@ -74,16 +86,16 @@ def debug(msg):
7486
else:
7587
debug("Waiting for child (%d) to finish."%pid)
7688
(pid, status) = os.waitpid(pid, 0)
77-
debug("Child (%d) exited with status %d."%(pid, status))
78-
if status / 256 == 1:
89+
res = status / 256
90+
debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
91+
if res == 1:
7992
raise TestFailed, "Child raised an unexpected exception in os.setsid()"
80-
elif status / 256 == 2:
93+
elif res == 2:
8194
raise TestFailed, "pty.fork() failed to make child a session leader."
82-
elif status / 256 == 3:
95+
elif res == 3:
8396
raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
84-
elif status / 256 != 4:
85-
raise TestFailed, "pty.fork() failed for unknown reasons:"
86-
print os.read(master_fd, 65536)
97+
elif res != 4:
98+
raise TestFailed, "pty.fork() failed for unknown reasons."
8799

88100
os.close(master_fd)
89101

0 commit comments

Comments
 (0)