subprocess py file compiles
[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
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
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,c2pread, c2pwrite,errread, errwrite = self._get_handles(stdin, stdout, stderr)
658
659 # We wrap OS handles *before* launching the child, otherwise a
660 # quickly terminating child could make our fds unwrappable
661 # (see #8458).
662
663 if _mswindows:
664 if p2cwrite != -1:
665 p2cwrite = msvcrt.open_osfhandle(p2cwrite.Detach(), 0)
666 if c2pread != -1:
667 c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0)
668 if errread != -1:
669 errread = msvcrt.open_osfhandle(errread.Detach(), 0)
670
671 text_mode = encoding or errors or universal_newlines
672
673 self._closed_child_pipe_fds = False
674
675 try:
676 if p2cwrite != -1:
677 self.stdin = io.open(p2cwrite, 'wb', bufsize)
678 if text_mode:
679 self.stdin = io.TextIOWrapper(self.stdin, write_through=True,
680 line_buffering=(bufsize == 1),
681 encoding=encoding, errors=errors)
682 if c2pread != -1:
683 self.stdout = io.open(c2pread, 'rb', bufsize)
684 if text_mode:
685 self.stdout = io.TextIOWrapper(self.stdout,
686 encoding=encoding, errors=errors)
687 if errread != -1:
688 self.stderr = io.open(errread, 'rb', bufsize)
689 if text_mode:
690 self.stderr = io.TextIOWrapper(self.stderr,
691 encoding=encoding, errors=errors)
692
693 self._execute_child(args, executable, preexec_fn, close_fds,
694 pass_fds, cwd, env,
695 startupinfo, creationflags, shell,
696 p2cread, p2cwrite,
697 c2pread, c2pwrite,
698 errread, errwrite,
699 restore_signals, start_new_session)
700 except:
701 # Cleanup if the child failed starting.
702 for f in filter(None, (self.stdin, self.stdout, self.stderr)):
703 try:
704 f.close()
705 except OSError:
706 pass # Ignore EBADF or other errors.
707
708 if not self._closed_child_pipe_fds:
709 to_close = []
710 if stdin == PIPE:
711 to_close.append(p2cread)
712 if stdout == PIPE:
713 to_close.append(c2pwrite)
714 if stderr == PIPE:
715 to_close.append(errwrite)
716 if hasattr(self, '_devnull'):
717 to_close.append(self._devnull)
718 for fd in to_close:
719 try:
720 if _mswindows and isinstance(fd, Handle):
721 fd.Close()
722 else:
723 os.close(fd)
724 except OSError:
725 pass
726
727 raise
728
729 def _translate_newlines(self, data, encoding, errors):
730 data = data.decode(encoding, errors)
731 return data.replace("\r\n", "\n").replace("\r", "\n")
732
733 def __enter__(self):
734 return self
735
736 def __exit__(self, type, value, traceback):
737 if self.stdout:
738 self.stdout.close()
739 if self.stderr:
740 self.stderr.close()
741 try: # Flushing a BufferedWriter may raise an error
742 if self.stdin:
743 self.stdin.close()
744 finally:
745 # Wait for the process to terminate, to avoid zombies.
746 self.wait()
747
748 def __del__(self, _maxsize=sys.maxsize, _warn=warnings.warn):
749 if not self._child_created:
750 # We didn't get to successfully create a child process.
751 return
752 if self.returncode is None:
753 # Not reading subprocess exit status creates a zombi process which
754 # is only destroyed at the parent python process exit
755 _warn("subprocess %s is still running" % self.pid,
756 ResourceWarning, source=self)
757 # In case the child hasn't been waited on, check if it's done.
758 self._internal_poll(_deadstate=_maxsize)
759 if self.returncode is None and _active is not None:
760 # Child is still running, keep us alive until we can wait on it.
761 _active.append(self)
762
763 def _get_devnull(self):
764 if not hasattr(self, '_devnull'):
765 self._devnull = os.open(os.devnull, os.O_RDWR)
766 return self._devnull
767
768 def _stdin_write(self, input):
769 if input:
770 try:
771 self.stdin.write(input)
772 except BrokenPipeError:
773 pass # communicate() must ignore broken pipe errors.
774 except OSError as exc:
775 if exc.errno == errno.EINVAL:
776 # bpo-19612, bpo-30418: On Windows, stdin.write() fails
777 # with EINVAL if the child process exited or if the child
778 # process is still running but closed the pipe.
779 pass
780 else:
781 raise
782
783 try:
784 self.stdin.close()
785 except BrokenPipeError:
786 pass # communicate() must ignore broken pipe errors.
787 except OSError as exc:
788 if exc.errno == errno.EINVAL:
789 pass
790 else:
791 raise
792
793 def communicate(self, input=None, timeout=None):
794 """Interact with process: Send data to stdin. Read data from
795 stdout and stderr, until end-of-file is reached. Wait for
796 process to terminate.
797
798 The optional "input" argument should be data to be sent to the
799 child process (if self.universal_newlines is True, this should
800 be a string; if it is False, "input" should be bytes), or
801 None, if no data should be sent to the child.
802
803 communicate() returns a tuple (stdout, stderr). These will be
804 bytes or, if self.universal_newlines was True, a string.
805 """
806
807 if self._communication_started and input:
808 raise ValueError("Cannot send input after starting communication")
809
810 # Optimization: If we are not worried about timeouts, we haven't
811 # started communicating, and we have one or zero pipes, using select()
812 # or threads is unnecessary.
813 if (timeout is None and not self._communication_started and
814 [self.stdin, self.stdout, self.stderr].count(None) >= 2):
815 stdout = None
816 stderr = None
817 if self.stdin:
818 self._stdin_write(input)
819 elif self.stdout:
820 stdout = self.stdout.read()
821 self.stdout.close()
822 elif self.stderr:
823 stderr = self.stderr.read()
824 self.stderr.close()
825 self.wait()
826 else:
827 if timeout is not None:
828 endtime = _time() + timeout
829 else:
830 endtime = None
831
832 try:
833 stdout, stderr = self._communicate(input, endtime, timeout)
834 finally:
835 self._communication_started = True
836
837 sts = self.wait(timeout=self._remaining_time(endtime))
838
839 return (stdout, stderr)
840
841
842 def poll(self):
843 """Check if child process has terminated. Set and return returncode
844 attribute."""
845 return self._internal_poll()
846
847
848 def _remaining_time(self, endtime):
849 """Convenience for _communicate when computing timeouts."""
850 if endtime is None:
851 return None
852 else:
853 return endtime - _time()
854
855
856 def _check_timeout(self, endtime, orig_timeout):
857 """Convenience for checking if a timeout has expired."""
858 if endtime is None:
859 return
860 if _time() > endtime:
861 raise TimeoutExpired(self.args, orig_timeout)
862
863
864 if _mswindows:
865 #
866 # Windows methods
867 #
868 def _get_handles(self, stdin, stdout, stderr):
869 """Construct and return tuple with IO objects:
870 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
871 """
872 if stdin is None and stdout is None and stderr is None:
873 return (-1, -1, -1, -1, -1, -1)
874
875 p2cread, p2cwrite = -1, -1
876 c2pread, c2pwrite = -1, -1
877 errread, errwrite = -1, -1
878
879 if stdin is None:
880 p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
881 if p2cread is None:
882 p2cread, _ = _winapi.CreatePipe(None, 0)
883 p2cread = Handle(p2cread)
884 _winapi.CloseHandle(_)
885 elif stdin == PIPE:
886 p2cread, p2cwrite = _winapi.CreatePipe(None, 0)
887 p2cread, p2cwrite = Handle(p2cread), Handle(p2cwrite)
888 elif stdin == DEVNULL:
889 p2cread = msvcrt.get_osfhandle(self._get_devnull())
890 elif isinstance(stdin, int):
891 p2cread = msvcrt.get_osfhandle(stdin)
892 else:
893 # Assuming file-like object
894 p2cread = msvcrt.get_osfhandle(stdin.fileno())
895 p2cread = self._make_inheritable(p2cread)
896
897 if stdout is None:
898 c2pwrite = _winapi.GetStdHandle(_winapi.STD_OUTPUT_HANDLE)
899 if c2pwrite is None:
900 _, c2pwrite = _winapi.CreatePipe(None, 0)
901 c2pwrite = Handle(c2pwrite)
902 _winapi.CloseHandle(_)
903 elif stdout == PIPE:
904 c2pread, c2pwrite = _winapi.CreatePipe(None, 0)
905 c2pread, c2pwrite = Handle(c2pread), Handle(c2pwrite)
906 elif stdout == DEVNULL:
907 c2pwrite = msvcrt.get_osfhandle(self._get_devnull())
908 elif isinstance(stdout, int):
909 c2pwrite = msvcrt.get_osfhandle(stdout)
910 else:
911 # Assuming file-like object
912 c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
913 c2pwrite = self._make_inheritable(c2pwrite)
914
915 if stderr is None:
916 errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
917 if errwrite is None:
918 _, errwrite = _winapi.CreatePipe(None, 0)
919 errwrite = Handle(errwrite)
920 _winapi.CloseHandle(_)
921 elif stderr == PIPE:
922 errread, errwrite = _winapi.CreatePipe(None, 0)
923 errread, errwrite = Handle(errread), Handle(errwrite)
924 elif stderr == STDOUT:
925 errwrite = c2pwrite
926 elif stderr == DEVNULL:
927 errwrite = msvcrt.get_osfhandle(self._get_devnull())
928 elif isinstance(stderr, int):
929 errwrite = msvcrt.get_osfhandle(stderr)
930 else:
931 # Assuming file-like object
932 errwrite = msvcrt.get_osfhandle(stderr.fileno())
933 errwrite = self._make_inheritable(errwrite)
934
935 return (p2cread, p2cwrite,
936 c2pread, c2pwrite,
937 errread, errwrite)
938
939
940 def _make_inheritable(self, handle):
941 """Return a duplicate of handle, which is inheritable"""
942 h = _winapi.DuplicateHandle(
943 _winapi.GetCurrentProcess(), handle,
944 _winapi.GetCurrentProcess(), 0, 1,
945 _winapi.DUPLICATE_SAME_ACCESS)
946 return Handle(h)
947
948
949 def _execute_child(self, args, executable, preexec_fn, close_fds,
950 pass_fds, cwd, env,
951 startupinfo, creationflags, shell,
952 p2cread, p2cwrite,
953 c2pread, c2pwrite,
954 errread, errwrite,
955 unused_restore_signals, unused_start_new_session):
956 """Execute program (MS Windows version)"""
957
958 assert not pass_fds, "pass_fds not supported on Windows."
959
960 if not isinstance(args, str):
961 args = list2cmdline(args)
962
963 # Process startup details
964 if startupinfo is None:
965 startupinfo = STARTUPINFO()
966 if -1 not in (p2cread, c2pwrite, errwrite):
967 startupinfo.dwFlags |= _winapi.STARTF_USESTDHANDLES
968 startupinfo.hStdInput = p2cread
969 startupinfo.hStdOutput = c2pwrite
970 startupinfo.hStdError = errwrite
971
972 if shell:
973 startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
974 startupinfo.wShowWindow = _winapi.SW_HIDE
975 comspec = os.environ.get("COMSPEC", "cmd.exe")
976 args = '{} /c "{}"'.format (comspec, args)
977
978 # Start the process
979 try:
980 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
981 # no special security
982 None, None,
983 int(not close_fds),
984 creationflags,
985 env,
986 os.fspath(cwd) if cwd is not None else None,
987 startupinfo)
988 finally:
989 # Child is launched. Close the parent's copy of those pipe
990 # handles that only the child should have open. You need
991 # to make sure that no handles to the write end of the
992 # output pipe are maintained in this process or else the
993 # pipe will not close when the child process exits and the
994 # ReadFile will hang.
995 if p2cread != -1:
996 p2cread.Close()
997 if c2pwrite != -1:
998 c2pwrite.Close()
999 if errwrite != -1:
1000 errwrite.Close()
1001 if hasattr(self, '_devnull'):
1002 os.close(self._devnull)
1003 # Prevent a double close of these handles/fds from __init__
1004 # on error.
1005 self._closed_child_pipe_fds = True
1006
1007 # Retain the process handle, but close the thread handle
1008 self._child_created = True
1009 self._handle = Handle(hp)
1010 self.pid = pid
1011 _winapi.CloseHandle(ht)
1012
1013 def _internal_poll(self, _deadstate=None,
1014 _WaitForSingleObject=_winapi.WaitForSingleObject,
1015 _WAIT_OBJECT_0=_winapi.WAIT_OBJECT_0,
1016 _GetExitCodeProcess=_winapi.GetExitCodeProcess):
1017 """Check if child process has terminated. Returns returncode
1018 attribute.
1019
1020 This method is called by __del__, so it can only refer to objects
1021 in its local scope.
1022
1023 """
1024 if self.returncode is None:
1025 if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
1026 self.returncode = _GetExitCodeProcess(self._handle)
1027 return self.returncode
1028
1029
1030 def wait(self, timeout=None, endtime=None):
1031 """Wait for child process to terminate. Returns returncode
1032 attribute."""
1033 if endtime is not None:
1034 warnings.warn(
1035 "'endtime' argument is deprecated; use 'timeout'.",
1036 DeprecationWarning,
1037 stacklevel=2)
1038 timeout = self._remaining_time(endtime)
1039 if timeout is None:
1040 timeout_millis = _winapi.INFINITE
1041 else:
1042 timeout_millis = int(timeout * 1000)
1043 if self.returncode is None:
1044 result = _winapi.WaitForSingleObject(self._handle,
1045 timeout_millis)
1046 if result == _winapi.WAIT_TIMEOUT:
1047 raise TimeoutExpired(self.args, timeout)
1048 self.returncode = _winapi.GetExitCodeProcess(self._handle)
1049 return self.returncode
1050
1051
1052 def _readerthread(self, fh, buffer):
1053 buffer.append(fh.read())
1054 fh.close()
1055
1056
1057 def _communicate(self, input, endtime, orig_timeout):
1058 # Start reader threads feeding into a list hanging off of this
1059 # object, unless they've already been started.
1060 if self.stdout and not hasattr(self, "_stdout_buff"):
1061 self._stdout_buff = []
1062 self.stdout_thread = \
1063 threading.Thread(target=self._readerthread,
1064 args=(self.stdout, self._stdout_buff))
1065 self.stdout_thread.daemon = True
1066 self.stdout_thread.start()
1067 if self.stderr and not hasattr(self, "_stderr_buff"):
1068 self._stderr_buff = []
1069 self.stderr_thread = \
1070 threading.Thread(target=self._readerthread,
1071 args=(self.stderr, self._stderr_buff))
1072 self.stderr_thread.daemon = True
1073 self.stderr_thread.start()
1074
1075 if self.stdin:
1076 self._stdin_write(input)
1077
1078 # Wait for the reader threads, or time out. If we time out, the
1079 # threads remain reading and the fds left open in case the user
1080 # calls communicate again.
1081 if self.stdout is not None:
1082 self.stdout_thread.join(self._remaining_time(endtime))
1083 if self.stdout_thread.is_alive():
1084 raise TimeoutExpired(self.args, orig_timeout)
1085 if self.stderr is not None:
1086 self.stderr_thread.join(self._remaining_time(endtime))
1087 if self.stderr_thread.is_alive():
1088 raise TimeoutExpired(self.args, orig_timeout)
1089
1090 # Collect the output from and close both pipes, now that we know
1091 # both have been read successfully.
1092 stdout = None
1093 stderr = None
1094 if self.stdout:
1095 stdout = self._stdout_buff
1096 self.stdout.close()
1097 if self.stderr:
1098 stderr = self._stderr_buff
1099 self.stderr.close()
1100
1101 # All data exchanged. Translate lists into strings.
1102 if stdout is not None:
1103 stdout = stdout[0]
1104 if stderr is not None:
1105 stderr = stderr[0]
1106
1107 return (stdout, stderr)
1108
1109 def send_signal(self, sig):
1110 """Send a signal to the process."""
1111 # Don't signal a process that we know has already died.
1112 if self.returncode is not None:
1113 return
1114 if sig == signal.SIGTERM:
1115 self.terminate()
1116 elif sig == signal.CTRL_C_EVENT:
1117 os.kill(self.pid, signal.CTRL_C_EVENT)
1118 elif sig == signal.CTRL_BREAK_EVENT:
1119 os.kill(self.pid, signal.CTRL_BREAK_EVENT)
1120 else:
1121 raise ValueError("Unsupported signal: {}".format(sig))
1122
1123 def terminate(self):
1124 """Terminates the process."""
1125 # Don't terminate a process that we know has already died.
1126 if self.returncode is not None:
1127 return
1128 try:
1129 _winapi.TerminateProcess(self._handle, 1)
1130 except PermissionError:
1131 # ERROR_ACCESS_DENIED (winerror 5) is received when the
1132 # process already died.
1133 rc = _winapi.GetExitCodeProcess(self._handle)
1134 if rc == _winapi.STILL_ACTIVE:
1135 raise
1136 self.returncode = rc
1137
1138 kill = terminate
1139
1140 else:
1141 #
1142 # POSIX methods
1143 #
1144 def _get_handles(self, stdin, stdout, stderr):
1145 """Construct and return tuple with IO objects:
1146 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
1147 """
1148 p2cread, p2cwrite = -1, -1
1149 c2pread, c2pwrite = -1, -1
1150 errread, errwrite = -1, -1
1151
1152 if stdin is None:
1153 pass
1154 elif stdin == PIPE:
1155 p2cread, p2cwrite = os.pipe()
1156 elif stdin == DEVNULL:
1157 p2cread = self._get_devnull()
1158 elif isinstance(stdin, int):
1159 p2cread = stdin
1160 else:
1161 # Assuming file-like object
1162 p2cread = stdin.fileno()
1163
1164 if stdout is None:
1165 pass
1166 elif stdout == PIPE:
1167 c2pread, c2pwrite = os.pipe()
1168 elif stdout == DEVNULL:
1169 c2pwrite = self._get_devnull()
1170 elif isinstance(stdout, int):
1171 c2pwrite = stdout
1172 else:
1173 # Assuming file-like object
1174 c2pwrite = stdout.fileno()
1175
1176 if stderr is None:
1177 pass
1178 elif stderr == PIPE:
1179 errread, errwrite = os.pipe()
1180 elif stderr == STDOUT:
1181 if c2pwrite != -1:
1182 errwrite = c2pwrite
1183 else: # child's stdout is not set, use parent's stdout
1184 errwrite = sys.__stdout__.fileno()
1185 elif stderr == DEVNULL:
1186 errwrite = self._get_devnull()
1187 elif isinstance(stderr, int):
1188 errwrite = stderr
1189 else:
1190 # Assuming file-like object
1191 errwrite = stderr.fileno()
1192
1193 return (p2cread, p2cwrite,
1194 c2pread, c2pwrite,
1195 errread, errwrite)
1196
1197
1198 def _execute_child(self, args, executable, preexec_fn, close_fds,
1199 pass_fds, cwd, env,
1200 startupinfo, creationflags, shell,
1201 p2cread, p2cwrite,
1202 c2pread, c2pwrite,
1203 errread, errwrite,
1204 restore_signals, start_new_session):
1205 """Execute program (POSIX version)"""
1206
1207 if isinstance(args, (str, bytes)):
1208 args = [args]
1209 else:
1210 args = list(args)
1211
1212 if shell:
1213 args = ["/bin/sh", "-c"] + args
1214 if executable:
1215 args[0] = executable
1216
1217 if executable is None:
1218 executable = args[0]
1219 orig_executable = executable
1220
1221 # For transferring possible exec failure from child to parent.
1222 # Data format: "exception name:hex errno:description"
1223 # Pickle is not used; it is complex and involves memory allocation.
1224 errpipe_read, errpipe_write = os.pipe()
1225 # errpipe_write must not be in the standard io 0, 1, or 2 fd range.
1226 low_fds_to_close = []
1227 while errpipe_write < 3:
1228 low_fds_to_close.append(errpipe_write)
1229 errpipe_write = os.dup(errpipe_write)
1230 for low_fd in low_fds_to_close:
1231 os.close(low_fd)
1232 try:
1233 try:
1234 # We must avoid complex work that could involve
1235 # malloc or free in the child process to avoid
1236 # potential deadlocks, thus we do all this here.
1237 # and pass it to fork_exec()
1238
1239 if env is not None:
1240 env_list = []
1241 for k, v in env.items():
1242 k = os.fsencode(k)
1243 if b'=' in k:
1244 raise ValueError("illegal environment variable name")
1245 env_list.append(k + b'=' + os.fsencode(v))
1246 else:
1247 env_list = None # Use execv instead of execve.
1248 executable = os.fsencode(executable)
1249 if os.path.dirname(executable):
1250 executable_list = (executable,)
1251 else:
1252 # This matches the behavior of os._execvpe().
1253 executable_list = tuple(
1254 os.path.join(os.fsencode(dir), executable)
1255 for dir in os.get_exec_path(env))
1256 fds_to_keep = set(pass_fds)
1257 fds_to_keep.add(errpipe_write)
1258 self.pid = _posixsubprocess.fork_exec(
1259 args, executable_list,
1260 close_fds, tuple(sorted(map(int, fds_to_keep))),
1261 cwd, env_list,
1262 p2cread, p2cwrite, c2pread, c2pwrite,
1263 errread, errwrite,
1264 errpipe_read, errpipe_write,
1265 restore_signals, start_new_session, preexec_fn)
1266 self._child_created = True
1267 finally:
1268 # be sure the FD is closed no matter what
1269 os.close(errpipe_write)
1270
1271 # self._devnull is not always defined.
1272 devnull_fd = getattr(self, '_devnull', None)
1273 if p2cread != -1 and p2cwrite != -1 and p2cread != devnull_fd:
1274 os.close(p2cread)
1275 if c2pwrite != -1 and c2pread != -1 and c2pwrite != devnull_fd:
1276 os.close(c2pwrite)
1277 if errwrite != -1 and errread != -1 and errwrite != devnull_fd:
1278 os.close(errwrite)
1279 if devnull_fd is not None:
1280 os.close(devnull_fd)
1281 # Prevent a double close of these fds from __init__ on error.
1282 self._closed_child_pipe_fds = True
1283
1284 # Wait for exec to fail or succeed; possibly raising an
1285 # exception (limited in size)
1286 errpipe_data = bytearray()
1287 while True:
1288 part = os.read(errpipe_read, 50000)
1289 errpipe_data += part
1290 if not part or len(errpipe_data) > 50000:
1291 break
1292 finally:
1293 # be sure the FD is closed no matter what
1294 os.close(errpipe_read)
1295
1296 if errpipe_data:
1297 try:
1298 pid, sts = os.waitpid(self.pid, 0)
1299 if pid == self.pid:
1300 self._handle_exitstatus(sts)
1301 else:
1302 self.returncode = sys.maxsize
1303 except ChildProcessError:
1304 pass
1305
1306 try:
1307 exception_name, hex_errno, err_msg = (
1308 errpipe_data.split(b':', 2))
1309 # The encoding here should match the encoding
1310 # written in by the subprocess implementations
1311 # like _posixsubprocess
1312 err_msg = err_msg.decode()
1313 except ValueError:
1314 exception_name = b'SubprocessError'
1315 hex_errno = b'0'
1316 err_msg = 'Bad exception data from child: {!r}'.format(
1317 bytes(errpipe_data))
1318 child_exception_type = getattr(
1319 builtins, exception_name.decode('ascii'),
1320 SubprocessError)
1321 if issubclass(child_exception_type, OSError) and hex_errno:
1322 errno_num = int(hex_errno, 16)
1323 child_exec_never_called = (err_msg == "noexec")
1324 if child_exec_never_called:
1325 err_msg = ""
1326 # The error must be from chdir(cwd).
1327 err_filename = cwd
1328 else:
1329 err_filename = orig_executable
1330 if errno_num != 0:
1331 err_msg = os.strerror(errno_num)
1332 if errno_num == errno.ENOENT:
1333 err_msg += ': ' + repr(err_filename)
1334 raise child_exception_type(errno_num, err_msg, err_filename)
1335 raise child_exception_type(err_msg)
1336
1337
1338 def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
1339 _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
1340 _WEXITSTATUS=os.WEXITSTATUS, _WIFSTOPPED=os.WIFSTOPPED,
1341 _WSTOPSIG=os.WSTOPSIG):
1342 """All callers to this function MUST hold self._waitpid_lock."""
1343 # This method is called (indirectly) by __del__, so it cannot
1344 # refer to anything outside of its local scope.
1345 if _WIFSIGNALED(sts):
1346 self.returncode = -_WTERMSIG(sts)
1347 elif _WIFEXITED(sts):
1348 self.returncode = _WEXITSTATUS(sts)
1349 elif _WIFSTOPPED(sts):
1350 self.returncode = -_WSTOPSIG(sts)
1351 else:
1352 # Should never happen
1353 raise SubprocessError("Unknown child exit status!")
1354
1355
1356 def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
1357 _WNOHANG=os.WNOHANG, _ECHILD=errno.ECHILD):
1358 """Check if child process has terminated. Returns returncode
1359 attribute.
1360
1361 This method is called by __del__, so it cannot reference anything
1362 outside of the local scope (nor can any methods it calls).
1363
1364 """
1365 if self.returncode is None:
1366 if not self._waitpid_lock.acquire(False):
1367 # Something else is busy calling waitpid. Don't allow two
1368 # at once. We know nothing yet.
1369 return None
1370 try:
1371 if self.returncode is not None:
1372 return self.returncode # Another thread waited.
1373 pid, sts = _waitpid(self.pid, _WNOHANG)
1374 if pid == self.pid:
1375 self._handle_exitstatus(sts)
1376 except OSError as e:
1377 if _deadstate is not None:
1378 self.returncode = _deadstate
1379 elif e.errno == _ECHILD:
1380 # This happens if SIGCLD is set to be ignored or
1381 # waiting for child processes has otherwise been
1382 # disabled for our process. This child is dead, we
1383 # can't get the status.
1384 # http://bugs.python.org/issue15756
1385 self.returncode = 0
1386 finally:
1387 self._waitpid_lock.release()
1388 return self.returncode
1389
1390
1391 def _try_wait(self, wait_flags):
1392 """All callers to this function MUST hold self._waitpid_lock."""
1393 try:
1394 pid, sts = os.waitpid(self.pid, wait_flags)
1395 except ChildProcessError:
1396 # This happens if SIGCLD is set to be ignored or waiting
1397 # for child processes has otherwise been disabled for our
1398 # process. This child is dead, we can't get the status.
1399 pid = self.pid
1400 sts = 0
1401 return pid, sts
1402
1403 def wait(self, timeout=None, endtime=None):
1404 """Wait for child process to terminate. Returns returncode
1405 attribute."""
1406 if self.returncode is not None:
1407 return self.returncode
1408
1409 if endtime is not None:
1410 warnings.warn(
1411 "'endtime' argument is deprecated; use 'timeout'.",
1412 DeprecationWarning,
1413 stacklevel=2)
1414 if endtime is not None or timeout is not None:
1415 if endtime is None:
1416 endtime = _time() + timeout
1417 elif timeout is None:
1418 timeout = self._remaining_time(endtime)
1419
1420 if endtime is not None:
1421 # Enter a busy loop if we have a timeout. This busy loop was
1422 # cribbed from Lib/threading.py in Thread.wait() at r71065.
1423 delay = 0.0005 # 500 us -> initial delay of 1 ms
1424 while True:
1425 if self._waitpid_lock.acquire(False):
1426 try:
1427 if self.returncode is not None:
1428 break # Another thread waited.
1429 pid, sts = self._try_wait(os.WNOHANG)
1430 assert pid == self.pid or pid == 0
1431 if pid == self.pid:
1432 self._handle_exitstatus(sts)
1433 break
1434 finally:
1435 self._waitpid_lock.release()
1436 remaining = self._remaining_time(endtime)
1437 if remaining <= 0:
1438 raise TimeoutExpired(self.args, timeout)
1439 delay = min(delay * 2, remaining, .05)
1440 time.sleep(delay)
1441 else:
1442 while self.returncode is None:
1443 with self._waitpid_lock:
1444 if self.returncode is not None:
1445 break # Another thread waited.
1446 pid, sts = self._try_wait(0)
1447 # Check the pid and loop as waitpid has been known to
1448 # return 0 even without WNOHANG in odd situations.
1449 # http://bugs.python.org/issue14396.
1450 if pid == self.pid:
1451 self._handle_exitstatus(sts)
1452 return self.returncode
1453
1454
1455 def _communicate(self, input, endtime, orig_timeout):
1456 if self.stdin and not self._communication_started:
1457 # Flush stdio buffer. This might block, if the user has
1458 # been writing to .stdin in an uncontrolled fashion.
1459 try:
1460 self.stdin.flush()
1461 except BrokenPipeError:
1462 pass # communicate() must ignore BrokenPipeError.
1463 if not input:
1464 try:
1465 self.stdin.close()
1466 except BrokenPipeError:
1467 pass # communicate() must ignore BrokenPipeError.
1468
1469 stdout = None
1470 stderr = None
1471
1472 # Only create this mapping if we haven't already.
1473 if not self._communication_started:
1474 self._fileobj2output = {}
1475 if self.stdout:
1476 self._fileobj2output[self.stdout] = []
1477 if self.stderr:
1478 self._fileobj2output[self.stderr] = []
1479
1480 if self.stdout:
1481 stdout = self._fileobj2output[self.stdout]
1482 if self.stderr:
1483 stderr = self._fileobj2output[self.stderr]
1484
1485 self._save_input(input)
1486
1487 if self._input:
1488 input_view = memoryview(self._input)
1489
1490 with _PopenSelector() as selector:
1491 if self.stdin and input:
1492 selector.register(self.stdin, selectors.EVENT_WRITE)
1493 if self.stdout:
1494 selector.register(self.stdout, selectors.EVENT_READ)
1495 if self.stderr:
1496 selector.register(self.stderr, selectors.EVENT_READ)
1497
1498 while selector.get_map():
1499 timeout = self._remaining_time(endtime)
1500 if timeout is not None and timeout < 0:
1501 raise TimeoutExpired(self.args, orig_timeout)
1502
1503 ready = selector.select(timeout)
1504 self._check_timeout(endtime, orig_timeout)
1505
1506 # XXX Rewrite these to use non-blocking I/O on the file
1507 # objects; they are no longer using C stdio!
1508
1509 for key, events in ready:
1510 if key.fileobj is self.stdin:
1511 chunk = input_view[self._input_offset :
1512 self._input_offset + _PIPE_BUF]
1513 try:
1514 self._input_offset += os.write(key.fd, chunk)
1515 except BrokenPipeError:
1516 selector.unregister(key.fileobj)
1517 key.fileobj.close()
1518 else:
1519 if self._input_offset >= len(self._input):
1520 selector.unregister(key.fileobj)
1521 key.fileobj.close()
1522 elif key.fileobj in (self.stdout, self.stderr):
1523 data = os.read(key.fd, 32768)
1524 if not data:
1525 selector.unregister(key.fileobj)
1526 key.fileobj.close()
1527 self._fileobj2output[key.fileobj].append(data)
1528
1529 self.wait(timeout=self._remaining_time(endtime))
1530
1531 # All data exchanged. Translate lists into strings.
1532 if stdout is not None:
1533 stdout = b''.join(stdout)
1534 if stderr is not None:
1535 stderr = b''.join(stderr)
1536
1537 # Translate newlines, if requested.
1538 # This also turns bytes into strings.
1539 if self.encoding or self.errors or self.universal_newlines:
1540 if stdout is not None:
1541 stdout = self._translate_newlines(stdout,
1542 self.stdout.encoding,
1543 self.stdout.errors)
1544 if stderr is not None:
1545 stderr = self._translate_newlines(stderr,
1546 self.stderr.encoding,
1547 self.stderr.errors)
1548
1549 return (stdout, stderr)
1550
1551
1552 def _save_input(self, input):
1553 # This method is called from the _communicate_with_*() methods
1554 # so that if we time out while communicating, we can continue
1555 # sending input if we retry.
1556 if self.stdin and self._input is None:
1557 self._input_offset = 0
1558 self._input = input
1559 if input is not None and (
1560 self.encoding or self.errors or self.universal_newlines):
1561 self._input = self._input.encode(self.stdin.encoding,
1562 self.stdin.errors)
1563
1564
1565 def send_signal(self, sig):
1566 """Send a signal to the process."""
1567 # Skip signalling a process that we know has already died.
1568 if self.returncode is None:
1569 os.kill(self.pid, sig)
1570
1571 def terminate(self):
1572 """Terminate the process with SIGTERM
1573 """
1574 self.send_signal(signal.SIGTERM)
1575
1576 def kill(self):
1577 """Kill the process with SIGKILL
1578 """
1579 self.send_signal(signal.SIGKILL)