Fix nondeterministic read in test_pty

This commit is contained in:
Charalampos Stratakis 2017-11-02 16:44:28 +01:00
parent f948d41aff
commit 9d3ac306c1
2 changed files with 66 additions and 0 deletions

View File

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

View File

@ -740,6 +740,11 @@ Patch283: 00283-fix-tests_with_COUNT_ALLOCS.patch
# Backported from upstream: https://bugs.python.org/issue31733
Patch284: 00284-add-PYTHONSHOWREFCOUNT-env-var.patch
# 00285 #
# Fix nondeterministic read in test_pty which fails randomly in koji.
# Fixed upstream: https://bugs.python.org/issue31158
Patch285: 00285-fix-non-deterministic-read-in-test_pty.patch
# (New patches go here ^^^)
#
# When adding new patches to "python2" and "python3" in Fedora, EL, etc.,
@ -1049,6 +1054,7 @@ mv Modules/cryptmodule.c Modules/_cryptmodule.c
%patch280 -p1
%patch283 -p1
%patch284 -p1
%patch285 -p1
%if 0%{?_module_build}
@ -1932,6 +1938,7 @@ rm -fr %{buildroot}
* Thu Nov 02 2017 Charalampos Stratakis <cstratak@redhat.com> - 2.7.14-2
- Add a new PYTHONSHOWREFCOUNT environment variable for printing the reference
count in debug builds.
- Fix nondeterministic read in test_pty.
* Mon Oct 09 2017 Iryna Shcherbina <ishcherb@redhat.com> - 2.7.14-1
- Update to version 2.7.14