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