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