1915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinnerimport signal 2915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinnerimport sys 3915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinnerimport unittest 46fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinnerimport warnings 55ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinnerfrom unittest import mock 6dc7765d12c8b3008935659d70970ac3cd563e566Victor Stinner 7dc7765d12c8b3008935659d70970ac3cd563e566Victor Stinnerimport asyncio 847cd10d7a903773f574fc93220dbca850067fa0cVictor Stinnerfrom asyncio import base_subprocess 9dc7765d12c8b3008935659d70970ac3cd563e566Victor Stinnerfrom asyncio import subprocess 10dc7765d12c8b3008935659d70970ac3cd563e566Victor Stinnerfrom asyncio import test_utils 11dc7765d12c8b3008935659d70970ac3cd563e566Victor Stinnertry: 12d7ff5a5375cc23dff10f91696ac4895971c5850cVictor Stinner from test import support 13dc7765d12c8b3008935659d70970ac3cd563e566Victor Stinnerexcept ImportError: 14dc7765d12c8b3008935659d70970ac3cd563e566Victor Stinner from asyncio import test_support as support 15d7ff5a5375cc23dff10f91696ac4895971c5850cVictor Stinnerif sys.platform != 'win32': 16d7ff5a5375cc23dff10f91696ac4895971c5850cVictor Stinner from asyncio import unix_events 17915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 18915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner# Program blocking 19915bcb01110c7db65f8be9139bf887c749fbde75Victor StinnerPROGRAM_BLOCKED = [sys.executable, '-c', 'import time; time.sleep(3600)'] 20915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 21915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner# Program copying input to output 22915bcb01110c7db65f8be9139bf887c749fbde75Victor StinnerPROGRAM_CAT = [ 23915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner sys.executable, '-c', 24915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner ';'.join(('import sys', 25915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 'data = sys.stdin.buffer.read()', 26915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 'sys.stdout.buffer.write(data)'))] 27915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 2847cd10d7a903773f574fc93220dbca850067fa0cVictor Stinnerclass TestSubprocessTransport(base_subprocess.BaseSubprocessTransport): 2947cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner def _start(self, *args, **kwargs): 3047cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self._proc = mock.Mock() 3147cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self._proc.stdin = None 3247cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self._proc.stdout = None 3347cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self._proc.stderr = None 3447cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 3547cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 3647cd10d7a903773f574fc93220dbca850067fa0cVictor Stinnerclass SubprocessTransportTests(test_utils.TestCase): 3747cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner def setUp(self): 38600a349781bfa0a8239e1cb95fac29c7c4a3302eYury Selivanov super().setUp() 3947cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.loop = self.new_test_loop() 4047cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.set_event_loop(self.loop) 4147cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 4247cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 4347cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner def create_transport(self, waiter=None): 4447cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner protocol = mock.Mock() 4547cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner protocol.connection_made._is_coroutine = False 4647cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner protocol.process_exited._is_coroutine = False 4747cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner transport = TestSubprocessTransport( 4847cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.loop, protocol, ['test'], False, 4947cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner None, None, None, 0, waiter=waiter) 5047cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner return (transport, protocol) 5147cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 5247cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner def test_proc_exited(self): 5347cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner waiter = asyncio.Future(loop=self.loop) 5447cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner transport, protocol = self.create_transport(waiter) 5547cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner transport._process_exited(6) 5647cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.loop.run_until_complete(waiter) 5747cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 5847cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertEqual(transport.get_returncode(), 6) 5947cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 6047cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertTrue(protocol.connection_made.called) 6147cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertTrue(protocol.process_exited.called) 6247cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertTrue(protocol.connection_lost.called) 6347cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertEqual(protocol.connection_lost.call_args[0], (None,)) 6447cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 655bb1afb3322ecf370cf328d40fb95eb0a3ddab7cYury Selivanov self.assertFalse(transport.is_closing()) 6647cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertIsNone(transport._loop) 6747cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertIsNone(transport._proc) 6847cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertIsNone(transport._protocol) 6947cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 7047cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner # methods must raise ProcessLookupError if the process exited 7147cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertRaises(ProcessLookupError, 7247cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner transport.send_signal, signal.SIGTERM) 7347cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertRaises(ProcessLookupError, transport.terminate) 7447cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner self.assertRaises(ProcessLookupError, transport.kill) 7547cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 760698638d797ca864f069d828dd2ea6d55b87a04eVictor Stinner transport.close() 770698638d797ca864f069d828dd2ea6d55b87a04eVictor Stinner 7847cd10d7a903773f574fc93220dbca850067fa0cVictor Stinner 79915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinnerclass SubprocessMixin: 8057797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov 81915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def test_stdin_stdout(self): 82915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner args = PROGRAM_CAT 83915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 84915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner @asyncio.coroutine 85915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def run(data): 86915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = yield from asyncio.create_subprocess_exec( 87915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner *args, 88915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner stdin=subprocess.PIPE, 89915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner stdout=subprocess.PIPE, 90915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner loop=self.loop) 91915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 92915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner # feed data 93915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc.stdin.write(data) 94915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner yield from proc.stdin.drain() 95915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc.stdin.close() 96915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 97915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner # get output and exitcode 98915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner data = yield from proc.stdout.read() 99915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner exitcode = yield from proc.wait() 100915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner return (exitcode, data) 101915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 102915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner task = run(b'some data') 1032955a0bf06807d4e00ad053a7e2acb516939859eVictor Stinner task = asyncio.wait_for(task, 60.0, loop=self.loop) 104915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner exitcode, stdout = self.loop.run_until_complete(task) 105915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(exitcode, 0) 106915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(stdout, b'some data') 107915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 108915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def test_communicate(self): 109915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner args = PROGRAM_CAT 110915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 111915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner @asyncio.coroutine 112915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def run(data): 113915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = yield from asyncio.create_subprocess_exec( 114915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner *args, 115915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner stdin=subprocess.PIPE, 116915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner stdout=subprocess.PIPE, 117915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner loop=self.loop) 118915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner stdout, stderr = yield from proc.communicate(data) 119915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner return proc.returncode, stdout 120915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 121915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner task = run(b'some data') 1222955a0bf06807d4e00ad053a7e2acb516939859eVictor Stinner task = asyncio.wait_for(task, 60.0, loop=self.loop) 123915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner exitcode, stdout = self.loop.run_until_complete(task) 124915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(exitcode, 0) 125915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(stdout, b'some data') 126915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 127915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def test_shell(self): 128915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner create = asyncio.create_subprocess_shell('exit 7', 129915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner loop=self.loop) 130915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = self.loop.run_until_complete(create) 131915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner exitcode = self.loop.run_until_complete(proc.wait()) 132915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(exitcode, 7) 133915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 134915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def test_start_new_session(self): 135915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner # start the new process in a new session 136915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner create = asyncio.create_subprocess_shell('exit 8', 137915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner start_new_session=True, 138915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner loop=self.loop) 139915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = self.loop.run_until_complete(create) 140915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner exitcode = self.loop.run_until_complete(proc.wait()) 141915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(exitcode, 8) 142915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 143915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def test_kill(self): 144915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner args = PROGRAM_BLOCKED 145915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner create = asyncio.create_subprocess_exec(*args, loop=self.loop) 146915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = self.loop.run_until_complete(create) 147915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc.kill() 148915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner returncode = self.loop.run_until_complete(proc.wait()) 149915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner if sys.platform == 'win32': 150915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertIsInstance(returncode, int) 151915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner # expect 1 but sometimes get 0 152915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner else: 153915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(-signal.SIGKILL, returncode) 154915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 155915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def test_terminate(self): 156915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner args = PROGRAM_BLOCKED 157915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner create = asyncio.create_subprocess_exec(*args, loop=self.loop) 158915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = self.loop.run_until_complete(create) 159915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc.terminate() 160915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner returncode = self.loop.run_until_complete(proc.wait()) 161915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner if sys.platform == 'win32': 162915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertIsInstance(returncode, int) 163915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner # expect 1 but sometimes get 0 164915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner else: 165915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(-signal.SIGTERM, returncode) 166915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 167915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner @unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP") 168915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def test_send_signal(self): 16998fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner code = 'import time; print("sleeping", flush=True); time.sleep(3600)' 17098fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner args = [sys.executable, '-c', code] 17115cc678d8971f5ee1a7ea5e88bcd22413601f033Victor Stinner create = asyncio.create_subprocess_exec(*args, 17215cc678d8971f5ee1a7ea5e88bcd22413601f033Victor Stinner stdout=subprocess.PIPE, 17315cc678d8971f5ee1a7ea5e88bcd22413601f033Victor Stinner loop=self.loop) 174915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = self.loop.run_until_complete(create) 17598fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner 17698fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner @asyncio.coroutine 17798fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner def send_signal(proc): 17898fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner # basic synchronization to wait until the program is sleeping 17998fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner line = yield from proc.stdout.readline() 18098fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner self.assertEqual(line, b'sleeping\n') 18198fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner 18298fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner proc.send_signal(signal.SIGHUP) 18398fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner returncode = (yield from proc.wait()) 18498fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner return returncode 18598fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner 18698fa332e334633d5559ff3963df0ab5c6108d8e1Victor Stinner returncode = self.loop.run_until_complete(send_signal(proc)) 187915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.assertEqual(-signal.SIGHUP, returncode) 188915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 189cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner def prepare_broken_pipe_test(self): 190cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner # buffer large enough to feed the whole pipe buffer 191915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner large_data = b'x' * support.PIPE_MAX_SIZE 192915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 193cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner # the program ends before the stdin can be feeded 194915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner create = asyncio.create_subprocess_exec( 195cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner sys.executable, '-c', 'pass', 196915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner stdin=subprocess.PIPE, 197915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner loop=self.loop) 198915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner proc = self.loop.run_until_complete(create) 199cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner return (proc, large_data) 200cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner 201cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner def test_stdin_broken_pipe(self): 202cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner proc, large_data = self.prepare_broken_pipe_test() 203cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner 2040dee8ad579c57ee533251004f66f1f4c6fee1691Victor Stinner @asyncio.coroutine 2050dee8ad579c57ee533251004f66f1f4c6fee1691Victor Stinner def write_stdin(proc, data): 2060dee8ad579c57ee533251004f66f1f4c6fee1691Victor Stinner proc.stdin.write(data) 2070dee8ad579c57ee533251004f66f1f4c6fee1691Victor Stinner yield from proc.stdin.drain() 2080dee8ad579c57ee533251004f66f1f4c6fee1691Victor Stinner 2090dee8ad579c57ee533251004f66f1f4c6fee1691Victor Stinner coro = write_stdin(proc, large_data) 210ddc8c8db1cd50d3b86cffbb1070ab32e3a14bbaeVictor Stinner # drain() must raise BrokenPipeError or ConnectionResetError 211b261475a48d905f160bc1f499e90b995b0d0b6c0Victor Stinner with test_utils.disable_logger(): 212b261475a48d905f160bc1f499e90b995b0d0b6c0Victor Stinner self.assertRaises((BrokenPipeError, ConnectionResetError), 213b261475a48d905f160bc1f499e90b995b0d0b6c0Victor Stinner self.loop.run_until_complete, coro) 214cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner self.loop.run_until_complete(proc.wait()) 215cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner 216cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner def test_communicate_ignore_broken_pipe(self): 217cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner proc, large_data = self.prepare_broken_pipe_test() 218cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner 219cc996b57890a251cef83101d5cfcbc58179cba5fVictor Stinner # communicate() must ignore BrokenPipeError when feeding stdin 220b261475a48d905f160bc1f499e90b995b0d0b6c0Victor Stinner with test_utils.disable_logger(): 221b261475a48d905f160bc1f499e90b995b0d0b6c0Victor Stinner self.loop.run_until_complete(proc.communicate(large_data)) 222915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.loop.run_until_complete(proc.wait()) 223915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 2245ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner def test_pause_reading(self): 225cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner limit = 10 226cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner size = (limit * 2 + 1) 227cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner 2285ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner @asyncio.coroutine 2295ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner def test_pause_reading(): 2305ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner code = '\n'.join(( 2315ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner 'import sys', 232cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner 'sys.stdout.write("x" * %s)' % size, 2335ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner 'sys.stdout.flush()', 2345ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner )) 235f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner 236f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner connect_read_pipe = self.loop.connect_read_pipe 237f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner 238f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner @asyncio.coroutine 239f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner def connect_read_pipe_mock(*args, **kw): 240f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner transport, protocol = yield from connect_read_pipe(*args, **kw) 241f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner transport.pause_reading = mock.Mock() 242f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner transport.resume_reading = mock.Mock() 243f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner return (transport, protocol) 244f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner 245f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner self.loop.connect_read_pipe = connect_read_pipe_mock 246f716d8b7a53ee2ed3b6dc42f7f6b00ac211209d8Victor Stinner 2475ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner proc = yield from asyncio.create_subprocess_exec( 2485ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner sys.executable, '-c', code, 2495ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner stdin=asyncio.subprocess.PIPE, 2505ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner stdout=asyncio.subprocess.PIPE, 2515ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner limit=limit, 2525ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner loop=self.loop) 2535ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner stdout_transport = proc._transport.get_pipe_transport(1) 2545ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner 255cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner stdout, stderr = yield from proc.communicate() 2565ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner 2575ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner # The child process produced more than limit bytes of output, 2585ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner # the stream reader transport should pause the protocol to not 2595ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner # allocate too much memory. 260cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner return (stdout, stdout_transport) 2615ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner 2625ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner # Issue #22685: Ensure that the stream reader pauses the protocol 2635ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner # when the child process produces too much data 264cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner stdout, transport = self.loop.run_until_complete(test_pause_reading()) 265cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner 266cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner self.assertEqual(stdout, b'x' * size) 267cbbd04d1769735f6dbb5fd7b204e5ea29f748375Victor Stinner self.assertTrue(transport.pause_reading.called) 268dd8224e6a483a45d7cf2e7be0fa7d818a3f04c80Victor Stinner self.assertTrue(transport.resume_reading.called) 2695ef586f25a6d5128a15341e849d7dca4fe882d22Victor Stinner 2701e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner def test_stdin_not_inheritable(self): 271e6ecea53c8394b5101bb3ddab3216a948065ef97Victor Stinner # asyncio issue #209: stdin must not be inheritable, otherwise 2721e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner # the Process.communicate() hangs 2731e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner @asyncio.coroutine 2741e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner def len_message(message): 2751e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner code = 'import sys; data = sys.stdin.read(); print(len(data))' 2761e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner proc = yield from asyncio.create_subprocess_exec( 2771e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner sys.executable, '-c', code, 2781e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner stdin=asyncio.subprocess.PIPE, 2791e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner stdout=asyncio.subprocess.PIPE, 2801e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner stderr=asyncio.subprocess.PIPE, 2811e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner close_fds=False, 2821e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner loop=self.loop) 2831e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner stdout, stderr = yield from proc.communicate(message) 2841e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner exitcode = yield from proc.wait() 2851e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner return (stdout, exitcode) 2861e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner 2871e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner output, exitcode = self.loop.run_until_complete(len_message(b'abc')) 2881e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner self.assertEqual(output.rstrip(), b'3') 2891e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner self.assertEqual(exitcode, 0) 2901e40f10886f1c83e47f69ab229b27ab2eceff939Victor Stinner 2917657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov def test_empty_input(self): 2927657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov @asyncio.coroutine 2937657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov def empty_input(): 2947657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov code = 'import sys; data = sys.stdin.read(); print(len(data))' 2957657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov proc = yield from asyncio.create_subprocess_exec( 2967657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov sys.executable, '-c', code, 2977657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov stdin=asyncio.subprocess.PIPE, 2987657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov stdout=asyncio.subprocess.PIPE, 2997657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov stderr=asyncio.subprocess.PIPE, 3007657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov close_fds=False, 3017657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov loop=self.loop) 3027657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov stdout, stderr = yield from proc.communicate(b'') 3037657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov exitcode = yield from proc.wait() 3047657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov return (stdout, exitcode) 3057657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov 3067657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov output, exitcode = self.loop.run_until_complete(empty_input()) 3077657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov self.assertEqual(output.rstrip(), b'0') 3087657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov self.assertEqual(exitcode, 0) 3097657f6ba218aa59ee085f2001dc44247f2fd0d4cYury Selivanov 310c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner def test_cancel_process_wait(self): 311c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner # Issue #23140: cancel Process.wait() 312c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner 313c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner @asyncio.coroutine 314c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner def cancel_wait(): 315c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner proc = yield from asyncio.create_subprocess_exec( 316c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner *PROGRAM_BLOCKED, 317c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner loop=self.loop) 318c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner 319c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner # Create an internal future waiting on the process exit 320212994e4e2ed023267ebfceeae541c8aa4257806Victor Stinner task = self.loop.create_task(proc.wait()) 321212994e4e2ed023267ebfceeae541c8aa4257806Victor Stinner self.loop.call_soon(task.cancel) 322212994e4e2ed023267ebfceeae541c8aa4257806Victor Stinner try: 323212994e4e2ed023267ebfceeae541c8aa4257806Victor Stinner yield from task 324212994e4e2ed023267ebfceeae541c8aa4257806Victor Stinner except asyncio.CancelledError: 325212994e4e2ed023267ebfceeae541c8aa4257806Victor Stinner pass 326c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner 327c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner # Cancel the future 328c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner task.cancel() 329c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner 330c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner # Kill the process and wait until it is done 331c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner proc.kill() 332c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner yield from proc.wait() 333c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner 334c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner self.loop.run_until_complete(cancel_wait()) 335c447ba04e78a91c1febe7744b9e6cbcdd3e23360Victor Stinner 336f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner def test_cancel_make_subprocess_transport_exec(self): 337f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner @asyncio.coroutine 338f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner def cancel_make_transport(): 339f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner coro = asyncio.create_subprocess_exec(*PROGRAM_BLOCKED, 340f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner loop=self.loop) 341f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner task = self.loop.create_task(coro) 342f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner 343f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner self.loop.call_soon(task.cancel) 344f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner try: 345f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner yield from task 346f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner except asyncio.CancelledError: 347f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner pass 348f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner 349f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner # ignore the log: 350f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner # "Exception during subprocess creation, kill the subprocess" 351f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner with test_utils.disable_logger(): 352f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner self.loop.run_until_complete(cancel_make_transport()) 353f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner 354f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner def test_cancel_post_init(self): 355f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner @asyncio.coroutine 356f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner def cancel_make_transport(): 357f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner coro = self.loop.subprocess_exec(asyncio.SubprocessProtocol, 358f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner *PROGRAM_BLOCKED) 359f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner task = self.loop.create_task(coro) 360f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner 361f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner self.loop.call_soon(task.cancel) 362f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner try: 363f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner yield from task 364f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner except asyncio.CancelledError: 365f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner pass 366f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner 367f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner # ignore the log: 368f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner # "Exception during subprocess creation, kill the subprocess" 369f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner with test_utils.disable_logger(): 370f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner self.loop.run_until_complete(cancel_make_transport()) 371ab8848bc2a64930e0e9a2e56592bb692fb31d9e9Victor Stinner test_utils.run_briefly(self.loop) 372f651a604075c2dc9a2d7f3d3bd74da374ff8a696Victor Stinner 3738e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner def test_close_kill_running(self): 3748e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner @asyncio.coroutine 3758e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner def kill_running(): 3768e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, 3778e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner *PROGRAM_BLOCKED) 3788e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner transport, protocol = yield from create 3794088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner 3804088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner kill_called = False 3814088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner def kill(): 3824088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner nonlocal kill_called 3834088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner kill_called = True 3844088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner orig_kill() 3854088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner 3868e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner proc = transport.get_extra_info('subprocess') 3874088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner orig_kill = proc.kill 3884088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner proc.kill = kill 3898e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner returncode = transport.get_returncode() 3908e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner transport.close() 391e7a2f64435d795712a766a088541b43dc7cee5b7Victor Stinner yield from transport._wait() 3924088ad9dcef0d7bbe26dc4a2527d4220ac558f53Victor Stinner return (returncode, kill_called) 3938e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 3948e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner # Ignore "Close running child process: kill ..." log 3958e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner with test_utils.disable_logger(): 3968e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner returncode, killed = self.loop.run_until_complete(kill_running()) 3978e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner self.assertIsNone(returncode) 3988e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 3998e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner # transport.close() must kill the process if it is still running 4008e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner self.assertTrue(killed) 4018e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner test_utils.run_briefly(self.loop) 4028e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 4038e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner def test_close_dont_kill_finished(self): 4048e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner @asyncio.coroutine 4058e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner def kill_running(): 4068e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, 4078e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner *PROGRAM_BLOCKED) 4088e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner transport, protocol = yield from create 4098e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner proc = transport.get_extra_info('subprocess') 4108e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 41146f50726a0047ae81d478c3a206f587b8f35ed08Martin Panter # kill the process (but asyncio is not notified immediately) 4128e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner proc.kill() 4138e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner proc.wait() 4148e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 4158e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner proc.kill = mock.Mock() 4168e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner proc_returncode = proc.poll() 4178e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner transport_returncode = transport.get_returncode() 4188e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner transport.close() 4198e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner return (proc_returncode, transport_returncode, proc.kill.called) 4208e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 4218e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner # Ignore "Unknown child process pid ..." log of SafeChildWatcher, 4228e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner # emitted because the test already consumes the exit status: 4238e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner # proc.wait() 4248e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner with test_utils.disable_logger(): 4258e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner result = self.loop.run_until_complete(kill_running()) 4268e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner test_utils.run_briefly(self.loop) 4278e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 4288e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner proc_returncode, transport_return_code, killed = result 4298e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 4308e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner self.assertIsNotNone(proc_returncode) 4318e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner self.assertIsNone(transport_return_code) 4328e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 4338e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner # transport.close() must not kill the process if it finished, even if 4348e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner # the transport was not notified yet 4358e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner self.assertFalse(killed) 4368e36812e27f70bd6e4b3b85c9e9e858b0ac0df5eVictor Stinner 4379eb6c677763c1eaf0646170ccff110c89828a06eYury Selivanov # Unlike SafeChildWatcher, FastChildWatcher does not pop the 4389eb6c677763c1eaf0646170ccff110c89828a06eYury Selivanov # callbacks if waitpid() is called elsewhere. Let's clear them 4399eb6c677763c1eaf0646170ccff110c89828a06eYury Selivanov # manually to avoid a warning when the watcher is detached. 4409eb6c677763c1eaf0646170ccff110c89828a06eYury Selivanov if sys.platform != 'win32' and \ 4419eb6c677763c1eaf0646170ccff110c89828a06eYury Selivanov isinstance(self, SubprocessFastWatcherTests): 4429eb6c677763c1eaf0646170ccff110c89828a06eYury Selivanov asyncio.get_child_watcher()._callbacks.clear() 4439eb6c677763c1eaf0646170ccff110c89828a06eYury Selivanov 4446fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner def test_popen_error(self): 4456fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner # Issue #24763: check that the subprocess transport is closed 4466fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner # when BaseSubprocessTransport fails 4479632ea2f26831ae3bbf41ca570d6e138c2f13656Yury Selivanov if sys.platform == 'win32': 4489632ea2f26831ae3bbf41ca570d6e138c2f13656Yury Selivanov target = 'asyncio.windows_utils.Popen' 4499632ea2f26831ae3bbf41ca570d6e138c2f13656Yury Selivanov else: 4509632ea2f26831ae3bbf41ca570d6e138c2f13656Yury Selivanov target = 'subprocess.Popen' 4519632ea2f26831ae3bbf41ca570d6e138c2f13656Yury Selivanov with mock.patch(target) as popen: 4526fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner exc = ZeroDivisionError 4536fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner popen.side_effect = exc 4546fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner 4556fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner create = asyncio.create_subprocess_exec(sys.executable, '-c', 4566fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner 'pass', loop=self.loop) 4570c6a34409ea9cf9a82171da09801f086d472aa89Yury Selivanov with warnings.catch_warnings(record=True) as warns: 4586fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner with self.assertRaises(exc): 4596fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner self.loop.run_until_complete(create) 4600c6a34409ea9cf9a82171da09801f086d472aa89Yury Selivanov self.assertEqual(warns, []) 4616fb1e740c67d8940fe27883d77f0dc46431aa9b4Victor Stinner 462604faba1db724951ee440337099d111e3ecade49Seth M. Larson def test_read_stdout_after_process_exit(self): 463604faba1db724951ee440337099d111e3ecade49Seth M. Larson @asyncio.coroutine 464604faba1db724951ee440337099d111e3ecade49Seth M. Larson def execute(): 465604faba1db724951ee440337099d111e3ecade49Seth M. Larson code = '\n'.join(['import sys', 466604faba1db724951ee440337099d111e3ecade49Seth M. Larson 'for _ in range(64):', 467604faba1db724951ee440337099d111e3ecade49Seth M. Larson ' sys.stdout.write("x" * 4096)', 468604faba1db724951ee440337099d111e3ecade49Seth M. Larson 'sys.stdout.flush()', 469604faba1db724951ee440337099d111e3ecade49Seth M. Larson 'sys.exit(1)']) 470604faba1db724951ee440337099d111e3ecade49Seth M. Larson 471604faba1db724951ee440337099d111e3ecade49Seth M. Larson fut = asyncio.create_subprocess_exec( 472604faba1db724951ee440337099d111e3ecade49Seth M. Larson sys.executable, '-c', code, 473604faba1db724951ee440337099d111e3ecade49Seth M. Larson stdout=asyncio.subprocess.PIPE, 474604faba1db724951ee440337099d111e3ecade49Seth M. Larson loop=self.loop) 475604faba1db724951ee440337099d111e3ecade49Seth M. Larson 476604faba1db724951ee440337099d111e3ecade49Seth M. Larson process = yield from fut 477604faba1db724951ee440337099d111e3ecade49Seth M. Larson while True: 478604faba1db724951ee440337099d111e3ecade49Seth M. Larson data = yield from process.stdout.read(65536) 479604faba1db724951ee440337099d111e3ecade49Seth M. Larson if data: 480604faba1db724951ee440337099d111e3ecade49Seth M. Larson yield from asyncio.sleep(0.3, loop=self.loop) 481604faba1db724951ee440337099d111e3ecade49Seth M. Larson else: 482604faba1db724951ee440337099d111e3ecade49Seth M. Larson break 483604faba1db724951ee440337099d111e3ecade49Seth M. Larson 484604faba1db724951ee440337099d111e3ecade49Seth M. Larson self.loop.run_until_complete(execute()) 485604faba1db724951ee440337099d111e3ecade49Seth M. Larson 486915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 487915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinnerif sys.platform != 'win32': 488915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner # Unix 489915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner class SubprocessWatcherMixin(SubprocessMixin): 49057797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov 491915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner Watcher = None 492915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 493915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def setUp(self): 494600a349781bfa0a8239e1cb95fac29c7c4a3302eYury Selivanov super().setUp() 495915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner policy = asyncio.get_event_loop_policy() 496915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.loop = policy.new_event_loop() 497956de691f8bfc379a1f1453e9a53661c92afa15eVictor Stinner self.set_event_loop(self.loop) 498915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 499915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner watcher = self.Watcher() 500915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner watcher.attach_loop(self.loop) 501915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner policy.set_child_watcher(watcher) 502956de691f8bfc379a1f1453e9a53661c92afa15eVictor Stinner self.addCleanup(policy.set_child_watcher, None) 503915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 50457797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov class SubprocessSafeWatcherTests(SubprocessWatcherMixin, 505c73701de7292b7de0fee5b7f82a610d7515c18a4Victor Stinner test_utils.TestCase): 50657797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov 507915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner Watcher = unix_events.SafeChildWatcher 508915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 50957797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov class SubprocessFastWatcherTests(SubprocessWatcherMixin, 510c73701de7292b7de0fee5b7f82a610d7515c18a4Victor Stinner test_utils.TestCase): 51157797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov 512915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner Watcher = unix_events.FastChildWatcher 51357797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov 514915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinnerelse: 515915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner # Windows 516c73701de7292b7de0fee5b7f82a610d7515c18a4Victor Stinner class SubprocessProactorTests(SubprocessMixin, test_utils.TestCase): 51757797521bdc5d64eb2440a5818a20c2dbc6358cbYury Selivanov 518915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner def setUp(self): 519600a349781bfa0a8239e1cb95fac29c7c4a3302eYury Selivanov super().setUp() 520915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner self.loop = asyncio.ProactorEventLoop() 521956de691f8bfc379a1f1453e9a53661c92afa15eVictor Stinner self.set_event_loop(self.loop) 522915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 523915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner 524915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinnerif __name__ == '__main__': 525915bcb01110c7db65f8be9139bf887c749fbde75Victor Stinner unittest.main() 526