Coverage for Lib/asyncio/base_subprocess.py: 82%
211 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-23 01:21 +0000
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-23 01:21 +0000
1import collections
2import subprocess
3import warnings
4import os
5import signal
6import sys
8from . import protocols
9from . import transports
10from .log import logger
13class BaseSubprocessTransport(transports.SubprocessTransport):
15 def __init__(self, loop, protocol, args, shell,
16 stdin, stdout, stderr, bufsize,
17 waiter=None, extra=None, **kwargs):
18 super().__init__(extra)
19 self._closed = False
20 self._protocol = protocol
21 self._loop = loop
22 self._proc = None
23 self._pid = None
24 self._returncode = None
25 self._exit_waiters = []
26 self._pending_calls = collections.deque()
27 self._pipes = {}
28 self._finished = False
30 if stdin == subprocess.PIPE:
31 self._pipes[0] = None
32 if stdout == subprocess.PIPE:
33 self._pipes[1] = None
34 if stderr == subprocess.PIPE:
35 self._pipes[2] = None
37 # Create the child process: set the _proc attribute
38 try:
39 self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
40 stderr=stderr, bufsize=bufsize, **kwargs)
41 except:
42 self.close()
43 raise
45 self._pid = self._proc.pid
46 self._extra['subprocess'] = self._proc
48 if self._loop.get_debug(): 48 ↛ 49line 48 didn't jump to line 49 because the condition on line 48 was never true
49 if isinstance(args, (bytes, str)):
50 program = args
51 else:
52 program = args[0]
53 logger.debug('process %r created: pid %s',
54 program, self._pid)
56 self._loop.create_task(self._connect_pipes(waiter))
58 def __repr__(self):
59 info = [self.__class__.__name__]
60 if self._closed: 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true
61 info.append('closed')
62 if self._pid is not None:
63 info.append(f'pid={self._pid}')
64 if self._returncode is not None:
65 info.append(f'returncode={self._returncode}')
66 elif self._pid is not None:
67 info.append('running')
68 else:
69 info.append('not started')
71 stdin = self._pipes.get(0)
72 if stdin is not None: 72 ↛ 73line 72 didn't jump to line 73 because the condition on line 72 was never true
73 info.append(f'stdin={stdin.pipe}')
75 stdout = self._pipes.get(1)
76 stderr = self._pipes.get(2)
77 if stdout is not None and stderr is stdout: 77 ↛ 78line 77 didn't jump to line 78 because the condition on line 77 was never true
78 info.append(f'stdout=stderr={stdout.pipe}')
79 else:
80 if stdout is not None: 80 ↛ 81line 80 didn't jump to line 81 because the condition on line 80 was never true
81 info.append(f'stdout={stdout.pipe}')
82 if stderr is not None: 82 ↛ 83line 82 didn't jump to line 83 because the condition on line 82 was never true
83 info.append(f'stderr={stderr.pipe}')
85 return '<{}>'.format(' '.join(info))
87 def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
88 raise NotImplementedError
90 def set_protocol(self, protocol):
91 self._protocol = protocol
93 def get_protocol(self):
94 return self._protocol
96 def is_closing(self):
97 return self._closed
99 def close(self):
100 if self._closed:
101 return
102 self._closed = True
104 for proto in self._pipes.values():
105 if proto is None:
106 continue
107 # See gh-114177
108 # skip closing the pipe if loop is already closed
109 # this can happen e.g. when loop is closed immediately after
110 # process is killed
111 if self._loop and not self._loop.is_closed():
112 proto.pipe.close()
114 if (self._proc is not None and
115 # has the child process finished?
116 self._returncode is None and
117 # the child process has finished, but the
118 # transport hasn't been notified yet?
119 self._proc.poll() is None):
121 if self._loop.get_debug(): 121 ↛ 122line 121 didn't jump to line 122 because the condition on line 121 was never true
122 logger.warning('Close running child process: kill %r', self)
124 try:
125 self._proc.kill()
126 except (ProcessLookupError, PermissionError):
127 # the process may have already exited or may be running setuid
128 pass
130 # Don't clear the _proc reference yet: _post_init() may still run
132 def __del__(self, _warn=warnings.warn):
133 if not self._closed: 133 ↛ 134line 133 didn't jump to line 134 because the condition on line 133 was never true
134 _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
135 self.close()
137 def get_pid(self):
138 return self._pid
140 def get_returncode(self):
141 return self._returncode
143 def get_pipe_transport(self, fd):
144 if fd in self._pipes:
145 return self._pipes[fd].pipe
146 else:
147 return None
149 def _check_proc(self):
150 if self._proc is None:
151 raise ProcessLookupError()
153 if sys.platform == 'win32': 153 ↛ 154line 153 didn't jump to line 154 because the condition on line 153 was never true
154 def send_signal(self, signal):
155 self._check_proc()
156 self._proc.send_signal(signal)
158 def terminate(self):
159 self._check_proc()
160 self._proc.terminate()
162 def kill(self):
163 self._check_proc()
164 self._proc.kill()
165 else:
166 def send_signal(self, signal):
167 self._check_proc()
168 try:
169 os.kill(self._proc.pid, signal)
170 except ProcessLookupError:
171 pass
173 def terminate(self):
174 self.send_signal(signal.SIGTERM)
176 def kill(self):
177 self.send_signal(signal.SIGKILL)
179 async def _connect_pipes(self, waiter):
180 try:
181 proc = self._proc
182 loop = self._loop
184 if proc.stdin is not None:
185 _, pipe = await loop.connect_write_pipe(
186 lambda: WriteSubprocessPipeProto(self, 0),
187 proc.stdin)
188 self._pipes[0] = pipe
190 if proc.stdout is not None:
191 _, pipe = await loop.connect_read_pipe(
192 lambda: ReadSubprocessPipeProto(self, 1),
193 proc.stdout)
194 self._pipes[1] = pipe
196 if proc.stderr is not None:
197 _, pipe = await loop.connect_read_pipe(
198 lambda: ReadSubprocessPipeProto(self, 2),
199 proc.stderr)
200 self._pipes[2] = pipe
202 assert self._pending_calls is not None
204 loop.call_soon(self._protocol.connection_made, self)
205 for callback, data in self._pending_calls:
206 loop.call_soon(callback, *data)
207 self._pending_calls = None
208 except (SystemExit, KeyboardInterrupt):
209 raise
210 except BaseException as exc:
211 if waiter is not None and not waiter.cancelled():
212 waiter.set_exception(exc)
213 else:
214 if waiter is not None and not waiter.cancelled():
215 waiter.set_result(None)
217 def _call(self, cb, *data):
218 if self._pending_calls is not None:
219 self._pending_calls.append((cb, data))
220 else:
221 self._loop.call_soon(cb, *data)
223 def _pipe_connection_lost(self, fd, exc):
224 self._call(self._protocol.pipe_connection_lost, fd, exc)
225 self._try_finish()
227 def _pipe_data_received(self, fd, data):
228 self._call(self._protocol.pipe_data_received, fd, data)
230 def _process_exited(self, returncode):
231 assert returncode is not None, returncode
232 assert self._returncode is None, self._returncode
233 if self._loop.get_debug(): 233 ↛ 234line 233 didn't jump to line 234 because the condition on line 233 was never true
234 logger.info('%r exited with return code %r', self, returncode)
235 self._returncode = returncode
236 if self._proc.returncode is None:
237 # asyncio uses a child watcher: copy the status into the Popen
238 # object. On Python 3.6, it is required to avoid a ResourceWarning.
239 self._proc.returncode = returncode
240 self._call(self._protocol.process_exited)
242 self._try_finish()
244 async def _wait(self):
245 """Wait until the process exit and return the process return code.
247 This method is a coroutine."""
248 if self._returncode is not None:
249 return self._returncode
251 waiter = self._loop.create_future()
252 self._exit_waiters.append(waiter)
253 return await waiter
255 def _try_finish(self):
256 assert not self._finished
257 if self._returncode is None:
258 return
259 if all(p is not None and p.disconnected
260 for p in self._pipes.values()):
261 self._finished = True
262 self._call(self._call_connection_lost, None)
264 def _call_connection_lost(self, exc):
265 try:
266 self._protocol.connection_lost(exc)
267 finally:
268 # wake up futures waiting for wait()
269 for waiter in self._exit_waiters:
270 if not waiter.cancelled():
271 waiter.set_result(self._returncode)
272 self._exit_waiters = None
273 self._loop = None
274 self._proc = None
275 self._protocol = None
278class WriteSubprocessPipeProto(protocols.BaseProtocol):
280 def __init__(self, proc, fd):
281 self.proc = proc
282 self.fd = fd
283 self.pipe = None
284 self.disconnected = False
286 def connection_made(self, transport):
287 self.pipe = transport
289 def __repr__(self):
290 return f'<{self.__class__.__name__} fd={self.fd} pipe={self.pipe!r}>'
292 def connection_lost(self, exc):
293 self.disconnected = True
294 self.proc._pipe_connection_lost(self.fd, exc)
295 self.proc = None
297 def pause_writing(self):
298 self.proc._protocol.pause_writing()
300 def resume_writing(self):
301 self.proc._protocol.resume_writing()
304class ReadSubprocessPipeProto(WriteSubprocessPipeProto,
305 protocols.Protocol):
307 def data_received(self, data):
308 self.proc._pipe_data_received(self.fd, data)