diff options
author | Bo Bayles <bbayles@gmail.com> | 2018-01-30 06:40:39 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2018-01-30 06:40:39 (GMT) |
commit | ce0f33d04528fcafc673a8707871f8430d8f7ce8 (patch) | |
tree | de60362ad2f3c1bb8af239c1926a7f907887e8c1 /Lib/subprocess.py | |
parent | 95441809ef77a8df5e14601ade6c054ef7114c02 (diff) | |
download | cpython-ce0f33d04528fcafc673a8707871f8430d8f7ce8.zip cpython-ce0f33d04528fcafc673a8707871f8430d8f7ce8.tar.gz cpython-ce0f33d04528fcafc673a8707871f8430d8f7ce8.tar.bz2 |
bpo-32102 Add "capture_output=True" to subprocess.run (GH-5149)
Add "capture_output=True" option to subprocess.run, this is equivalent to
setting stdout=PIPE, stderr=PIPE but is much more readable.
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index f69159e..93635ee 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -409,7 +409,8 @@ class CompletedProcess(object): self.stderr) -def run(*popenargs, input=None, timeout=None, check=False, **kwargs): +def run(*popenargs, + input=None, capture_output=False, timeout=None, check=False, **kwargs): """Run command with arguments and return a CompletedProcess instance. The returned instance will have attributes args, returncode, stdout and @@ -442,6 +443,13 @@ def run(*popenargs, input=None, timeout=None, check=False, **kwargs): raise ValueError('stdin and input arguments may not both be used.') kwargs['stdin'] = PIPE + if capture_output: + if ('stdout' in kwargs) or ('stderr' in kwargs): + raise ValueError('stdout and stderr arguments may not be used ' + 'with capture_output.') + kwargs['stdout'] = PIPE + kwargs['stderr'] = PIPE + with Popen(*popenargs, **kwargs) as process: try: stdout, stderr = process.communicate(input, timeout=timeout) |