Blame SOURCES/00285-fix-non-deterministic-read-in-test_pty.patch

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