The arguments shown above are merely the most common ones, described below
in :ref:`frequently-used-arguments` (hence the use of keyword-only notation
in the abbreviated signature). The full function signature is largely the
- same as that of the :class:`Popen` constructor - apart from *timeout*,
- *input* and *check*, all the arguments to this function are passed through to
- that interface.
+ same as that of the :class:`Popen` constructor - most of the arguments to
+ this function are passed through to that interface. (*timeout*, *input*,
+ *check*, and *capture_output* are not.)
- This does not capture stdout or stderr by default. To do so, pass
- :data:`PIPE` for the *stdout* and/or *stderr* arguments.
+ If *capture_output* is true, stdout and stderr will be captured.
+ When used, the internal :class:`Popen` object is automatically created with
+ ``stdout=PIPE`` and ``stderr=PIPE``. The *stdout* and *stderr* arguments may
+ not be used as well.
The *timeout* argument is passed to :meth:`Popen.communicate`. If the timeout
expires, the child process will be killed and waited for. The
...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
- >>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
+ >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
- stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')
+ stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
.. versionadded:: 3.5
.. versionchanged:: 3.7
- Added the *text* parameter, as a more understandable alias of *universal_newlines*
+ Added the *text* parameter, as a more understandable alias of *universal_newlines*.
+ Added the *capture_output* parameter.
.. class:: CompletedProcess
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
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)
env=newenv)
self.assertEqual(cp.returncode, 33)
+ def test_capture_output(self):
+ cp = self.run_python(("import sys;"
+ "sys.stdout.write('BDFL'); "
+ "sys.stderr.write('FLUFL')"),
+ capture_output=True)
+ self.assertIn(b'BDFL', cp.stdout)
+ self.assertIn(b'FLUFL', cp.stderr)
+
+ def test_stdout_with_capture_output_arg(self):
+ # run() refuses to accept 'stdout' with 'capture_output'
+ tf = tempfile.TemporaryFile()
+ self.addCleanup(tf.close)
+ with self.assertRaises(ValueError,
+ msg=("Expected ValueError when stdout and capture_output "
+ "args supplied.")) as c:
+ output = self.run_python("print('will not be run')",
+ capture_output=True, stdout=tf)
+ self.assertIn('stdout', c.exception.args[0])
+ self.assertIn('capture_output', c.exception.args[0])
+
+ def test_stderr_with_capture_output_arg(self):
+ # run() refuses to accept 'stderr' with 'capture_output'
+ tf = tempfile.TemporaryFile()
+ self.addCleanup(tf.close)
+ with self.assertRaises(ValueError,
+ msg=("Expected ValueError when stderr and capture_output "
+ "args supplied.")) as c:
+ output = self.run_python("print('will not be run')",
+ capture_output=True, stderr=tf)
+ self.assertIn('stderr', c.exception.args[0])
+ self.assertIn('capture_output', c.exception.args[0])
+
@unittest.skipIf(mswindows, "POSIX specific tests")
class POSIXProcessTestCase(BaseTestCase):
Anthony Baxter
Mike Bayer
Samuel L. Bayer
+Bo Bayles
Tommy Beadle
Donald Beaudry
David Beazley
--- /dev/null
+New argument ``capture_output`` for subprocess.run