Skip to content

Commit 205ac6e

Browse files
[3.12] gh-114100: Remove superfluous writing to fd 1 in test_pty (GH-114647) (GH-114655)
(cherry picked from commit 7a47054) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 01910d6 commit 205ac6e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Lib/test/test_pty.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from test.support import verbose, reap_children
2+
from test.support.os_helper import TESTFN, unlink
23
from test.support.import_helper import import_module
34

45
# Skip these tests if termios or fcntl are not available
@@ -292,7 +293,26 @@ def test_master_read(self):
292293
self.assertEqual(data, b"")
293294

294295
def test_spawn_doesnt_hang(self):
295-
pty.spawn([sys.executable, '-c', 'print("hi there")'])
296+
self.addCleanup(unlink, TESTFN)
297+
with open(TESTFN, 'wb') as f:
298+
STDOUT_FILENO = 1
299+
dup_stdout = os.dup(STDOUT_FILENO)
300+
os.dup2(f.fileno(), STDOUT_FILENO)
301+
buf = b''
302+
def master_read(fd):
303+
nonlocal buf
304+
data = os.read(fd, 1024)
305+
buf += data
306+
return data
307+
try:
308+
pty.spawn([sys.executable, '-c', 'print("hi there")'],
309+
master_read)
310+
finally:
311+
os.dup2(dup_stdout, STDOUT_FILENO)
312+
os.close(dup_stdout)
313+
self.assertEqual(buf, b'hi there\r\n')
314+
with open(TESTFN, 'rb') as f:
315+
self.assertEqual(f.read(), b'hi there\r\n')
296316

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

0 commit comments

Comments
 (0)