bpo-31158: Fix nondeterministic read in test_pty by diekmann · Pull Request #3808 · python/cpython (original) (raw)
Thanks @pitrou for the suggestion. The following also works on my machine:
diff --git a/Lib/test/test_pty.py b/Lib/test/test_pty.py index f283e19..ee9b589 100644 --- a/Lib/test/test_pty.py +++ b/Lib/test/test_pty.py @@ -10,6 +10,7 @@ import sys import select import signal import socket +import io import unittest TEST_STRING_1 = b"I wish to buy a fish license.\n" @@ -92,17 +93,18 @@ class PtyTest(unittest.TestCase): # Restore the original flags. os.set_blocking(master_fd, blocking) - debug("Writing to slave_fd") - os.write(slave_fd, TEST_STRING_1) - s1 = os.read(master_fd, 1024) - self.assertEqual(b'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)
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
with io.FileIO(master_fd, mode='rb', closefd=False) as master:
debug("Writing to slave_fd")
os.write(slave_fd, TEST_STRING_1)
s1 = master.readline()
self.assertEqual(b'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 = master.readline()
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2)) os.close(slave_fd) os.close(master_fd)
Which version is better?
Problem in version 1:
- Re-implementing
readline
Problem in version 2:
- Depends on the
io
module, while thepty
module usually only uses the rawos.read
andos.write
calls. A failure could make debugging harder (is it thepty
module or a change in theio
module?). - I don't like that we have both,
master
andmaster_fd
hanging around