ยปCore Development>Code coverage>Lib/subprocess.py

Python code coverage for Lib/subprocess.py

#countcontent
1n/a# subprocess - Subprocesses with accessible I/O streams
2n/a#
3n/a# For more information about this module, see PEP 324.
4n/a#
5n/a# Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
6n/a#
7n/a# Licensed to PSF under a Contributor Agreement.
8n/a# See http://www.python.org/2.4/license for licensing details.
9n/a
10n/ar"""Subprocesses with accessible I/O streams
11n/a
12n/aThis module allows you to spawn processes, connect to their
13n/ainput/output/error pipes, and obtain their return codes.
14n/a
15n/aFor a complete description of this module see the Python documentation.
16n/a
17n/aMain API
18n/a========
19n/arun(...): Runs a command, waits for it to complete, then returns a
20n/a CompletedProcess instance.
21n/aPopen(...): A class for flexibly executing a command in a new process
22n/a
23n/aConstants
24n/a---------
25n/aDEVNULL: Special value that indicates that os.devnull should be used
26n/aPIPE: Special value that indicates a pipe should be created
27n/aSTDOUT: Special value that indicates that stderr should go to stdout
28n/a
29n/a
30n/aOlder API
31n/a=========
32n/acall(...): Runs a command, waits for it to complete, then returns
33n/a the return code.
34n/acheck_call(...): Same as call() but raises CalledProcessError()
35n/a if return code is not 0
36n/acheck_output(...): Same as check_call() but returns the contents of
37n/a stdout instead of a return code
38n/agetoutput(...): Runs a command in the shell, waits for it to complete,
39n/a then returns the output
40n/agetstatusoutput(...): Runs a command in the shell, waits for it to complete,
41n/a then returns a (status, output) tuple
42n/a"""
43n/a
44n/aimport sys
45n/a_mswindows = (sys.platform == "win32")
46n/a
47n/aimport io
48n/aimport os
49n/aimport time
50n/aimport signal
51n/aimport builtins
52n/aimport warnings
53n/aimport errno
54n/afrom time import monotonic as _time
55n/a
56n/a# Exception classes used by this module.
57n/aclass SubprocessError(Exception): pass
58n/a
59n/a
60n/aclass CalledProcessError(SubprocessError):
61n/a """Raised when run() is called with check=True and the process
62n/a returns a non-zero exit status.
63n/a
64n/a Attributes:
65n/a cmd, returncode, stdout, stderr, output
66n/a """
67n/a def __init__(self, returncode, cmd, output=None, stderr=None):
68n/a self.returncode = returncode
69n/a self.cmd = cmd
70n/a self.output = output
71n/a self.stderr = stderr
72n/a
73n/a def __str__(self):
74n/a if self.returncode and self.returncode < 0:
75n/a try:
76n/a return "Command '%s' died with %r." % (
77n/a self.cmd, signal.Signals(-self.returncode))
78n/a except ValueError:
79n/a return "Command '%s' died with unknown signal %d." % (
80n/a self.cmd, -self.returncode)
81n/a else:
82n/a return "Command '%s' returned non-zero exit status %d." % (
83n/a self.cmd, self.returncode)
84n/a
85n/a @property
86n/a def stdout(self):
87n/a """Alias for output attribute, to match stderr"""
88n/a return self.output
89n/a
90n/a @stdout.setter
91n/a def stdout(self, value):
92n/a # There's no obvious reason to set this, but allow it anyway so
93n/a # .stdout is a transparent alias for .output
94n/a self.output = value
95n/a
96n/a
97n/aclass TimeoutExpired(SubprocessError):
98n/a """This exception is raised when the timeout expires while waiting for a
99n/a child process.
100n/a
101n/a Attributes:
102n/a cmd, output, stdout, stderr, timeout
103n/a """
104n/a def __init__(self, cmd, timeout, output=None, stderr=None):
105n/a self.cmd = cmd
106n/a self.timeout = timeout
107n/a self.output = output
108n/a self.stderr = stderr
109n/a
110n/a def __str__(self):
111n/a return ("Command '%s' timed out after %s seconds" %
112n/a (self.cmd, self.timeout))
113n/a
114n/a @property
115n/a def stdout(self):
116n/a return self.output
117n/a
118n/a @stdout.setter
119n/a def stdout(self, value):
120n/a # There's no obvious reason to set this, but allow it anyway so
121n/a # .stdout is a transparent alias for .output
122n/a self.output = value
123n/a
124n/a
125n/aif _mswindows:
126n/a import threading
127n/a import msvcrt
128n/a import _winapi
129n/a class STARTUPINFO:
130n/a dwFlags = 0
131n/a hStdInput = None
132n/a hStdOutput = None
133n/a hStdError = None
134n/a wShowWindow = 0
135n/aelse:
136n/a import _posixsubprocess
137n/a import select
138n/a import selectors
139n/a try:
140n/a import threading
141n/a except ImportError:
142n/a import dummy_threading as threading
143n/a
144n/a # When select or poll has indicated that the file is writable,
145n/a # we can write up to _PIPE_BUF bytes without risk of blocking.
146n/a # POSIX defines PIPE_BUF as >= 512.
147n/a _PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
148n/a
149n/a # poll/select have the advantage of not requiring any extra file
150n/a # descriptor, contrarily to epoll/kqueue (also, they require a single
151n/a # syscall).
152n/a if hasattr(selectors, 'PollSelector'):
153n/a _PopenSelector = selectors.PollSelector
154n/a else:
155n/a _PopenSelector = selectors.SelectSelector
156n/a
157n/a
158n/a__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "getstatusoutput",
159n/a "getoutput", "check_output", "run", "CalledProcessError", "DEVNULL",
160n/a "SubprocessError", "TimeoutExpired", "CompletedProcess"]
161n/a # NOTE: We intentionally exclude list2cmdline as it is
162n/a # considered an internal implementation detail. issue10838.
163n/a
164n/aif _mswindows:
165n/a from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
166n/a STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
167n/a STD_ERROR_HANDLE, SW_HIDE,
168n/a STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW)
169n/a
170n/a __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
171n/a "STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
172n/a "STD_ERROR_HANDLE", "SW_HIDE",
173n/a "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW",
174n/a "STARTUPINFO"])
175n/a
176n/a class Handle(int):
177n/a closed = False
178n/a
179n/a def Close(self, CloseHandle=_winapi.CloseHandle):
180n/a if not self.closed:
181n/a self.closed = True
182n/a CloseHandle(self)
183n/a
184n/a def Detach(self):
185n/a if not self.closed:
186n/a self.closed = True
187n/a return int(self)
188n/a raise ValueError("already closed")
189n/a
190n/a def __repr__(self):
191n/a return "%s(%d)" % (self.__class__.__name__, int(self))
192n/a
193n/a __del__ = Close
194n/a __str__ = __repr__
195n/a
196n/a
197n/a# This lists holds Popen instances for which the underlying process had not
198n/a# exited at the time its __del__ method got called: those processes are wait()ed
199n/a# for synchronously from _cleanup() when a new Popen object is created, to avoid
200n/a# zombie processes.
201n/a_active = []
202n/a
203n/adef _cleanup():
204n/a for inst in _active[:]:
205n/a res = inst._internal_poll(_deadstate=sys.maxsize)
206n/a if res is not None:
207n/a try:
208n/a _active.remove(inst)
209n/a except ValueError:
210n/a # This can happen if two threads create a new Popen instance.
211n/a # It's harmless that it was already removed, so ignore.
212n/a pass
213n/a
214n/aPIPE = -1
215n/aSTDOUT = -2
216n/aDEVNULL = -3
217n/a
218n/a
219n/a# XXX This function is only used by multiprocessing and the test suite,
220n/a# but it's here so that it can be imported when Python is compiled without
221n/a# threads.
222n/a
223n/adef _optim_args_from_interpreter_flags():
224n/a """Return a list of command-line arguments reproducing the current
225n/a optimization settings in sys.flags."""
226n/a args = []
227n/a value = sys.flags.optimize
228n/a if value > 0:
229n/a args.append('-' + 'O' * value)
230n/a return args
231n/a
232n/a
233n/adef _args_from_interpreter_flags():
234n/a """Return a list of command-line arguments reproducing the current
235n/a settings in sys.flags and sys.warnoptions."""
236n/a flag_opt_map = {
237n/a 'debug': 'd',
238n/a # 'inspect': 'i',
239n/a # 'interactive': 'i',
240n/a 'dont_write_bytecode': 'B',
241n/a 'no_user_site': 's',
242n/a 'no_site': 'S',
243n/a 'ignore_environment': 'E',
244n/a 'verbose': 'v',
245n/a 'bytes_warning': 'b',
246n/a 'quiet': 'q',
247n/a # -O is handled in _optim_args_from_interpreter_flags()
248n/a }
249n/a args = _optim_args_from_interpreter_flags()
250n/a for flag, opt in flag_opt_map.items():
251n/a v = getattr(sys.flags, flag)
252n/a if v > 0:
253n/a args.append('-' + opt * v)
254n/a for opt in sys.warnoptions:
255n/a args.append('-W' + opt)
256n/a return args
257n/a
258n/a
259n/adef call(*popenargs, timeout=None, **kwargs):
260n/a """Run command with arguments. Wait for command to complete or
261n/a timeout, then return the returncode attribute.
262n/a
263n/a The arguments are the same as for the Popen constructor. Example:
264n/a
265n/a retcode = call(["ls", "-l"])
266n/a """
267n/a with Popen(*popenargs, **kwargs) as p:
268n/a try:
269n/a return p.wait(timeout=timeout)
270n/a except:
271n/a p.kill()
272n/a p.wait()
273n/a raise
274n/a
275n/a
276n/adef check_call(*popenargs, **kwargs):
277n/a """Run command with arguments. Wait for command to complete. If
278n/a the exit code was zero then return, otherwise raise
279n/a CalledProcessError. The CalledProcessError object will have the
280n/a return code in the returncode attribute.
281n/a
282n/a The arguments are the same as for the call function. Example:
283n/a
284n/a check_call(["ls", "-l"])
285n/a """
286n/a retcode = call(*popenargs, **kwargs)
287n/a if retcode:
288n/a cmd = kwargs.get("args")
289n/a if cmd is None:
290n/a cmd = popenargs[0]
291n/a raise CalledProcessError(retcode, cmd)
292n/a return 0
293n/a
294n/a
295n/adef check_output(*popenargs, timeout=None, **kwargs):
296n/a r"""Run command with arguments and return its output.
297n/a
298n/a If the exit code was non-zero it raises a CalledProcessError. The
299n/a CalledProcessError object will have the return code in the returncode
300n/a attribute and output in the output attribute.
301n/a
302n/a The arguments are the same as for the Popen constructor. Example:
303n/a
304n/a >>> check_output(["ls", "-l", "/dev/null"])
305n/a b'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
306n/a
307n/a The stdout argument is not allowed as it is used internally.
308n/a To capture standard error in the result, use stderr=STDOUT.
309n/a
310n/a >>> check_output(["/bin/sh", "-c",
311n/a ... "ls -l non_existent_file ; exit 0"],
312n/a ... stderr=STDOUT)
313n/a b'ls: non_existent_file: No such file or directory\n'
314n/a
315n/a There is an additional optional argument, "input", allowing you to
316n/a pass a string to the subprocess's stdin. If you use this argument
317n/a you may not also use the Popen constructor's "stdin" argument, as
318n/a it too will be used internally. Example:
319n/a
320n/a >>> check_output(["sed", "-e", "s/foo/bar/"],
321n/a ... input=b"when in the course of fooman events\n")
322n/a b'when in the course of barman events\n'
323n/a
324n/a If universal_newlines=True is passed, the "input" argument must be a
325n/a string and the return value will be a string rather than bytes.
326n/a """
327n/a if 'stdout' in kwargs:
328n/a raise ValueError('stdout argument not allowed, it will be overridden.')
329n/a
330n/a if 'input' in kwargs and kwargs['input'] is None:
331n/a # Explicitly passing input=None was previously equivalent to passing an
332n/a # empty string. That is maintained here for backwards compatibility.
333n/a kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''
334n/a
335n/a return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
336n/a **kwargs).stdout
337n/a
338n/a
339n/aclass CompletedProcess(object):
340n/a """A process that has finished running.
341n/a
342n/a This is returned by run().
343n/a
344n/a Attributes:
345n/a args: The list or str args passed to run().
346n/a returncode: The exit code of the process, negative for signals.
347n/a stdout: The standard output (None if not captured).
348n/a stderr: The standard error (None if not captured).
349n/a """
350n/a def __init__(self, args, returncode, stdout=None, stderr=None):
351n/a self.args = args
352n/a self.returncode = returncode
353n/a self.stdout = stdout
354n/a self.stderr = stderr
355n/a
356n/a def __repr__(self):
357n/a args = ['args={!r}'.format(self.args),
358n/a 'returncode={!r}'.format(self.returncode)]
359n/a if self.stdout is not None:
360n/a args.append('stdout={!r}'.format(self.stdout))
361n/a if self.stderr is not None:
362n/a args.append('stderr={!r}'.format(self.stderr))
363n/a return "{}({})".format(type(self).__name__, ', '.join(args))
364n/a
365n/a def check_returncode(self):
366n/a """Raise CalledProcessError if the exit code is non-zero."""
367n/a if self.returncode:
368n/a raise CalledProcessError(self.returncode, self.args, self.stdout,
369n/a self.stderr)
370n/a
371n/a
372n/adef run(*popenargs, input=None, timeout=None, check=False, **kwargs):
373n/a """Run command with arguments and return a CompletedProcess instance.
374n/a
375n/a The returned instance will have attributes args, returncode, stdout and
376n/a stderr. By default, stdout and stderr are not captured, and those attributes
377n/a will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.
378n/a
379n/a If check is True and the exit code was non-zero, it raises a
380n/a CalledProcessError. The CalledProcessError object will have the return code
381n/a in the returncode attribute, and output & stderr attributes if those streams
382n/a were captured.
383n/a
384n/a If timeout is given, and the process takes too long, a TimeoutExpired
385n/a exception will be raised.
386n/a
387n/a There is an optional argument "input", allowing you to
388n/a pass a string to the subprocess's stdin. If you use this argument
389n/a you may not also use the Popen constructor's "stdin" argument, as
390n/a it will be used internally.
391n/a
392n/a The other arguments are the same as for the Popen constructor.
393n/a
394n/a If universal_newlines=True is passed, the "input" argument must be a
395n/a string and stdout/stderr in the returned object will be strings rather than
396n/a bytes.
397n/a """
398n/a if input is not None:
399n/a if 'stdin' in kwargs:
400n/a raise ValueError('stdin and input arguments may not both be used.')
401n/a kwargs['stdin'] = PIPE
402n/a
403n/a with Popen(*popenargs, **kwargs) as process:
404n/a try:
405n/a stdout, stderr = process.communicate(input, timeout=timeout)
406n/a except TimeoutExpired:
407n/a process.kill()
408n/a stdout, stderr = process.communicate()
409n/a raise TimeoutExpired(process.args, timeout, output=stdout,
410n/a stderr=stderr)
411n/a except:
412n/a process.kill()
413n/a process.wait()
414n/a raise
415n/a retcode = process.poll()
416n/a if check and retcode:
417n/a raise CalledProcessError(retcode, process.args,
418n/a output=stdout, stderr=stderr)
419n/a return CompletedProcess(process.args, retcode, stdout, stderr)
420n/a
421n/a
422n/adef list2cmdline(seq):
423n/a """
424n/a Translate a sequence of arguments into a command line
425n/a string, using the same rules as the MS C runtime:
426n/a
427n/a 1) Arguments are delimited by white space, which is either a
428n/a space or a tab.
429n/a
430n/a 2) A string surrounded by double quotation marks is
431n/a interpreted as a single argument, regardless of white space
432n/a contained within. A quoted string can be embedded in an
433n/a argument.
434n/a
435n/a 3) A double quotation mark preceded by a backslash is
436n/a interpreted as a literal double quotation mark.
437n/a
438n/a 4) Backslashes are interpreted literally, unless they
439n/a immediately precede a double quotation mark.
440n/a
441n/a 5) If backslashes immediately precede a double quotation mark,
442n/a every pair of backslashes is interpreted as a literal
443n/a backslash. If the number of backslashes is odd, the last
444n/a backslash escapes the next double quotation mark as
445n/a described in rule 3.
446n/a """
447n/a
448n/a # See
449n/a # http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
450n/a # or search http://msdn.microsoft.com for
451n/a # "Parsing C++ Command-Line Arguments"
452n/a result = []
453n/a needquote = False
454n/a for arg in seq:
455n/a bs_buf = []
456n/a
457n/a # Add a space to separate this argument from the others
458n/a if result:
459n/a result.append(' ')
460n/a
461n/a needquote = (" " in arg) or ("\t" in arg) or not arg
462n/a if needquote:
463n/a result.append('"')
464n/a
465n/a for c in arg:
466n/a if c == '\\':
467n/a # Don't know if we need to double yet.
468n/a bs_buf.append(c)
469n/a elif c == '"':
470n/a # Double backslashes.
471n/a result.append('\\' * len(bs_buf)*2)
472n/a bs_buf = []
473n/a result.append('\\"')
474n/a else:
475n/a # Normal char
476n/a if bs_buf:
477n/a result.extend(bs_buf)
478n/a bs_buf = []
479n/a result.append(c)
480n/a
481n/a # Add remaining backslashes, if any.
482n/a if bs_buf:
483n/a result.extend(bs_buf)
484n/a
485n/a if needquote:
486n/a result.extend(bs_buf)
487n/a result.append('"')
488n/a
489n/a return ''.join(result)
490n/a
491n/a
492n/a# Various tools for executing commands and looking at their output and status.
493n/a#
494n/a
495n/adef getstatusoutput(cmd):
496n/a """ Return (status, output) of executing cmd in a shell.
497n/a
498n/a Execute the string 'cmd' in a shell with 'check_output' and
499n/a return a 2-tuple (status, output). The locale encoding is used
500n/a to decode the output and process newlines.
501n/a
502n/a A trailing newline is stripped from the output.
503n/a The exit status for the command can be interpreted
504n/a according to the rules for the function 'wait'. Example:
505n/a
506n/a >>> import subprocess
507n/a >>> subprocess.getstatusoutput('ls /bin/ls')
508n/a (0, '/bin/ls')
509n/a >>> subprocess.getstatusoutput('cat /bin/junk')
510n/a (256, 'cat: /bin/junk: No such file or directory')
511n/a >>> subprocess.getstatusoutput('/bin/junk')
512n/a (256, 'sh: /bin/junk: not found')
513n/a """
514n/a try:
515n/a data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT)
516n/a status = 0
517n/a except CalledProcessError as ex:
518n/a data = ex.output
519n/a status = ex.returncode
520n/a if data[-1:] == '\n':
521n/a data = data[:-1]
522n/a return status, data
523n/a
524n/adef getoutput(cmd):
525n/a """Return output (stdout or stderr) of executing cmd in a shell.
526n/a
527n/a Like getstatusoutput(), except the exit status is ignored and the return
528n/a value is a string containing the command's output. Example:
529n/a
530n/a >>> import subprocess
531n/a >>> subprocess.getoutput('ls /bin/ls')
532n/a '/bin/ls'
533n/a """
534n/a return getstatusoutput(cmd)[1]
535n/a
536n/a
537n/a_PLATFORM_DEFAULT_CLOSE_FDS = object()
538n/a
539n/a
540n/aclass Popen(object):
541n/a """ Execute a child program in a new process.
542n/a
543n/a For a complete description of the arguments see the Python documentation.
544n/a
545n/a Arguments:
546n/a args: A string, or a sequence of program arguments.
547n/a
548n/a bufsize: supplied as the buffering argument to the open() function when
549n/a creating the stdin/stdout/stderr pipe file objects
550n/a
551n/a executable: A replacement program to execute.
552n/a
553n/a stdin, stdout and stderr: These specify the executed programs' standard
554n/a input, standard output and standard error file handles, respectively.
555n/a
556n/a preexec_fn: (POSIX only) An object to be called in the child process
557n/a just before the child is executed.
558n/a
559n/a close_fds: Controls closing or inheriting of file descriptors.
560n/a
561n/a shell: If true, the command will be executed through the shell.
562n/a
563n/a cwd: Sets the current directory before the child is executed.
564n/a
565n/a env: Defines the environment variables for the new process.
566n/a
567n/a universal_newlines: If true, use universal line endings for file
568n/a objects stdin, stdout and stderr.
569n/a
570n/a startupinfo and creationflags (Windows only)
571n/a
572n/a restore_signals (POSIX only)
573n/a
574n/a start_new_session (POSIX only)
575n/a
576n/a pass_fds (POSIX only)
577n/a
578n/a encoding and errors: Text mode encoding and error handling to use for
579n/a file objects stdin, stdout and stderr.
580n/a
581n/a Attributes:
582n/a stdin, stdout, stderr, pid, returncode
583n/a """
584n/a _child_created = False # Set here since __del__ checks it
585n/a
586n/a def __init__(self, args, bufsize=-1, executable=None,
587n/a stdin=None, stdout=None, stderr=None,
588n/a preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
589n/a shell=False, cwd=None, env=None, universal_newlines=False,
590n/a startupinfo=None, creationflags=0,
591n/a restore_signals=True, start_new_session=False,
592n/a pass_fds=(), *, encoding=None, errors=None):
593n/a """Create new Popen instance."""
594n/a _cleanup()
595n/a # Held while anything is calling waitpid before returncode has been
596n/a # updated to prevent clobbering returncode if wait() or poll() are
597n/a # called from multiple threads at once. After acquiring the lock,
598n/a # code must re-check self.returncode to see if another thread just
599n/a # finished a waitpid() call.
600n/a self._waitpid_lock = threading.Lock()
601n/a
602n/a self._input = None
603n/a self._communication_started = False
604n/a if bufsize is None:
605n/a bufsize = -1 # Restore default
606n/a if not isinstance(bufsize, int):
607n/a raise TypeError("bufsize must be an integer")
608n/a
609n/a if _mswindows:
610n/a if preexec_fn is not None:
611n/a raise ValueError("preexec_fn is not supported on Windows "
612n/a "platforms")
613n/a any_stdio_set = (stdin is not None or stdout is not None or
614n/a stderr is not None)
615n/a if close_fds is _PLATFORM_DEFAULT_CLOSE_FDS:
616n/a if any_stdio_set:
617n/a close_fds = False
618n/a else:
619n/a close_fds = True
620n/a elif close_fds and any_stdio_set:
621n/a raise ValueError(
622n/a "close_fds is not supported on Windows platforms"
623n/a " if you redirect stdin/stdout/stderr")
624n/a else:
625n/a # POSIX
626n/a if close_fds is _PLATFORM_DEFAULT_CLOSE_FDS:
627n/a close_fds = True
628n/a if pass_fds and not close_fds:
629n/a warnings.warn("pass_fds overriding close_fds.", RuntimeWarning)
630n/a close_fds = True
631n/a if startupinfo is not None:
632n/a raise ValueError("startupinfo is only supported on Windows "
633n/a "platforms")
634n/a if creationflags != 0:
635n/a raise ValueError("creationflags is only supported on Windows "
636n/a "platforms")
637n/a
638n/a self.args = args
639n/a self.stdin = None
640n/a self.stdout = None
641n/a self.stderr = None
642n/a self.pid = None
643n/a self.returncode = None
644n/a self.universal_newlines = universal_newlines
645n/a self.encoding = encoding
646n/a self.errors = errors
647n/a
648n/a # Input and output objects. The general principle is like
649n/a # this:
650n/a #
651n/a # Parent Child
652n/a # ------ -----
653n/a # p2cwrite ---stdin---> p2cread
654n/a # c2pread <--stdout--- c2pwrite
655n/a # errread <--stderr--- errwrite
656n/a #
657n/a # On POSIX, the child objects are file descriptors. On
658n/a # Windows, these are Windows file handles. The parent objects
659n/a # are file descriptors on both platforms. The parent objects
660n/a # are -1 when not using PIPEs. The child objects are -1
661n/a # when not redirecting.
662n/a
663n/a (p2cread, p2cwrite,
664n/a c2pread, c2pwrite,
665n/a errread, errwrite) = self._get_handles(stdin, stdout, stderr)
666n/a
667n/a # We wrap OS handles *before* launching the child, otherwise a
668n/a # quickly terminating child could make our fds unwrappable
669n/a # (see #8458).
670n/a
671n/a if _mswindows:
672n/a if p2cwrite != -1:
673n/a p2cwrite = msvcrt.open_osfhandle(p2cwrite.Detach(), 0)
674n/a if c2pread != -1:
675n/a c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0)
676n/a if errread != -1:
677n/a errread = msvcrt.open_osfhandle(errread.Detach(), 0)
678n/a
679n/a text_mode = encoding or errors or universal_newlines
680n/a
681n/a self._closed_child_pipe_fds = False
682n/a
683n/a try:
684n/a if p2cwrite != -1:
685n/a self.stdin = io.open(p2cwrite, 'wb', bufsize)
686n/a if text_mode:
687n/a self.stdin = io.TextIOWrapper(self.stdin, write_through=True,
688n/a line_buffering=(bufsize == 1),
689n/a encoding=encoding, errors=errors)
690n/a if c2pread != -1:
691n/a self.stdout = io.open(c2pread, 'rb', bufsize)
692n/a if text_mode:
693n/a self.stdout = io.TextIOWrapper(self.stdout,
694n/a encoding=encoding, errors=errors)
695n/a if errread != -1:
696n/a self.stderr = io.open(errread, 'rb', bufsize)
697n/a if text_mode:
698n/a self.stderr = io.TextIOWrapper(self.stderr,
699n/a encoding=encoding, errors=errors)
700n/a
701n/a self._execute_child(args, executable, preexec_fn, close_fds,
702n/a pass_fds, cwd, env,
703n/a startupinfo, creationflags, shell,
704n/a p2cread, p2cwrite,
705n/a c2pread, c2pwrite,
706n/a errread, errwrite,
707n/a restore_signals, start_new_session)
708n/a except:
709n/a # Cleanup if the child failed starting.
710n/a for f in filter(None, (self.stdin, self.stdout, self.stderr)):
711n/a try:
712n/a f.close()
713n/a except OSError:
714n/a pass # Ignore EBADF or other errors.
715n/a
716n/a if not self._closed_child_pipe_fds:
717n/a to_close = []
718n/a if stdin == PIPE:
719n/a to_close.append(p2cread)
720n/a if stdout == PIPE:
721n/a to_close.append(c2pwrite)
722n/a if stderr == PIPE:
723n/a to_close.append(errwrite)
724n/a if hasattr(self, '_devnull'):
725n/a to_close.append(self._devnull)
726n/a for fd in to_close:
727n/a try:
728n/a os.close(fd)
729n/a except OSError:
730n/a pass
731n/a
732n/a raise
733n/a
734n/a def _translate_newlines(self, data, encoding, errors):
735n/a data = data.decode(encoding, errors)
736n/a return data.replace("\r\n", "\n").replace("\r", "\n")
737n/a
738n/a def __enter__(self):
739n/a return self
740n/a
741n/a def __exit__(self, type, value, traceback):
742n/a if self.stdout:
743n/a self.stdout.close()
744n/a if self.stderr:
745n/a self.stderr.close()
746n/a try: # Flushing a BufferedWriter may raise an error
747n/a if self.stdin:
748n/a self.stdin.close()
749n/a finally:
750n/a # Wait for the process to terminate, to avoid zombies.
751n/a self.wait()
752n/a
753n/a def __del__(self, _maxsize=sys.maxsize, _warn=warnings.warn):
754n/a if not self._child_created:
755n/a # We didn't get to successfully create a child process.
756n/a return
757n/a if self.returncode is None:
758n/a # Not reading subprocess exit status creates a zombi process which
759n/a # is only destroyed at the parent python process exit
760n/a _warn("subprocess %s is still running" % self.pid,
761n/a ResourceWarning, source=self)
762n/a # In case the child hasn't been waited on, check if it's done.
763n/a self._internal_poll(_deadstate=_maxsize)
764n/a if self.returncode is None and _active is not None:
765n/a # Child is still running, keep us alive until we can wait on it.
766n/a _active.append(self)
767n/a
768n/a def _get_devnull(self):
769n/a if not hasattr(self, '_devnull'):
770n/a self._devnull = os.open(os.devnull, os.O_RDWR)
771n/a return self._devnull
772n/a
773n/a def _stdin_write(self, input):
774n/a if input:
775n/a try:
776n/a self.stdin.write(input)
777n/a except BrokenPipeError:
778n/a pass # communicate() must ignore broken pipe errors.
779n/a except OSError as e:
780n/a if e.errno == errno.EINVAL and self.poll() is not None:
781n/a # Issue #19612: On Windows, stdin.write() fails with EINVAL
782n/a # if the process already exited before the write
783n/a pass
784n/a else:
785n/a raise
786n/a try:
787n/a self.stdin.close()
788n/a except BrokenPipeError:
789n/a pass # communicate() must ignore broken pipe errors.
790n/a except OSError as e:
791n/a if e.errno == errno.EINVAL and self.poll() is not None:
792n/a pass
793n/a else:
794n/a raise
795n/a
796n/a def communicate(self, input=None, timeout=None):
797n/a """Interact with process: Send data to stdin. Read data from
798n/a stdout and stderr, until end-of-file is reached. Wait for
799n/a process to terminate.
800n/a
801n/a The optional "input" argument should be data to be sent to the
802n/a child process (if self.universal_newlines is True, this should
803n/a be a string; if it is False, "input" should be bytes), or
804n/a None, if no data should be sent to the child.
805n/a
806n/a communicate() returns a tuple (stdout, stderr). These will be
807n/a bytes or, if self.universal_newlines was True, a string.
808n/a """
809n/a
810n/a if self._communication_started and input:
811n/a raise ValueError("Cannot send input after starting communication")
812n/a
813n/a # Optimization: If we are not worried about timeouts, we haven't
814n/a # started communicating, and we have one or zero pipes, using select()
815n/a # or threads is unnecessary.
816n/a if (timeout is None and not self._communication_started and
817n/a [self.stdin, self.stdout, self.stderr].count(None) >= 2):
818n/a stdout = None
819n/a stderr = None
820n/a if self.stdin:
821n/a self._stdin_write(input)
822n/a elif self.stdout:
823n/a stdout = self.stdout.read()
824n/a self.stdout.close()
825n/a elif self.stderr:
826n/a stderr = self.stderr.read()
827n/a self.stderr.close()
828n/a self.wait()
829n/a else:
830n/a if timeout is not None:
831n/a endtime = _time() + timeout
832n/a else:
833n/a endtime = None
834n/a
835n/a try:
836n/a stdout, stderr = self._communicate(input, endtime, timeout)
837n/a finally:
838n/a self._communication_started = True
839n/a
840n/a sts = self.wait(timeout=self._remaining_time(endtime))
841n/a
842n/a return (stdout, stderr)
843n/a
844n/a
845n/a def poll(self):
846n/a """Check if child process has terminated. Set and return returncode
847n/a attribute."""
848n/a return self._internal_poll()
849n/a
850n/a
851n/a def _remaining_time(self, endtime):
852n/a """Convenience for _communicate when computing timeouts."""
853n/a if endtime is None:
854n/a return None
855n/a else:
856n/a return endtime - _time()
857n/a
858n/a
859n/a def _check_timeout(self, endtime, orig_timeout):
860n/a """Convenience for checking if a timeout has expired."""
861n/a if endtime is None:
862n/a return
863n/a if _time() > endtime:
864n/a raise TimeoutExpired(self.args, orig_timeout)
865n/a
866n/a
867n/a if _mswindows:
868n/a #
869n/a # Windows methods
870n/a #
871n/a def _get_handles(self, stdin, stdout, stderr):
872n/a """Construct and return tuple with IO objects:
873n/a p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
874n/a """
875n/a if stdin is None and stdout is None and stderr is None:
876n/a return (-1, -1, -1, -1, -1, -1)
877n/a
878n/a p2cread, p2cwrite = -1, -1
879n/a c2pread, c2pwrite = -1, -1
880n/a errread, errwrite = -1, -1
881n/a
882n/a if stdin is None:
883n/a p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
884n/a if p2cread is None:
885n/a p2cread, _ = _winapi.CreatePipe(None, 0)
886n/a p2cread = Handle(p2cread)
887n/a _winapi.CloseHandle(_)
888n/a elif stdin == PIPE:
889n/a p2cread, p2cwrite = _winapi.CreatePipe(None, 0)
890n/a p2cread, p2cwrite = Handle(p2cread), Handle(p2cwrite)
891n/a elif stdin == DEVNULL:
892n/a p2cread = msvcrt.get_osfhandle(self._get_devnull())
893n/a elif isinstance(stdin, int):
894n/a p2cread = msvcrt.get_osfhandle(stdin)
895n/a else:
896n/a # Assuming file-like object
897n/a p2cread = msvcrt.get_osfhandle(stdin.fileno())
898n/a p2cread = self._make_inheritable(p2cread)
899n/a
900n/a if stdout is None:
901n/a c2pwrite = _winapi.GetStdHandle(_winapi.STD_OUTPUT_HANDLE)
902n/a if c2pwrite is None:
903n/a _, c2pwrite = _winapi.CreatePipe(None, 0)
904n/a c2pwrite = Handle(c2pwrite)
905n/a _winapi.CloseHandle(_)
906n/a elif stdout == PIPE:
907n/a c2pread, c2pwrite = _winapi.CreatePipe(None, 0)
908n/a c2pread, c2pwrite = Handle(c2pread), Handle(c2pwrite)
909n/a elif stdout == DEVNULL:
910n/a c2pwrite = msvcrt.get_osfhandle(self._get_devnull())
911n/a elif isinstance(stdout, int):
912n/a c2pwrite = msvcrt.get_osfhandle(stdout)
913n/a else:
914n/a # Assuming file-like object
915n/a c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
916n/a c2pwrite = self._make_inheritable(c2pwrite)
917n/a
918n/a if stderr is None:
919n/a errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
920n/a if errwrite is None:
921n/a _, errwrite = _winapi.CreatePipe(None, 0)
922n/a errwrite = Handle(errwrite)
923n/a _winapi.CloseHandle(_)
924n/a elif stderr == PIPE:
925n/a errread, errwrite = _winapi.CreatePipe(None, 0)
926n/a errread, errwrite = Handle(errread), Handle(errwrite)
927n/a elif stderr == STDOUT:
928n/a errwrite = c2pwrite
929n/a elif stderr == DEVNULL:
930n/a errwrite = msvcrt.get_osfhandle(self._get_devnull())
931n/a elif isinstance(stderr, int):
932n/a errwrite = msvcrt.get_osfhandle(stderr)
933n/a else:
934n/a # Assuming file-like object
935n/a errwrite = msvcrt.get_osfhandle(stderr.fileno())
936n/a errwrite = self._make_inheritable(errwrite)
937n/a
938n/a return (p2cread, p2cwrite,
939n/a c2pread, c2pwrite,
940n/a errread, errwrite)
941n/a
942n/a
943n/a def _make_inheritable(self, handle):
944n/a """Return a duplicate of handle, which is inheritable"""
945n/a h = _winapi.DuplicateHandle(
946n/a _winapi.GetCurrentProcess(), handle,
947n/a _winapi.GetCurrentProcess(), 0, 1,
948n/a _winapi.DUPLICATE_SAME_ACCESS)
949n/a return Handle(h)
950n/a
951n/a
952n/a def _execute_child(self, args, executable, preexec_fn, close_fds,
953n/a pass_fds, cwd, env,
954n/a startupinfo, creationflags, shell,
955n/a p2cread, p2cwrite,
956n/a c2pread, c2pwrite,
957n/a errread, errwrite,
958n/a unused_restore_signals, unused_start_new_session):
959n/a """Execute program (MS Windows version)"""
960n/a
961n/a assert not pass_fds, "pass_fds not supported on Windows."
962n/a
963n/a if not isinstance(args, str):
964n/a args = list2cmdline(args)
965n/a
966n/a # Process startup details
967n/a if startupinfo is None:
968n/a startupinfo = STARTUPINFO()
969n/a if -1 not in (p2cread, c2pwrite, errwrite):
970n/a startupinfo.dwFlags |= _winapi.STARTF_USESTDHANDLES
971n/a startupinfo.hStdInput = p2cread
972n/a startupinfo.hStdOutput = c2pwrite
973n/a startupinfo.hStdError = errwrite
974n/a
975n/a if shell:
976n/a startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
977n/a startupinfo.wShowWindow = _winapi.SW_HIDE
978n/a comspec = os.environ.get("COMSPEC", "cmd.exe")
979n/a args = '{} /c "{}"'.format (comspec, args)
980n/a
981n/a # Start the process
982n/a try:
983n/a hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
984n/a # no special security
985n/a None, None,
986n/a int(not close_fds),
987n/a creationflags,
988n/a env,
989n/a cwd,
990n/a startupinfo)
991n/a finally:
992n/a # Child is launched. Close the parent's copy of those pipe
993n/a # handles that only the child should have open. You need
994n/a # to make sure that no handles to the write end of the
995n/a # output pipe are maintained in this process or else the
996n/a # pipe will not close when the child process exits and the
997n/a # ReadFile will hang.
998n/a if p2cread != -1:
999n/a p2cread.Close()
1000n/a if c2pwrite != -1:
1001n/a c2pwrite.Close()
1002n/a if errwrite != -1:
1003n/a errwrite.Close()
1004n/a if hasattr(self, '_devnull'):
1005n/a os.close(self._devnull)
1006n/a
1007n/a # Retain the process handle, but close the thread handle
1008n/a self._child_created = True
1009n/a self._handle = Handle(hp)
1010n/a self.pid = pid
1011n/a _winapi.CloseHandle(ht)
1012n/a
1013n/a def _internal_poll(self, _deadstate=None,
1014n/a _WaitForSingleObject=_winapi.WaitForSingleObject,
1015n/a _WAIT_OBJECT_0=_winapi.WAIT_OBJECT_0,
1016n/a _GetExitCodeProcess=_winapi.GetExitCodeProcess):
1017n/a """Check if child process has terminated. Returns returncode
1018n/a attribute.
1019n/a
1020n/a This method is called by __del__, so it can only refer to objects
1021n/a in its local scope.
1022n/a
1023n/a """
1024n/a if self.returncode is None:
1025n/a if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
1026n/a self.returncode = _GetExitCodeProcess(self._handle)
1027n/a return self.returncode
1028n/a
1029n/a
1030n/a def wait(self, timeout=None):
1031n/a """Wait for child process to terminate. Returns returncode
1032n/a attribute."""
1033n/a if timeout is None:
1034n/a timeout_millis = _winapi.INFINITE
1035n/a else:
1036n/a timeout_millis = int(timeout * 1000)
1037n/a if self.returncode is None:
1038n/a result = _winapi.WaitForSingleObject(self._handle,
1039n/a timeout_millis)
1040n/a if result == _winapi.WAIT_TIMEOUT:
1041n/a raise TimeoutExpired(self.args, timeout)
1042n/a self.returncode = _winapi.GetExitCodeProcess(self._handle)
1043n/a return self.returncode
1044n/a
1045n/a
1046n/a def _readerthread(self, fh, buffer):
1047n/a buffer.append(fh.read())
1048n/a fh.close()
1049n/a
1050n/a
1051n/a def _communicate(self, input, endtime, orig_timeout):
1052n/a # Start reader threads feeding into a list hanging off of this
1053n/a # object, unless they've already been started.
1054n/a if self.stdout and not hasattr(self, "_stdout_buff"):
1055n/a self._stdout_buff = []
1056n/a self.stdout_thread = \
1057n/a threading.Thread(target=self._readerthread,
1058n/a args=(self.stdout, self._stdout_buff))
1059n/a self.stdout_thread.daemon = True
1060n/a self.stdout_thread.start()
1061n/a if self.stderr and not hasattr(self, "_stderr_buff"):
1062n/a self._stderr_buff = []
1063n/a self.stderr_thread = \
1064n/a threading.Thread(target=self._readerthread,
1065n/a args=(self.stderr, self._stderr_buff))
1066n/a self.stderr_thread.daemon = True
1067n/a self.stderr_thread.start()
1068n/a
1069n/a if self.stdin:
1070n/a self._stdin_write(input)
1071n/a
1072n/a # Wait for the reader threads, or time out. If we time out, the
1073n/a # threads remain reading and the fds left open in case the user
1074n/a # calls communicate again.
1075n/a if self.stdout is not None:
1076n/a self.stdout_thread.join(self._remaining_time(endtime))
1077n/a if self.stdout_thread.is_alive():
1078n/a raise TimeoutExpired(self.args, orig_timeout)
1079n/a if self.stderr is not None:
1080n/a self.stderr_thread.join(self._remaining_time(endtime))
1081n/a if self.stderr_thread.is_alive():
1082n/a raise TimeoutExpired(self.args, orig_timeout)
1083n/a
1084n/a # Collect the output from and close both pipes, now that we know
1085n/a # both have been read successfully.
1086n/a stdout = None
1087n/a stderr = None
1088n/a if self.stdout:
1089n/a stdout = self._stdout_buff
1090n/a self.stdout.close()
1091n/a if self.stderr:
1092n/a stderr = self._stderr_buff
1093n/a self.stderr.close()
1094n/a
1095n/a # All data exchanged. Translate lists into strings.
1096n/a if stdout is not None:
1097n/a stdout = stdout[0]
1098n/a if stderr is not None:
1099n/a stderr = stderr[0]
1100n/a
1101n/a return (stdout, stderr)
1102n/a
1103n/a def send_signal(self, sig):
1104n/a """Send a signal to the process."""
1105n/a # Don't signal a process that we know has already died.
1106n/a if self.returncode is not None:
1107n/a return
1108n/a if sig == signal.SIGTERM:
1109n/a self.terminate()
1110n/a elif sig == signal.CTRL_C_EVENT:
1111n/a os.kill(self.pid, signal.CTRL_C_EVENT)
1112n/a elif sig == signal.CTRL_BREAK_EVENT:
1113n/a os.kill(self.pid, signal.CTRL_BREAK_EVENT)
1114n/a else:
1115n/a raise ValueError("Unsupported signal: {}".format(sig))
1116n/a
1117n/a def terminate(self):
1118n/a """Terminates the process."""
1119n/a # Don't terminate a process that we know has already died.
1120n/a if self.returncode is not None:
1121n/a return
1122n/a try:
1123n/a _winapi.TerminateProcess(self._handle, 1)
1124n/a except PermissionError:
1125n/a # ERROR_ACCESS_DENIED (winerror 5) is received when the
1126n/a # process already died.
1127n/a rc = _winapi.GetExitCodeProcess(self._handle)
1128n/a if rc == _winapi.STILL_ACTIVE:
1129n/a raise
1130n/a self.returncode = rc
1131n/a
1132n/a kill = terminate
1133n/a
1134n/a else:
1135n/a #
1136n/a # POSIX methods
1137n/a #
1138n/a def _get_handles(self, stdin, stdout, stderr):
1139n/a """Construct and return tuple with IO objects:
1140n/a p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
1141n/a """
1142n/a p2cread, p2cwrite = -1, -1
1143n/a c2pread, c2pwrite = -1, -1
1144n/a errread, errwrite = -1, -1
1145n/a
1146n/a if stdin is None:
1147n/a pass
1148n/a elif stdin == PIPE:
1149n/a p2cread, p2cwrite = os.pipe()
1150n/a elif stdin == DEVNULL:
1151n/a p2cread = self._get_devnull()
1152n/a elif isinstance(stdin, int):
1153n/a p2cread = stdin
1154n/a else:
1155n/a # Assuming file-like object
1156n/a p2cread = stdin.fileno()
1157n/a
1158n/a if stdout is None:
1159n/a pass
1160n/a elif stdout == PIPE:
1161n/a c2pread, c2pwrite = os.pipe()
1162n/a elif stdout == DEVNULL:
1163n/a c2pwrite = self._get_devnull()
1164n/a elif isinstance(stdout, int):
1165n/a c2pwrite = stdout
1166n/a else:
1167n/a # Assuming file-like object
1168n/a c2pwrite = stdout.fileno()
1169n/a
1170n/a if stderr is None:
1171n/a pass
1172n/a elif stderr == PIPE:
1173n/a errread, errwrite = os.pipe()
1174n/a elif stderr == STDOUT:
1175n/a if c2pwrite != -1:
1176n/a errwrite = c2pwrite
1177n/a else: # child's stdout is not set, use parent's stdout
1178n/a errwrite = sys.__stdout__.fileno()
1179n/a elif stderr == DEVNULL:
1180n/a errwrite = self._get_devnull()
1181n/a elif isinstance(stderr, int):
1182n/a errwrite = stderr
1183n/a else:
1184n/a # Assuming file-like object
1185n/a errwrite = stderr.fileno()
1186n/a
1187n/a return (p2cread, p2cwrite,
1188n/a c2pread, c2pwrite,
1189n/a errread, errwrite)
1190n/a
1191n/a
1192n/a def _execute_child(self, args, executable, preexec_fn, close_fds,
1193n/a pass_fds, cwd, env,
1194n/a startupinfo, creationflags, shell,
1195n/a p2cread, p2cwrite,
1196n/a c2pread, c2pwrite,
1197n/a errread, errwrite,
1198n/a restore_signals, start_new_session):
1199n/a """Execute program (POSIX version)"""
1200n/a
1201n/a if isinstance(args, (str, bytes)):
1202n/a args = [args]
1203n/a else:
1204n/a args = list(args)
1205n/a
1206n/a if shell:
1207n/a # On Android the default shell is at '/system/bin/sh'.
1208n/a unix_shell = ('/system/bin/sh' if
1209n/a hasattr(sys, 'getandroidapilevel') else '/bin/sh')
1210n/a args = [unix_shell, "-c"] + args
1211n/a if executable:
1212n/a args[0] = executable
1213n/a
1214n/a if executable is None:
1215n/a executable = args[0]
1216n/a orig_executable = executable
1217n/a
1218n/a # For transferring possible exec failure from child to parent.
1219n/a # Data format: "exception name:hex errno:description"
1220n/a # Pickle is not used; it is complex and involves memory allocation.
1221n/a errpipe_read, errpipe_write = os.pipe()
1222n/a # errpipe_write must not be in the standard io 0, 1, or 2 fd range.
1223n/a low_fds_to_close = []
1224n/a while errpipe_write < 3:
1225n/a low_fds_to_close.append(errpipe_write)
1226n/a errpipe_write = os.dup(errpipe_write)
1227n/a for low_fd in low_fds_to_close:
1228n/a os.close(low_fd)
1229n/a try:
1230n/a try:
1231n/a # We must avoid complex work that could involve
1232n/a # malloc or free in the child process to avoid
1233n/a # potential deadlocks, thus we do all this here.
1234n/a # and pass it to fork_exec()
1235n/a
1236n/a if env is not None:
1237n/a env_list = [os.fsencode(k) + b'=' + os.fsencode(v)
1238n/a for k, v in env.items()]
1239n/a else:
1240n/a env_list = None # Use execv instead of execve.
1241n/a executable = os.fsencode(executable)
1242n/a if os.path.dirname(executable):
1243n/a executable_list = (executable,)
1244n/a else:
1245n/a # This matches the behavior of os._execvpe().
1246n/a executable_list = tuple(
1247n/a os.path.join(os.fsencode(dir), executable)
1248n/a for dir in os.get_exec_path(env))
1249n/a fds_to_keep = set(pass_fds)
1250n/a fds_to_keep.add(errpipe_write)
1251n/a self.pid = _posixsubprocess.fork_exec(
1252n/a args, executable_list,
1253n/a close_fds, sorted(fds_to_keep), cwd, env_list,
1254n/a p2cread, p2cwrite, c2pread, c2pwrite,
1255n/a errread, errwrite,
1256n/a errpipe_read, errpipe_write,
1257n/a restore_signals, start_new_session, preexec_fn)
1258n/a self._child_created = True
1259n/a finally:
1260n/a # be sure the FD is closed no matter what
1261n/a os.close(errpipe_write)
1262n/a
1263n/a # self._devnull is not always defined.
1264n/a devnull_fd = getattr(self, '_devnull', None)
1265n/a if p2cread != -1 and p2cwrite != -1 and p2cread != devnull_fd:
1266n/a os.close(p2cread)
1267n/a if c2pwrite != -1 and c2pread != -1 and c2pwrite != devnull_fd:
1268n/a os.close(c2pwrite)
1269n/a if errwrite != -1 and errread != -1 and errwrite != devnull_fd:
1270n/a os.close(errwrite)
1271n/a if devnull_fd is not None:
1272n/a os.close(devnull_fd)
1273n/a # Prevent a double close of these fds from __init__ on error.
1274n/a self._closed_child_pipe_fds = True
1275n/a
1276n/a # Wait for exec to fail or succeed; possibly raising an
1277n/a # exception (limited in size)
1278n/a errpipe_data = bytearray()
1279n/a while True:
1280n/a part = os.read(errpipe_read, 50000)
1281n/a errpipe_data += part
1282n/a if not part or len(errpipe_data) > 50000:
1283n/a break
1284n/a finally:
1285n/a # be sure the FD is closed no matter what
1286n/a os.close(errpipe_read)
1287n/a
1288n/a if errpipe_data:
1289n/a try:
1290n/a pid, sts = os.waitpid(self.pid, 0)
1291n/a if pid == self.pid:
1292n/a self._handle_exitstatus(sts)
1293n/a else:
1294n/a self.returncode = sys.maxsize
1295n/a except ChildProcessError:
1296n/a pass
1297n/a
1298n/a try:
1299n/a exception_name, hex_errno, err_msg = (
1300n/a errpipe_data.split(b':', 2))
1301n/a except ValueError:
1302n/a exception_name = b'SubprocessError'
1303n/a hex_errno = b'0'
1304n/a err_msg = (b'Bad exception data from child: ' +
1305n/a repr(errpipe_data))
1306n/a child_exception_type = getattr(
1307n/a builtins, exception_name.decode('ascii'),
1308n/a SubprocessError)
1309n/a err_msg = err_msg.decode(errors="surrogatepass")
1310n/a if issubclass(child_exception_type, OSError) and hex_errno:
1311n/a errno_num = int(hex_errno, 16)
1312n/a child_exec_never_called = (err_msg == "noexec")
1313n/a if child_exec_never_called:
1314n/a err_msg = ""
1315n/a if errno_num != 0:
1316n/a err_msg = os.strerror(errno_num)
1317n/a if errno_num == errno.ENOENT:
1318n/a if child_exec_never_called:
1319n/a # The error must be from chdir(cwd).
1320n/a err_msg += ': ' + repr(cwd)
1321n/a else:
1322n/a err_msg += ': ' + repr(orig_executable)
1323n/a raise child_exception_type(errno_num, err_msg)
1324n/a raise child_exception_type(err_msg)
1325n/a
1326n/a
1327n/a def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
1328n/a _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
1329n/a _WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
1330n/a _WSTOPSIG=os.WSTOPSIG):
1331n/a """All callers to this function MUST hold self._waitpid_lock."""
1332n/a # This method is called (indirectly) by __del__, so it cannot
1333n/a # refer to anything outside of its local scope.
1334n/a if _WIFSIGNALED(sts):
1335n/a self.returncode = -_WTERMSIG(sts)
1336n/a elif _WIFEXITED(sts):
1337n/a self.returncode = _WEXITSTATUS(sts)
1338n/a elif _WIFSTOPPED(sts):
1339n/a self.returncode = -_WSTOPSIG(sts)
1340n/a else:
1341n/a # Should never happen
1342n/a raise SubprocessError("Unknown child exit status!")
1343n/a
1344n/a
1345n/a def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
1346n/a _WNOHANG=os.WNOHANG, _ECHILD=errno.ECHILD):
1347n/a """Check if child process has terminated. Returns returncode
1348n/a attribute.
1349n/a
1350n/a This method is called by __del__, so it cannot reference anything
1351n/a outside of the local scope (nor can any methods it calls).
1352n/a
1353n/a """
1354n/a if self.returncode is None:
1355n/a if not self._waitpid_lock.acquire(False):
1356n/a # Something else is busy calling waitpid. Don't allow two
1357n/a # at once. We know nothing yet.
1358n/a return None
1359n/a try:
1360n/a if self.returncode is not None:
1361n/a return self.returncode # Another thread waited.
1362n/a pid, sts = _waitpid(self.pid, _WNOHANG)
1363n/a if pid == self.pid:
1364n/a self._handle_exitstatus(sts)
1365n/a except OSError as e:
1366n/a if _deadstate is not None:
1367n/a self.returncode = _deadstate
1368n/a elif e.errno == _ECHILD:
1369n/a # This happens if SIGCLD is set to be ignored or
1370n/a # waiting for child processes has otherwise been
1371n/a # disabled for our process. This child is dead, we
1372n/a # can't get the status.
1373n/a # http://bugs.python.org/issue15756
1374n/a self.returncode = 0
1375n/a finally:
1376n/a self._waitpid_lock.release()
1377n/a return self.returncode
1378n/a
1379n/a
1380n/a def _try_wait(self, wait_flags):
1381n/a """All callers to this function MUST hold self._waitpid_lock."""
1382n/a try:
1383n/a (pid, sts) = os.waitpid(self.pid, wait_flags)
1384n/a except ChildProcessError:
1385n/a # This happens if SIGCLD is set to be ignored or waiting
1386n/a # for child processes has otherwise been disabled for our
1387n/a # process. This child is dead, we can't get the status.
1388n/a pid = self.pid
1389n/a sts = 0
1390n/a return (pid, sts)
1391n/a
1392n/a
1393n/a def wait(self, timeout=None):
1394n/a """Wait for child process to terminate. Returns returncode
1395n/a attribute."""
1396n/a if self.returncode is not None:
1397n/a return self.returncode
1398n/a
1399n/a if timeout is not None:
1400n/a endtime = _time() + timeout
1401n/a # Enter a busy loop if we have a timeout. This busy loop was
1402n/a # cribbed from Lib/threading.py in Thread.wait() at r71065.
1403n/a delay = 0.0005 # 500 us -> initial delay of 1 ms
1404n/a while True:
1405n/a if self._waitpid_lock.acquire(False):
1406n/a try:
1407n/a if self.returncode is not None:
1408n/a break # Another thread waited.
1409n/a (pid, sts) = self._try_wait(os.WNOHANG)
1410n/a assert pid == self.pid or pid == 0
1411n/a if pid == self.pid:
1412n/a self._handle_exitstatus(sts)
1413n/a break
1414n/a finally:
1415n/a self._waitpid_lock.release()
1416n/a remaining = self._remaining_time(endtime)
1417n/a if remaining <= 0:
1418n/a raise TimeoutExpired(self.args, timeout)
1419n/a delay = min(delay * 2, remaining, .05)
1420n/a time.sleep(delay)
1421n/a else:
1422n/a while self.returncode is None:
1423n/a with self._waitpid_lock:
1424n/a if self.returncode is not None:
1425n/a break # Another thread waited.
1426n/a (pid, sts) = self._try_wait(0)
1427n/a # Check the pid and loop as waitpid has been known to
1428n/a # return 0 even without WNOHANG in odd situations.
1429n/a # http://bugs.python.org/issue14396.
1430n/a if pid == self.pid:
1431n/a self._handle_exitstatus(sts)
1432n/a return self.returncode
1433n/a
1434n/a
1435n/a def _communicate(self, input, endtime, orig_timeout):
1436n/a if self.stdin and not self._communication_started:
1437n/a # Flush stdio buffer. This might block, if the user has
1438n/a # been writing to .stdin in an uncontrolled fashion.
1439n/a try:
1440n/a self.stdin.flush()
1441n/a except BrokenPipeError:
1442n/a pass # communicate() must ignore BrokenPipeError.
1443n/a if not input:
1444n/a try:
1445n/a self.stdin.close()
1446n/a except BrokenPipeError:
1447n/a pass # communicate() must ignore BrokenPipeError.
1448n/a
1449n/a stdout = None
1450n/a stderr = None
1451n/a
1452n/a # Only create this mapping if we haven't already.
1453n/a if not self._communication_started:
1454n/a self._fileobj2output = {}
1455n/a if self.stdout:
1456n/a self._fileobj2output[self.stdout] = []
1457n/a if self.stderr:
1458n/a self._fileobj2output[self.stderr] = []
1459n/a
1460n/a if self.stdout:
1461n/a stdout = self._fileobj2output[self.stdout]
1462n/a if self.stderr:
1463n/a stderr = self._fileobj2output[self.stderr]
1464n/a
1465n/a self._save_input(input)
1466n/a
1467n/a if self._input:
1468n/a input_view = memoryview(self._input)
1469n/a
1470n/a with _PopenSelector() as selector:
1471n/a if self.stdin and input:
1472n/a selector.register(self.stdin, selectors.EVENT_WRITE)
1473n/a if self.stdout:
1474n/a selector.register(self.stdout, selectors.EVENT_READ)
1475n/a if self.stderr:
1476n/a selector.register(self.stderr, selectors.EVENT_READ)
1477n/a
1478n/a while selector.get_map():
1479n/a timeout = self._remaining_time(endtime)
1480n/a if timeout is not None and timeout < 0:
1481n/a raise TimeoutExpired(self.args, orig_timeout)
1482n/a
1483n/a ready = selector.select(timeout)
1484n/a self._check_timeout(endtime, orig_timeout)
1485n/a
1486n/a # XXX Rewrite these to use non-blocking I/O on the file
1487n/a # objects; they are no longer using C stdio!
1488n/a
1489n/a for key, events in ready:
1490n/a if key.fileobj is self.stdin:
1491n/a chunk = input_view[self._input_offset :
1492n/a self._input_offset + _PIPE_BUF]
1493n/a try:
1494n/a self._input_offset += os.write(key.fd, chunk)
1495n/a except BrokenPipeError:
1496n/a selector.unregister(key.fileobj)
1497n/a key.fileobj.close()
1498n/a else:
1499n/a if self._input_offset >= len(self._input):
1500n/a selector.unregister(key.fileobj)
1501n/a key.fileobj.close()
1502n/a elif key.fileobj in (self.stdout, self.stderr):
1503n/a data = os.read(key.fd, 32768)
1504n/a if not data:
1505n/a selector.unregister(key.fileobj)
1506n/a key.fileobj.close()
1507n/a self._fileobj2output[key.fileobj].append(data)
1508n/a
1509n/a self.wait(timeout=self._remaining_time(endtime))
1510n/a
1511n/a # All data exchanged. Translate lists into strings.
1512n/a if stdout is not None:
1513n/a stdout = b''.join(stdout)
1514n/a if stderr is not None:
1515n/a stderr = b''.join(stderr)
1516n/a
1517n/a # Translate newlines, if requested.
1518n/a # This also turns bytes into strings.
1519n/a if self.encoding or self.errors or self.universal_newlines:
1520n/a if stdout is not None:
1521n/a stdout = self._translate_newlines(stdout,
1522n/a self.stdout.encoding,
1523n/a self.stdout.errors)
1524n/a if stderr is not None:
1525n/a stderr = self._translate_newlines(stderr,
1526n/a self.stderr.encoding,
1527n/a self.stderr.errors)
1528n/a
1529n/a return (stdout, stderr)
1530n/a
1531n/a
1532n/a def _save_input(self, input):
1533n/a # This method is called from the _communicate_with_*() methods
1534n/a # so that if we time out while communicating, we can continue
1535n/a # sending input if we retry.
1536n/a if self.stdin and self._input is None:
1537n/a self._input_offset = 0
1538n/a self._input = input
1539n/a if input is not None and (
1540n/a self.encoding or self.errors or self.universal_newlines):
1541n/a self._input = self._input.encode(self.stdin.encoding,
1542n/a self.stdin.errors)
1543n/a
1544n/a
1545n/a def send_signal(self, sig):
1546n/a """Send a signal to the process."""
1547n/a # Skip signalling a process that we know has already died.
1548n/a if self.returncode is None:
1549n/a os.kill(self.pid, sig)
1550n/a
1551n/a def terminate(self):
1552n/a """Terminate the process with SIGTERM
1553n/a """
1554n/a self.send_signal(signal.SIGTERM)
1555n/a
1556n/a def kill(self):
1557n/a """Kill the process with SIGKILL
1558n/a """
1559n/a self.send_signal(signal.SIGKILL)