|
3 | 3 | is_android, is_apple_mobile, is_emscripten, is_wasi, reap_children, verbose |
4 | 4 | ) |
5 | 5 | from test.support.import_helper import import_module |
6 | | -from test.support.os_helper import TESTFN, unlink |
7 | 6 |
|
8 | 7 | # Skip these tests if termios is not available |
9 | 8 | import_module('termios') |
@@ -297,26 +296,27 @@ def test_master_read(self): |
297 | 296 | self.assertEqual(data, b"") |
298 | 297 |
|
299 | 298 | def test_spawn_doesnt_hang(self): |
300 | | - self.addCleanup(unlink, TESTFN) |
301 | | - with open(TESTFN, 'wb') as f: |
302 | | - STDOUT_FILENO = 1 |
303 | | - dup_stdout = os.dup(STDOUT_FILENO) |
304 | | - os.dup2(f.fileno(), STDOUT_FILENO) |
305 | | - buf = b'' |
306 | | - def master_read(fd): |
307 | | - nonlocal buf |
308 | | - data = os.read(fd, 1024) |
309 | | - buf += data |
310 | | - return data |
| 299 | + # gh-140482: Do the test in a pty.fork() child to avoid messing |
| 300 | + # with the interactive test runner's terminal settings. |
| 301 | + pid, fd = pty.fork() |
| 302 | + if pid == pty.CHILD: |
| 303 | + pty.spawn([sys.executable, '-c', 'print("hi there")']) |
| 304 | + os._exit(0) |
| 305 | + |
| 306 | + try: |
| 307 | + buf = bytearray() |
311 | 308 | try: |
312 | | - pty.spawn([sys.executable, '-c', 'print("hi there")'], |
313 | | - master_read) |
314 | | - finally: |
315 | | - os.dup2(dup_stdout, STDOUT_FILENO) |
316 | | - os.close(dup_stdout) |
317 | | - self.assertEqual(buf, b'hi there\r\n') |
318 | | - with open(TESTFN, 'rb') as f: |
319 | | - self.assertEqual(f.read(), b'hi there\r\n') |
| 309 | + while (data := os.read(fd, 1024)) != b'': |
| 310 | + buf.extend(data) |
| 311 | + except OSError as e: |
| 312 | + if e.errno != errno.EIO: |
| 313 | + raise |
| 314 | + |
| 315 | + (pid, status) = os.waitpid(pid, 0) |
| 316 | + self.assertEqual(status, 0) |
| 317 | + self.assertEqual(bytes(buf), b"hi there\r\n") |
| 318 | + finally: |
| 319 | + os.close(fd) |
320 | 320 |
|
321 | 321 | class SmallPtyTests(unittest.TestCase): |
322 | 322 | """These tests don't spawn children or hang.""" |
|
0 commit comments