churchyard / rpms / python2

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