Coverage for Lib/asyncio/base_subprocess.py: 82%
210 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-15 02:02 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-15 02:02 +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 proto.pipe.close()
109 if (self._proc is not None and
110 # has the child process finished?
111 self._returncode is None and
112 # the child process has finished, but the
113 # transport hasn't been notified yet?
114 self._proc.poll() is None):
116 if self._loop.get_debug(): 116 ↛ 117line 116 didn't jump to line 117 because the condition on line 116 was never true
117 logger.warning('Close running child process: kill %r', self)
119 try:
120 self._proc.kill()
121 except (ProcessLookupError, PermissionError):
122 # the process may have already exited or may be running setuid
123 pass
125 # Don't clear the _proc reference yet: _post_init() may still run
127 def __del__(self, _warn=warnings.warn):
128 if not self._closed: 128 ↛ 129line 128 didn't jump to line 129 because the condition on line 128 was never true
129 _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
130 self.close()
132 def get_pid(self):
133 return self._pid
135 def get_returncode(self):
136 return self._returncode
138 def get_pipe_transport(self, fd):
139 if fd in self._pipes:
140 return self._pipes[fd].pipe
141 else:
142 return None
144 def _check_proc(self):
145 if self._proc is None:
146 raise ProcessLookupError()
148 if sys.platform == 'win32': 148 ↛ 149line 148 didn't jump to line 149 because the condition on line 148 was never true
149 def send_signal(self, signal):
150 self._check_proc()
151 self._proc.send_signal(signal)
153 def terminate(self):
154 self._check_proc()
155 self._proc.terminate()
157 def kill(self):
158 self._check_proc()
159 self._proc.kill()
160 else:
161 def send_signal(self, signal):
162 self._check_proc()
163 try:
164 os.kill(self._proc.pid, signal)
165 except ProcessLookupError:
166 pass
168 def terminate(self):
169 self.send_signal(signal.SIGTERM)
171 def kill(self):
172 self.send_signal(signal.SIGKILL)
174 async def _connect_pipes(self, waiter):
175 try:
176 proc = self._proc
177 loop = self._loop
179 if proc.stdin is not None:
180 _, pipe = await loop.connect_write_pipe(
181 lambda: WriteSubprocessPipeProto(self, 0),
182 proc.stdin)
183 self._pipes[0] = pipe
185 if proc.stdout is not None:
186 _, pipe = await loop.connect_read_pipe(
187 lambda: ReadSubprocessPipeProto(self, 1),
188 proc.stdout)
189 self._pipes[1] = pipe
191 if proc.stderr is not None:
192 _, pipe = await loop.connect_read_pipe(
193 lambda: ReadSubprocessPipeProto(self, 2),
194 proc.stderr)
195 self._pipes[2] = pipe
197 assert self._pending_calls is not None
199 loop.call_soon(self._protocol.connection_made, self)
200 for callback, data in self._pending_calls:
201 loop.call_soon(callback, *data)
202 self._pending_calls = None
203 except (SystemExit, KeyboardInterrupt):
204 raise
205 except BaseException as exc:
206 if waiter is not None and not waiter.cancelled():
207 waiter.set_exception(exc)
208 else:
209 if waiter is not None and not waiter.cancelled():
210 waiter.set_result(None)
212 def _call(self, cb, *data):
213 if self._pending_calls is not None:
214 self._pending_calls.append((cb, data))
215 else:
216 self._loop.call_soon(cb, *data)
218 def _pipe_connection_lost(self, fd, exc):
219 self._call(self._protocol.pipe_connection_lost, fd, exc)
220 self._try_finish()
222 def _pipe_data_received(self, fd, data):
223 self._call(self._protocol.pipe_data_received, fd, data)
225 def _process_exited(self, returncode):
226 assert returncode is not None, returncode
227 assert self._returncode is None, self._returncode
228 if self._loop.get_debug(): 228 ↛ 229line 228 didn't jump to line 229 because the condition on line 228 was never true
229 logger.info('%r exited with return code %r', self, returncode)
230 self._returncode = returncode
231 if self._proc.returncode is None:
232 # asyncio uses a child watcher: copy the status into the Popen
233 # object. On Python 3.6, it is required to avoid a ResourceWarning.
234 self._proc.returncode = returncode
235 self._call(self._protocol.process_exited)
237 self._try_finish()
239 async def _wait(self):
240 """Wait until the process exit and return the process return code.
242 This method is a coroutine."""
243 if self._returncode is not None:
244 return self._returncode
246 waiter = self._loop.create_future()
247 self._exit_waiters.append(waiter)
248 return await waiter
250 def _try_finish(self):
251 assert not self._finished
252 if self._returncode is None:
253 return
254 if all(p is not None and p.disconnected
255 for p in self._pipes.values()):
256 self._finished = True
257 self._call(self._call_connection_lost, None)
259 def _call_connection_lost(self, exc):
260 try:
261 self._protocol.connection_lost(exc)
262 finally:
263 # wake up futures waiting for wait()
264 for waiter in self._exit_waiters:
265 if not waiter.cancelled():
266 waiter.set_result(self._returncode)
267 self._exit_waiters = None
268 self._loop = None
269 self._proc = None
270 self._protocol = None
273class WriteSubprocessPipeProto(protocols.BaseProtocol):
275 def __init__(self, proc, fd):
276 self.proc = proc
277 self.fd = fd
278 self.pipe = None
279 self.disconnected = False
281 def connection_made(self, transport):
282 self.pipe = transport
284 def __repr__(self):
285 return f'<{self.__class__.__name__} fd={self.fd} pipe={self.pipe!r}>'
287 def connection_lost(self, exc):
288 self.disconnected = True
289 self.proc._pipe_connection_lost(self.fd, exc)
290 self.proc = None
292 def pause_writing(self):
293 self.proc._protocol.pause_writing()
295 def resume_writing(self):
296 self.proc._protocol.resume_writing()
299class ReadSubprocessPipeProto(WriteSubprocessPipeProto,
300 protocols.Protocol):
302 def data_received(self, data):
303 self.proc._pipe_data_received(self.fd, data)