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

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