Run command with arguments and return a CompletedProcess instance. The returned instance will have attributes args, returncode, stdout and stderr. By default, stdout and stderr are not captured, and those attributes will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture t
(*popenargs,
input=None, capture_output=False, timeout=None, check=False, **kwargs)
| 510 | |
| 511 | |
| 512 | def run(*popenargs, |
| 513 | input=None, capture_output=False, timeout=None, check=False, **kwargs): |
| 514 | """Run command with arguments and return a CompletedProcess instance. |
| 515 | |
| 516 | The returned instance will have attributes args, returncode, stdout and |
| 517 | stderr. By default, stdout and stderr are not captured, and those attributes |
| 518 | will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them, |
| 519 | or pass capture_output=True to capture both. |
| 520 | |
| 521 | If check is True and the exit code was non-zero, it raises a |
| 522 | CalledProcessError. The CalledProcessError object will have the return code |
| 523 | in the returncode attribute, and output & stderr attributes if those streams |
| 524 | were captured. |
| 525 | |
| 526 | If timeout (seconds) is given and the process takes too long, |
| 527 | a TimeoutExpired exception will be raised. |
| 528 | |
| 529 | There is an optional argument "input", allowing you to |
| 530 | pass bytes or a string to the subprocess's stdin. If you use this argument |
| 531 | you may not also use the Popen constructor's "stdin" argument, as |
| 532 | it will be used internally. |
| 533 | |
| 534 | By default, all communication is in bytes, and therefore any "input" should |
| 535 | be bytes, and the stdout and stderr will be bytes. If in text mode, any |
| 536 | "input" should be a string, and stdout and stderr will be strings decoded |
| 537 | according to locale encoding, or by "encoding" if set. Text mode is |
| 538 | triggered by setting any of text, encoding, errors or universal_newlines. |
| 539 | |
| 540 | The other arguments are the same as for the Popen constructor. |
| 541 | """ |
| 542 | if input is not None: |
| 543 | if kwargs.get('stdin') is not None: |
| 544 | raise ValueError('stdin and input arguments may not both be used.') |
| 545 | kwargs['stdin'] = PIPE |
| 546 | |
| 547 | if capture_output: |
| 548 | if kwargs.get('stdout') is not None or kwargs.get('stderr') is not None: |
| 549 | raise ValueError('stdout and stderr arguments may not be used ' |
| 550 | 'with capture_output.') |
| 551 | kwargs['stdout'] = PIPE |
| 552 | kwargs['stderr'] = PIPE |
| 553 | |
| 554 | with Popen(*popenargs, **kwargs) as process: |
| 555 | try: |
| 556 | stdout, stderr = process.communicate(input, timeout=timeout) |
| 557 | except TimeoutExpired as exc: |
| 558 | process.kill() |
| 559 | if _mswindows: |
| 560 | # Windows accumulates the output in a single blocking |
| 561 | # read() call run on child threads, with the timeout |
| 562 | # being done in a join() on those threads. communicate() |
| 563 | # _after_ kill() is required to collect that and add it |
| 564 | # to the exception. |
| 565 | exc.stdout, exc.stderr = process.communicate() |
| 566 | else: |
| 567 | # POSIX _communicate already populated the output so |
| 568 | # far into the TimeoutExpired exception. |
| 569 | process.wait() |
no test coverage detected