churchyard / rpms / python2

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