|
|
28b261 |
diff --git a/Lib/test/test_pty.py b/Lib/test/test_pty.py
|
|
|
28b261 |
index bec38c45456..f623aa09620 100644
|
|
|
28b261 |
--- a/Lib/test/test_pty.py
|
|
|
28b261 |
+++ b/Lib/test/test_pty.py
|
|
|
28b261 |
@@ -11,6 +11,7 @@
|
|
|
28b261 |
import select
|
|
|
28b261 |
import signal
|
|
|
28b261 |
import socket
|
|
|
28b261 |
+import io # readline
|
|
|
28b261 |
import unittest
|
|
|
28b261 |
|
|
|
28b261 |
TEST_STRING_1 = "I wish to buy a fish license.\n"
|
|
|
28b261 |
@@ -24,6 +25,16 @@ def debug(msg):
|
|
|
28b261 |
pass
|
|
|
28b261 |
|
|
|
28b261 |
|
|
|
28b261 |
+# Note that os.read() is nondeterministic so we need to be very careful
|
|
|
28b261 |
+# to make the test suite deterministic. A normal call to os.read() may
|
|
|
28b261 |
+# give us less than expected.
|
|
|
28b261 |
+#
|
|
|
28b261 |
+# Beware, on my Linux system, if I put 'foo\n' into a terminal fd, I get
|
|
|
28b261 |
+# back 'foo\r\n' at the other end. The behavior depends on the termios
|
|
|
28b261 |
+# setting. The newline translation may be OS-specific. To make the
|
|
|
28b261 |
+# test suite deterministic and OS-independent, the functions _readline
|
|
|
28b261 |
+# and normalize_output can be used.
|
|
|
28b261 |
+
|
|
|
28b261 |
def normalize_output(data):
|
|
|
28b261 |
# Some operating systems do conversions on newline. We could possibly
|
|
|
28b261 |
# fix that by doing the appropriate termios.tcsetattr()s. I couldn't
|
|
|
28b261 |
@@ -45,6 +56,12 @@ def normalize_output(data):
|
|
|
28b261 |
|
|
|
28b261 |
return data
|
|
|
28b261 |
|
|
|
28b261 |
+def _readline(fd):
|
|
|
28b261 |
+ """Read one line. May block forever if no newline is read."""
|
|
|
28b261 |
+ reader = io.FileIO(fd, mode='rb', closefd=False)
|
|
|
28b261 |
+ return reader.readline()
|
|
|
28b261 |
+
|
|
|
28b261 |
+
|
|
|
28b261 |
|
|
|
28b261 |
# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
|
|
|
28b261 |
# because pty code is not too portable.
|
|
|
28b261 |
@@ -97,14 +114,14 @@ def test_basic(self):
|
|
|
28b261 |
|
|
|
28b261 |
debug("Writing to slave_fd")
|
|
|
28b261 |
os.write(slave_fd, TEST_STRING_1)
|
|
|
28b261 |
- s1 = os.read(master_fd, 1024)
|
|
|
28b261 |
+ s1 = _readline(master_fd)
|
|
|
28b261 |
self.assertEqual('I wish to buy a fish license.\n',
|
|
|
28b261 |
normalize_output(s1))
|
|
|
28b261 |
|
|
|
28b261 |
debug("Writing chunked output")
|
|
|
28b261 |
os.write(slave_fd, TEST_STRING_2[:5])
|
|
|
28b261 |
os.write(slave_fd, TEST_STRING_2[5:])
|
|
|
28b261 |
- s2 = os.read(master_fd, 1024)
|
|
|
28b261 |
+ s2 = _readline(master_fd)
|
|
|
28b261 |
self.assertEqual('For my pet fish, Eric.\n', normalize_output(s2))
|
|
|
28b261 |
|
|
|
28b261 |
os.close(slave_fd)
|