您的位置:首页 > 编程语言 > Python开发

python模块:subprocess

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


python:subprocess
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: