1 | n/a | import signal |
---|
2 | n/a | import sys |
---|
3 | n/a | import unittest |
---|
4 | n/a | import warnings |
---|
5 | n/a | from unittest import mock |
---|
6 | n/a | |
---|
7 | n/a | import asyncio |
---|
8 | n/a | from asyncio import base_subprocess |
---|
9 | n/a | from asyncio import subprocess |
---|
10 | n/a | from asyncio import test_utils |
---|
11 | n/a | try: |
---|
12 | n/a | from test import support |
---|
13 | n/a | except ImportError: |
---|
14 | n/a | from asyncio import test_support as support |
---|
15 | n/a | if sys.platform != 'win32': |
---|
16 | n/a | from asyncio import unix_events |
---|
17 | n/a | |
---|
18 | n/a | # Program blocking |
---|
19 | n/a | PROGRAM_BLOCKED = [sys.executable, '-c', 'import time; time.sleep(3600)'] |
---|
20 | n/a | |
---|
21 | n/a | # Program copying input to output |
---|
22 | n/a | PROGRAM_CAT = [ |
---|
23 | n/a | sys.executable, '-c', |
---|
24 | n/a | ';'.join(('import sys', |
---|
25 | n/a | 'data = sys.stdin.buffer.read()', |
---|
26 | n/a | 'sys.stdout.buffer.write(data)'))] |
---|
27 | n/a | |
---|
28 | n/a | class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport): |
---|
29 | n/a | def _start(self, *args, **kwargs): |
---|
30 | n/a | self._proc = mock.Mock() |
---|
31 | n/a | self._proc.stdin = None |
---|
32 | n/a | self._proc.stdout = None |
---|
33 | n/a | self._proc.stderr = None |
---|
34 | n/a | |
---|
35 | n/a | |
---|
36 | n/a | class SubprocessTransportTests(test_utils.TestCase): |
---|
37 | n/a | def setUp(self): |
---|
38 | n/a | super().setUp() |
---|
39 | n/a | self.loop = self.new_test_loop() |
---|
40 | n/a | self.set_event_loop(self.loop) |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | def create_transport(self, waiter=None): |
---|
44 | n/a | protocol = mock.Mock() |
---|
45 | n/a | protocol.connection_made._is_coroutine = False |
---|
46 | n/a | protocol.process_exited._is_coroutine = False |
---|
47 | n/a | transport = TestSubprocessTransport( |
---|
48 | n/a | self.loop, protocol, ['test'], False, |
---|
49 | n/a | None, None, None, 0, waiter=waiter) |
---|
50 | n/a | return (transport, protocol) |
---|
51 | n/a | |
---|
52 | n/a | def test_proc_exited(self): |
---|
53 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
54 | n/a | transport, protocol = self.create_transport(waiter) |
---|
55 | n/a | transport._process_exited(6) |
---|
56 | n/a | self.loop.run_until_complete(waiter) |
---|
57 | n/a | |
---|
58 | n/a | self.assertEqual(transport.get_returncode(), 6) |
---|
59 | n/a | |
---|
60 | n/a | self.assertTrue(protocol.connection_made.called) |
---|
61 | n/a | self.assertTrue(protocol.process_exited.called) |
---|
62 | n/a | self.assertTrue(protocol.connection_lost.called) |
---|
63 | n/a | self.assertEqual(protocol.connection_lost.call_args[0], (None,)) |
---|
64 | n/a | |
---|
65 | n/a | self.assertFalse(transport.is_closing()) |
---|
66 | n/a | self.assertIsNone(transport._loop) |
---|
67 | n/a | self.assertIsNone(transport._proc) |
---|
68 | n/a | self.assertIsNone(transport._protocol) |
---|
69 | n/a | |
---|
70 | n/a | # methods must raise ProcessLookupError if the process exited |
---|
71 | n/a | self.assertRaises(ProcessLookupError, |
---|
72 | n/a | transport.send_signal, signal.SIGTERM) |
---|
73 | n/a | self.assertRaises(ProcessLookupError, transport.terminate) |
---|
74 | n/a | self.assertRaises(ProcessLookupError, transport.kill) |
---|
75 | n/a | |
---|
76 | n/a | transport.close() |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | class SubprocessMixin: |
---|
80 | n/a | |
---|
81 | n/a | def test_stdin_stdout(self): |
---|
82 | n/a | args = PROGRAM_CAT |
---|
83 | n/a | |
---|
84 | n/a | @asyncio.coroutine |
---|
85 | n/a | def run(data): |
---|
86 | n/a | proc = yield from asyncio.create_subprocess_exec( |
---|
87 | n/a | *args, |
---|
88 | n/a | stdin=subprocess.PIPE, |
---|
89 | n/a | stdout=subprocess.PIPE, |
---|
90 | n/a | loop=self.loop) |
---|
91 | n/a | |
---|
92 | n/a | # feed data |
---|
93 | n/a | proc.stdin.write(data) |
---|
94 | n/a | yield from proc.stdin.drain() |
---|
95 | n/a | proc.stdin.close() |
---|
96 | n/a | |
---|
97 | n/a | # get output and exitcode |
---|
98 | n/a | data = yield from proc.stdout.read() |
---|
99 | n/a | exitcode = yield from proc.wait() |
---|
100 | n/a | return (exitcode, data) |
---|
101 | n/a | |
---|
102 | n/a | task = run(b'some data') |
---|
103 | n/a | task = asyncio.wait_for(task, 60.0, loop=self.loop) |
---|
104 | n/a | exitcode, stdout = self.loop.run_until_complete(task) |
---|
105 | n/a | self.assertEqual(exitcode, 0) |
---|
106 | n/a | self.assertEqual(stdout, b'some data') |
---|
107 | n/a | |
---|
108 | n/a | def test_communicate(self): |
---|
109 | n/a | args = PROGRAM_CAT |
---|
110 | n/a | |
---|
111 | n/a | @asyncio.coroutine |
---|
112 | n/a | def run(data): |
---|
113 | n/a | proc = yield from asyncio.create_subprocess_exec( |
---|
114 | n/a | *args, |
---|
115 | n/a | stdin=subprocess.PIPE, |
---|
116 | n/a | stdout=subprocess.PIPE, |
---|
117 | n/a | loop=self.loop) |
---|
118 | n/a | stdout, stderr = yield from proc.communicate(data) |
---|
119 | n/a | return proc.returncode, stdout |
---|
120 | n/a | |
---|
121 | n/a | task = run(b'some data') |
---|
122 | n/a | task = asyncio.wait_for(task, 60.0, loop=self.loop) |
---|
123 | n/a | exitcode, stdout = self.loop.run_until_complete(task) |
---|
124 | n/a | self.assertEqual(exitcode, 0) |
---|
125 | n/a | self.assertEqual(stdout, b'some data') |
---|
126 | n/a | |
---|
127 | n/a | def test_shell(self): |
---|
128 | n/a | create = asyncio.create_subprocess_shell('exit 7', |
---|
129 | n/a | loop=self.loop) |
---|
130 | n/a | proc = self.loop.run_until_complete(create) |
---|
131 | n/a | exitcode = self.loop.run_until_complete(proc.wait()) |
---|
132 | n/a | self.assertEqual(exitcode, 7) |
---|
133 | n/a | |
---|
134 | n/a | def test_start_new_session(self): |
---|
135 | n/a | # start the new process in a new session |
---|
136 | n/a | create = asyncio.create_subprocess_shell('exit 8', |
---|
137 | n/a | start_new_session=True, |
---|
138 | n/a | loop=self.loop) |
---|
139 | n/a | proc = self.loop.run_until_complete(create) |
---|
140 | n/a | exitcode = self.loop.run_until_complete(proc.wait()) |
---|
141 | n/a | self.assertEqual(exitcode, 8) |
---|
142 | n/a | |
---|
143 | n/a | def test_kill(self): |
---|
144 | n/a | args = PROGRAM_BLOCKED |
---|
145 | n/a | create = asyncio.create_subprocess_exec(*args, loop=self.loop) |
---|
146 | n/a | proc = self.loop.run_until_complete(create) |
---|
147 | n/a | proc.kill() |
---|
148 | n/a | returncode = self.loop.run_until_complete(proc.wait()) |
---|
149 | n/a | if sys.platform == 'win32': |
---|
150 | n/a | self.assertIsInstance(returncode, int) |
---|
151 | n/a | # expect 1 but sometimes get 0 |
---|
152 | n/a | else: |
---|
153 | n/a | self.assertEqual(-signal.SIGKILL, returncode) |
---|
154 | n/a | |
---|
155 | n/a | def test_terminate(self): |
---|
156 | n/a | args = PROGRAM_BLOCKED |
---|
157 | n/a | create = asyncio.create_subprocess_exec(*args, loop=self.loop) |
---|
158 | n/a | proc = self.loop.run_until_complete(create) |
---|
159 | n/a | proc.terminate() |
---|
160 | n/a | returncode = self.loop.run_until_complete(proc.wait()) |
---|
161 | n/a | if sys.platform == 'win32': |
---|
162 | n/a | self.assertIsInstance(returncode, int) |
---|
163 | n/a | # expect 1 but sometimes get 0 |
---|
164 | n/a | else: |
---|
165 | n/a | self.assertEqual(-signal.SIGTERM, returncode) |
---|
166 | n/a | |
---|
167 | n/a | @unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP") |
---|
168 | n/a | def test_send_signal(self): |
---|
169 | n/a | code = 'import time; print("sleeping", flush=True); time.sleep(3600)' |
---|
170 | n/a | args = [sys.executable, '-c', code] |
---|
171 | n/a | create = asyncio.create_subprocess_exec(*args, |
---|
172 | n/a | stdout=subprocess.PIPE, |
---|
173 | n/a | loop=self.loop) |
---|
174 | n/a | proc = self.loop.run_until_complete(create) |
---|
175 | n/a | |
---|
176 | n/a | @asyncio.coroutine |
---|
177 | n/a | def send_signal(proc): |
---|
178 | n/a | # basic synchronization to wait until the program is sleeping |
---|
179 | n/a | line = yield from proc.stdout.readline() |
---|
180 | n/a | self.assertEqual(line, b'sleeping\n') |
---|
181 | n/a | |
---|
182 | n/a | proc.send_signal(signal.SIGHUP) |
---|
183 | n/a | returncode = (yield from proc.wait()) |
---|
184 | n/a | return returncode |
---|
185 | n/a | |
---|
186 | n/a | returncode = self.loop.run_until_complete(send_signal(proc)) |
---|
187 | n/a | self.assertEqual(-signal.SIGHUP, returncode) |
---|
188 | n/a | |
---|
189 | n/a | def prepare_broken_pipe_test(self): |
---|
190 | n/a | # buffer large enough to feed the whole pipe buffer |
---|
191 | n/a | large_data = b'x' * support.PIPE_MAX_SIZE |
---|
192 | n/a | |
---|
193 | n/a | # the program ends before the stdin can be feeded |
---|
194 | n/a | create = asyncio.create_subprocess_exec( |
---|
195 | n/a | sys.executable, '-c', 'pass', |
---|
196 | n/a | stdin=subprocess.PIPE, |
---|
197 | n/a | loop=self.loop) |
---|
198 | n/a | proc = self.loop.run_until_complete(create) |
---|
199 | n/a | return (proc, large_data) |
---|
200 | n/a | |
---|
201 | n/a | def test_stdin_broken_pipe(self): |
---|
202 | n/a | proc, large_data = self.prepare_broken_pipe_test() |
---|
203 | n/a | |
---|
204 | n/a | @asyncio.coroutine |
---|
205 | n/a | def write_stdin(proc, data): |
---|
206 | n/a | proc.stdin.write(data) |
---|
207 | n/a | yield from proc.stdin.drain() |
---|
208 | n/a | |
---|
209 | n/a | coro = write_stdin(proc, large_data) |
---|
210 | n/a | # drain() must raise BrokenPipeError or ConnectionResetError |
---|
211 | n/a | with test_utils.disable_logger(): |
---|
212 | n/a | self.assertRaises((BrokenPipeError, ConnectionResetError), |
---|
213 | n/a | self.loop.run_until_complete, coro) |
---|
214 | n/a | self.loop.run_until_complete(proc.wait()) |
---|
215 | n/a | |
---|
216 | n/a | def test_communicate_ignore_broken_pipe(self): |
---|
217 | n/a | proc, large_data = self.prepare_broken_pipe_test() |
---|
218 | n/a | |
---|
219 | n/a | # communicate() must ignore BrokenPipeError when feeding stdin |
---|
220 | n/a | with test_utils.disable_logger(): |
---|
221 | n/a | self.loop.run_until_complete(proc.communicate(large_data)) |
---|
222 | n/a | self.loop.run_until_complete(proc.wait()) |
---|
223 | n/a | |
---|
224 | n/a | def test_pause_reading(self): |
---|
225 | n/a | limit = 10 |
---|
226 | n/a | size = (limit * 2 + 1) |
---|
227 | n/a | |
---|
228 | n/a | @asyncio.coroutine |
---|
229 | n/a | def test_pause_reading(): |
---|
230 | n/a | code = '\n'.join(( |
---|
231 | n/a | 'import sys', |
---|
232 | n/a | 'sys.stdout.write("x" * %s)' % size, |
---|
233 | n/a | 'sys.stdout.flush()', |
---|
234 | n/a | )) |
---|
235 | n/a | |
---|
236 | n/a | connect_read_pipe = self.loop.connect_read_pipe |
---|
237 | n/a | |
---|
238 | n/a | @asyncio.coroutine |
---|
239 | n/a | def connect_read_pipe_mock(*args, **kw): |
---|
240 | n/a | transport, protocol = yield from connect_read_pipe(*args, **kw) |
---|
241 | n/a | transport.pause_reading = mock.Mock() |
---|
242 | n/a | transport.resume_reading = mock.Mock() |
---|
243 | n/a | return (transport, protocol) |
---|
244 | n/a | |
---|
245 | n/a | self.loop.connect_read_pipe = connect_read_pipe_mock |
---|
246 | n/a | |
---|
247 | n/a | proc = yield from asyncio.create_subprocess_exec( |
---|
248 | n/a | sys.executable, '-c', code, |
---|
249 | n/a | stdin=asyncio.subprocess.PIPE, |
---|
250 | n/a | stdout=asyncio.subprocess.PIPE, |
---|
251 | n/a | limit=limit, |
---|
252 | n/a | loop=self.loop) |
---|
253 | n/a | stdout_transport = proc._transport.get_pipe_transport(1) |
---|
254 | n/a | |
---|
255 | n/a | stdout, stderr = yield from proc.communicate() |
---|
256 | n/a | |
---|
257 | n/a | # The child process produced more than limit bytes of output, |
---|
258 | n/a | # the stream reader transport should pause the protocol to not |
---|
259 | n/a | # allocate too much memory. |
---|
260 | n/a | return (stdout, stdout_transport) |
---|
261 | n/a | |
---|
262 | n/a | # Issue #22685: Ensure that the stream reader pauses the protocol |
---|
263 | n/a | # when the child process produces too much data |
---|
264 | n/a | stdout, transport = self.loop.run_until_complete(test_pause_reading()) |
---|
265 | n/a | |
---|
266 | n/a | self.assertEqual(stdout, b'x' * size) |
---|
267 | n/a | self.assertTrue(transport.pause_reading.called) |
---|
268 | n/a | self.assertTrue(transport.resume_reading.called) |
---|
269 | n/a | |
---|
270 | n/a | def test_stdin_not_inheritable(self): |
---|
271 | n/a | # asyncio issue #209: stdin must not be inheritable, otherwise |
---|
272 | n/a | # the Process.communicate() hangs |
---|
273 | n/a | @asyncio.coroutine |
---|
274 | n/a | def len_message(message): |
---|
275 | n/a | code = 'import sys; data = sys.stdin.read(); print(len(data))' |
---|
276 | n/a | proc = yield from asyncio.create_subprocess_exec( |
---|
277 | n/a | sys.executable, '-c', code, |
---|
278 | n/a | stdin=asyncio.subprocess.PIPE, |
---|
279 | n/a | stdout=asyncio.subprocess.PIPE, |
---|
280 | n/a | stderr=asyncio.subprocess.PIPE, |
---|
281 | n/a | close_fds=False, |
---|
282 | n/a | loop=self.loop) |
---|
283 | n/a | stdout, stderr = yield from proc.communicate(message) |
---|
284 | n/a | exitcode = yield from proc.wait() |
---|
285 | n/a | return (stdout, exitcode) |
---|
286 | n/a | |
---|
287 | n/a | output, exitcode = self.loop.run_until_complete(len_message(b'abc')) |
---|
288 | n/a | self.assertEqual(output.rstrip(), b'3') |
---|
289 | n/a | self.assertEqual(exitcode, 0) |
---|
290 | n/a | |
---|
291 | n/a | def test_empty_input(self): |
---|
292 | n/a | @asyncio.coroutine |
---|
293 | n/a | def empty_input(): |
---|
294 | n/a | code = 'import sys; data = sys.stdin.read(); print(len(data))' |
---|
295 | n/a | proc = yield from asyncio.create_subprocess_exec( |
---|
296 | n/a | sys.executable, '-c', code, |
---|
297 | n/a | stdin=asyncio.subprocess.PIPE, |
---|
298 | n/a | stdout=asyncio.subprocess.PIPE, |
---|
299 | n/a | stderr=asyncio.subprocess.PIPE, |
---|
300 | n/a | close_fds=False, |
---|
301 | n/a | loop=self.loop) |
---|
302 | n/a | stdout, stderr = yield from proc.communicate(b'') |
---|
303 | n/a | exitcode = yield from proc.wait() |
---|
304 | n/a | return (stdout, exitcode) |
---|
305 | n/a | |
---|
306 | n/a | output, exitcode = self.loop.run_until_complete(empty_input()) |
---|
307 | n/a | self.assertEqual(output.rstrip(), b'0') |
---|
308 | n/a | self.assertEqual(exitcode, 0) |
---|
309 | n/a | |
---|
310 | n/a | def test_cancel_process_wait(self): |
---|
311 | n/a | # Issue #23140: cancel Process.wait() |
---|
312 | n/a | |
---|
313 | n/a | @asyncio.coroutine |
---|
314 | n/a | def cancel_wait(): |
---|
315 | n/a | proc = yield from asyncio.create_subprocess_exec( |
---|
316 | n/a | *PROGRAM_BLOCKED, |
---|
317 | n/a | loop=self.loop) |
---|
318 | n/a | |
---|
319 | n/a | # Create an internal future waiting on the process exit |
---|
320 | n/a | task = self.loop.create_task(proc.wait()) |
---|
321 | n/a | self.loop.call_soon(task.cancel) |
---|
322 | n/a | try: |
---|
323 | n/a | yield from task |
---|
324 | n/a | except asyncio.CancelledError: |
---|
325 | n/a | pass |
---|
326 | n/a | |
---|
327 | n/a | # Cancel the future |
---|
328 | n/a | task.cancel() |
---|
329 | n/a | |
---|
330 | n/a | # Kill the process and wait until it is done |
---|
331 | n/a | proc.kill() |
---|
332 | n/a | yield from proc.wait() |
---|
333 | n/a | |
---|
334 | n/a | self.loop.run_until_complete(cancel_wait()) |
---|
335 | n/a | |
---|
336 | n/a | def test_cancel_make_subprocess_transport_exec(self): |
---|
337 | n/a | @asyncio.coroutine |
---|
338 | n/a | def cancel_make_transport(): |
---|
339 | n/a | coro = asyncio.create_subprocess_exec(*PROGRAM_BLOCKED, |
---|
340 | n/a | loop=self.loop) |
---|
341 | n/a | task = self.loop.create_task(coro) |
---|
342 | n/a | |
---|
343 | n/a | self.loop.call_soon(task.cancel) |
---|
344 | n/a | try: |
---|
345 | n/a | yield from task |
---|
346 | n/a | except asyncio.CancelledError: |
---|
347 | n/a | pass |
---|
348 | n/a | |
---|
349 | n/a | # ignore the log: |
---|
350 | n/a | # "Exception during subprocess creation, kill the subprocess" |
---|
351 | n/a | with test_utils.disable_logger(): |
---|
352 | n/a | self.loop.run_until_complete(cancel_make_transport()) |
---|
353 | n/a | |
---|
354 | n/a | def test_cancel_post_init(self): |
---|
355 | n/a | @asyncio.coroutine |
---|
356 | n/a | def cancel_make_transport(): |
---|
357 | n/a | coro = self.loop.subprocess_exec(asyncio.SubprocessProtocol, |
---|
358 | n/a | *PROGRAM_BLOCKED) |
---|
359 | n/a | task = self.loop.create_task(coro) |
---|
360 | n/a | |
---|
361 | n/a | self.loop.call_soon(task.cancel) |
---|
362 | n/a | try: |
---|
363 | n/a | yield from task |
---|
364 | n/a | except asyncio.CancelledError: |
---|
365 | n/a | pass |
---|
366 | n/a | |
---|
367 | n/a | # ignore the log: |
---|
368 | n/a | # "Exception during subprocess creation, kill the subprocess" |
---|
369 | n/a | with test_utils.disable_logger(): |
---|
370 | n/a | self.loop.run_until_complete(cancel_make_transport()) |
---|
371 | n/a | test_utils.run_briefly(self.loop) |
---|
372 | n/a | |
---|
373 | n/a | def test_close_kill_running(self): |
---|
374 | n/a | @asyncio.coroutine |
---|
375 | n/a | def kill_running(): |
---|
376 | n/a | create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, |
---|
377 | n/a | *PROGRAM_BLOCKED) |
---|
378 | n/a | transport, protocol = yield from create |
---|
379 | n/a | |
---|
380 | n/a | kill_called = False |
---|
381 | n/a | def kill(): |
---|
382 | n/a | nonlocal kill_called |
---|
383 | n/a | kill_called = True |
---|
384 | n/a | orig_kill() |
---|
385 | n/a | |
---|
386 | n/a | proc = transport.get_extra_info('subprocess') |
---|
387 | n/a | orig_kill = proc.kill |
---|
388 | n/a | proc.kill = kill |
---|
389 | n/a | returncode = transport.get_returncode() |
---|
390 | n/a | transport.close() |
---|
391 | n/a | yield from transport._wait() |
---|
392 | n/a | return (returncode, kill_called) |
---|
393 | n/a | |
---|
394 | n/a | # Ignore "Close running child process: kill ..." log |
---|
395 | n/a | with test_utils.disable_logger(): |
---|
396 | n/a | returncode, killed = self.loop.run_until_complete(kill_running()) |
---|
397 | n/a | self.assertIsNone(returncode) |
---|
398 | n/a | |
---|
399 | n/a | # transport.close() must kill the process if it is still running |
---|
400 | n/a | self.assertTrue(killed) |
---|
401 | n/a | test_utils.run_briefly(self.loop) |
---|
402 | n/a | |
---|
403 | n/a | def test_close_dont_kill_finished(self): |
---|
404 | n/a | @asyncio.coroutine |
---|
405 | n/a | def kill_running(): |
---|
406 | n/a | create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, |
---|
407 | n/a | *PROGRAM_BLOCKED) |
---|
408 | n/a | transport, protocol = yield from create |
---|
409 | n/a | proc = transport.get_extra_info('subprocess') |
---|
410 | n/a | |
---|
411 | n/a | # kill the process (but asyncio is not notified immediately) |
---|
412 | n/a | proc.kill() |
---|
413 | n/a | proc.wait() |
---|
414 | n/a | |
---|
415 | n/a | proc.kill = mock.Mock() |
---|
416 | n/a | proc_returncode = proc.poll() |
---|
417 | n/a | transport_returncode = transport.get_returncode() |
---|
418 | n/a | transport.close() |
---|
419 | n/a | return (proc_returncode, transport_returncode, proc.kill.called) |
---|
420 | n/a | |
---|
421 | n/a | # Ignore "Unknown child process pid ..." log of SafeChildWatcher, |
---|
422 | n/a | # emitted because the test already consumes the exit status: |
---|
423 | n/a | # proc.wait() |
---|
424 | n/a | with test_utils.disable_logger(): |
---|
425 | n/a | result = self.loop.run_until_complete(kill_running()) |
---|
426 | n/a | test_utils.run_briefly(self.loop) |
---|
427 | n/a | |
---|
428 | n/a | proc_returncode, transport_return_code, killed = result |
---|
429 | n/a | |
---|
430 | n/a | self.assertIsNotNone(proc_returncode) |
---|
431 | n/a | self.assertIsNone(transport_return_code) |
---|
432 | n/a | |
---|
433 | n/a | # transport.close() must not kill the process if it finished, even if |
---|
434 | n/a | # the transport was not notified yet |
---|
435 | n/a | self.assertFalse(killed) |
---|
436 | n/a | |
---|
437 | n/a | # Unlike SafeChildWatcher, FastChildWatcher does not pop the |
---|
438 | n/a | # callbacks if waitpid() is called elsewhere. Let's clear them |
---|
439 | n/a | # manually to avoid a warning when the watcher is detached. |
---|
440 | n/a | if sys.platform != 'win32' and \ |
---|
441 | n/a | isinstance(self, SubprocessFastWatcherTests): |
---|
442 | n/a | asyncio.get_child_watcher()._callbacks.clear() |
---|
443 | n/a | |
---|
444 | n/a | def test_popen_error(self): |
---|
445 | n/a | # Issue #24763: check that the subprocess transport is closed |
---|
446 | n/a | # when BaseSubprocessTransport fails |
---|
447 | n/a | if sys.platform == 'win32': |
---|
448 | n/a | target = 'asyncio.windows_utils.Popen' |
---|
449 | n/a | else: |
---|
450 | n/a | target = 'subprocess.Popen' |
---|
451 | n/a | with mock.patch(target) as popen: |
---|
452 | n/a | exc = ZeroDivisionError |
---|
453 | n/a | popen.side_effect = exc |
---|
454 | n/a | |
---|
455 | n/a | create = asyncio.create_subprocess_exec(sys.executable, '-c', |
---|
456 | n/a | 'pass', loop=self.loop) |
---|
457 | n/a | with warnings.catch_warnings(record=True) as warns: |
---|
458 | n/a | with self.assertRaises(exc): |
---|
459 | n/a | self.loop.run_until_complete(create) |
---|
460 | n/a | self.assertEqual(warns, []) |
---|
461 | n/a | |
---|
462 | n/a | |
---|
463 | n/a | if sys.platform != 'win32': |
---|
464 | n/a | # Unix |
---|
465 | n/a | class SubprocessWatcherMixin(SubprocessMixin): |
---|
466 | n/a | |
---|
467 | n/a | Watcher = None |
---|
468 | n/a | |
---|
469 | n/a | def setUp(self): |
---|
470 | n/a | super().setUp() |
---|
471 | n/a | policy = asyncio.get_event_loop_policy() |
---|
472 | n/a | self.loop = policy.new_event_loop() |
---|
473 | n/a | self.set_event_loop(self.loop) |
---|
474 | n/a | |
---|
475 | n/a | watcher = self.Watcher() |
---|
476 | n/a | watcher.attach_loop(self.loop) |
---|
477 | n/a | policy.set_child_watcher(watcher) |
---|
478 | n/a | self.addCleanup(policy.set_child_watcher, None) |
---|
479 | n/a | |
---|
480 | n/a | class SubprocessSafeWatcherTests(SubprocessWatcherMixin, |
---|
481 | n/a | test_utils.TestCase): |
---|
482 | n/a | |
---|
483 | n/a | Watcher = unix_events.SafeChildWatcher |
---|
484 | n/a | |
---|
485 | n/a | class SubprocessFastWatcherTests(SubprocessWatcherMixin, |
---|
486 | n/a | test_utils.TestCase): |
---|
487 | n/a | |
---|
488 | n/a | Watcher = unix_events.FastChildWatcher |
---|
489 | n/a | |
---|
490 | n/a | else: |
---|
491 | n/a | # Windows |
---|
492 | n/a | class SubprocessProactorTests(SubprocessMixin, test_utils.TestCase): |
---|
493 | n/a | |
---|
494 | n/a | def setUp(self): |
---|
495 | n/a | super().setUp() |
---|
496 | n/a | self.loop = asyncio.ProactorEventLoop() |
---|
497 | n/a | self.set_event_loop(self.loop) |
---|
498 | n/a | |
---|
499 | n/a | |
---|
500 | n/a | if __name__ == '__main__': |
---|
501 | n/a | unittest.main() |
---|