From ce0f33d04528fcafc673a8707871f8430d8f7ce8 Mon Sep 17 00:00:00 2001 From: Bo Bayles Date: Tue, 30 Jan 2018 00:40:39 -0600 Subject: 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. --- Lib/subprocess.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'Lib/subprocess.py') diff --git a/Lib/subprocess.py b/Lib/subprocess.py index f69159e3aa..93635ee61f 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) -- cgit v1.2.1