Skip to content

Commit b30dc28

Browse files
committed
gh-140482: Avoid changing terminal settings in test_pty
The previous test_spawn_doesnt_hang test had a few problems: * It would cause ENV CHANGED failures if other tests were running concurrently due to stty changes * Typing while the test was running could cause it to fail
1 parent d3c888b commit b30dc28

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

Lib/test/test_pty.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -299,26 +299,20 @@ def test_master_read(self):
299299

300300
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
301301
def test_spawn_doesnt_hang(self):
302-
self.addCleanup(unlink, TESTFN)
303-
with open(TESTFN, 'wb') as f:
304-
STDOUT_FILENO = 1
305-
dup_stdout = os.dup(STDOUT_FILENO)
306-
os.dup2(f.fileno(), STDOUT_FILENO)
307-
buf = b''
308-
def master_read(fd):
309-
nonlocal buf
310-
data = os.read(fd, 1024)
311-
buf += data
312-
return data
313-
try:
314-
pty.spawn([sys.executable, '-c', 'print("hi there")'],
315-
master_read)
316-
finally:
317-
os.dup2(dup_stdout, STDOUT_FILENO)
318-
os.close(dup_stdout)
319-
self.assertEqual(buf, b'hi there\r\n')
320-
with open(TESTFN, 'rb') as f:
321-
self.assertEqual(f.read(), b'hi there\r\n')
302+
# gh-140482: Do the test in a pty.fork() child to avoid messing
303+
# with the interactive test runner's terminal settings.
304+
pid, fd = pty.fork()
305+
if pid == pty.CHILD:
306+
pty.spawn([sys.executable, '-c', 'print("hi there")'])
307+
os._exit(0)
308+
309+
try:
310+
(pid, status) = os.waitpid(pid, 0)
311+
self.assertEqual(status, 0)
312+
data = os.read(fd, 1024)
313+
self.assertEqual(data, b"hi there\r\n")
314+
finally:
315+
os.close(fd)
322316

323317
class SmallPtyTests(unittest.TestCase):
324318
"""These tests don't spawn children or hang."""

0 commit comments

Comments
 (0)