r"""Run command with arguments and return its output. If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute. The arguments are the same as for the Popen c
(*popenargs, timeout=None, **kwargs)
| 421 | |
| 422 | |
| 423 | def check_output(*popenargs, timeout=None, **kwargs): |
| 424 | r"""Run command with arguments and return its output. |
| 425 | |
| 426 | If the exit code was non-zero it raises a CalledProcessError. The |
| 427 | CalledProcessError object will have the return code in the returncode |
| 428 | attribute and output in the output attribute. |
| 429 | |
| 430 | The arguments are the same as for the Popen constructor. Example: |
| 431 | |
| 432 | >>> check_output(["ls", "-l", "/dev/null"]) |
| 433 | b'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
| 434 | |
| 435 | The stdout argument is not allowed as it is used internally. |
| 436 | To capture standard error in the result, use stderr=STDOUT. |
| 437 | |
| 438 | >>> check_output(["/bin/sh", "-c", |
| 439 | ... "ls -l non_existent_file ; exit 0"], |
| 440 | ... stderr=STDOUT) |
| 441 | b'ls: non_existent_file: No such file or directory\n' |
| 442 | |
| 443 | There is an additional optional argument, "input", allowing you to |
| 444 | pass a string to the subprocess's stdin. If you use this argument |
| 445 | you may not also use the Popen constructor's "stdin" argument, as |
| 446 | it too will be used internally. Example: |
| 447 | |
| 448 | >>> check_output(["sed", "-e", "s/foo/bar/"], |
| 449 | ... input=b"when in the course of fooman events\n") |
| 450 | b'when in the course of barman events\n' |
| 451 | |
| 452 | By default, all communication is in bytes, and therefore any "input" |
| 453 | should be bytes, and the return value will be bytes. If in text mode, |
| 454 | any "input" should be a string, and the return value will be a string |
| 455 | decoded according to locale encoding, or by "encoding" if set. Text mode |
| 456 | is triggered by setting any of text, encoding, errors or universal_newlines. |
| 457 | """ |
| 458 | for kw in ('stdout', 'check'): |
| 459 | if kw in kwargs: |
| 460 | raise ValueError(f'{kw} argument not allowed, it will be overridden.') |
| 461 | |
| 462 | if 'input' in kwargs and kwargs['input'] is None: |
| 463 | # Explicitly passing input=None was previously equivalent to passing an |
| 464 | # empty string. That is maintained here for backwards compatibility. |
| 465 | if kwargs.get('universal_newlines') or kwargs.get('text') or kwargs.get('encoding') \ |
| 466 | or kwargs.get('errors'): |
| 467 | empty = '' |
| 468 | else: |
| 469 | empty = b'' |
| 470 | kwargs['input'] = empty |
| 471 | |
| 472 | return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, |
| 473 | **kwargs).stdout |
| 474 | |
| 475 | |
| 476 | class CompletedProcess(object): |
no test coverage detected
searching dependent graphs…