rename the new check_call_output to check_output. its less ugly.
This commit is contained in:
parent
1743201364
commit
26576801a6
|
@ -149,7 +149,7 @@ This module also defines two shortcut functions:
|
||||||
.. versionadded:: 2.5
|
.. versionadded:: 2.5
|
||||||
|
|
||||||
|
|
||||||
.. function:: check_call_output(*popenargs, **kwargs)
|
.. function:: check_output(*popenargs, **kwargs)
|
||||||
|
|
||||||
Run command with arguments and return its output as a byte string.
|
Run command with arguments and return its output as a byte string.
|
||||||
|
|
||||||
|
@ -159,13 +159,13 @@ This module also defines two shortcut functions:
|
||||||
|
|
||||||
The arguments are the same as for the Popen constructor. Example:
|
The arguments are the same as for the Popen constructor. Example:
|
||||||
|
|
||||||
>>> subprocess.check_call_output(["ls", "-l", "/dev/null"])
|
>>> subprocess.check_output(["ls", "-l", "/dev/null"])
|
||||||
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
|
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
|
||||||
|
|
||||||
The stdout argument is not allowed as it is used internally.
|
The stdout argument is not allowed as it is used internally.
|
||||||
To capture standard error in the result, use stderr=subprocess.STDOUT.
|
To capture standard error in the result, use stderr=subprocess.STDOUT.
|
||||||
|
|
||||||
>>> subprocess.check_call_output(
|
>>> subprocess.check_output(
|
||||||
["/bin/sh", "-c", "ls non_existant_file ; exit 0"],
|
["/bin/sh", "-c", "ls non_existant_file ; exit 0"],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
'ls: non_existant_file: No such file or directory\n'
|
'ls: non_existant_file: No such file or directory\n'
|
||||||
|
|
|
@ -127,7 +127,7 @@ check_call(*popenargs, **kwargs):
|
||||||
|
|
||||||
check_call(["ls", "-l"])
|
check_call(["ls", "-l"])
|
||||||
|
|
||||||
check_call_output(*popenargs, **kwargs):
|
check_output(*popenargs, **kwargs):
|
||||||
Run command with arguments and return its output as a byte string.
|
Run command with arguments and return its output as a byte string.
|
||||||
|
|
||||||
If the exit code was non-zero it raises a CalledProcessError. The
|
If the exit code was non-zero it raises a CalledProcessError. The
|
||||||
|
@ -136,7 +136,7 @@ check_call_output(*popenargs, **kwargs):
|
||||||
|
|
||||||
The arguments are the same as for the Popen constructor. Example:
|
The arguments are the same as for the Popen constructor. Example:
|
||||||
|
|
||||||
output = subprocess.check_call_output(["ls", "-l", "/dev/null"])
|
output = subprocess.check_output(["ls", "-l", "/dev/null"])
|
||||||
|
|
||||||
Exceptions
|
Exceptions
|
||||||
----------
|
----------
|
||||||
|
@ -152,7 +152,7 @@ should prepare for OSErrors.
|
||||||
|
|
||||||
A ValueError will be raised if Popen is called with invalid arguments.
|
A ValueError will be raised if Popen is called with invalid arguments.
|
||||||
|
|
||||||
check_call() and check_call_output() will raise CalledProcessError, if the
|
check_call() and check_output() will raise CalledProcessError, if the
|
||||||
called process returns a non-zero return code.
|
called process returns a non-zero return code.
|
||||||
|
|
||||||
|
|
||||||
|
@ -373,9 +373,9 @@ import signal
|
||||||
# Exception classes used by this module.
|
# Exception classes used by this module.
|
||||||
class CalledProcessError(Exception):
|
class CalledProcessError(Exception):
|
||||||
"""This exception is raised when a process run by check_call() or
|
"""This exception is raised when a process run by check_call() or
|
||||||
check_call_output() returns a non-zero exit status.
|
check_output() returns a non-zero exit status.
|
||||||
The exit status will be stored in the returncode attribute;
|
The exit status will be stored in the returncode attribute;
|
||||||
check_call_output() will also store the output in the output attribute.
|
check_output() will also store the output in the output attribute.
|
||||||
"""
|
"""
|
||||||
def __init__(self, returncode, cmd, output=None):
|
def __init__(self, returncode, cmd, output=None):
|
||||||
self.returncode = returncode
|
self.returncode = returncode
|
||||||
|
@ -418,7 +418,7 @@ else:
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call",
|
__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call",
|
||||||
"check_call_output", "CalledProcessError"]
|
"check_output", "CalledProcessError"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
MAXFD = os.sysconf("SC_OPEN_MAX")
|
MAXFD = os.sysconf("SC_OPEN_MAX")
|
||||||
|
@ -478,7 +478,7 @@ def check_call(*popenargs, **kwargs):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def check_call_output(*popenargs, **kwargs):
|
def check_output(*popenargs, **kwargs):
|
||||||
"""Run command with arguments and return its output as a byte string.
|
"""Run command with arguments and return its output as a byte string.
|
||||||
|
|
||||||
If the exit code was non-zero it raises a CalledProcessError. The
|
If the exit code was non-zero it raises a CalledProcessError. The
|
||||||
|
@ -487,13 +487,13 @@ def check_call_output(*popenargs, **kwargs):
|
||||||
|
|
||||||
The arguments are the same as for the Popen constructor. Example:
|
The arguments are the same as for the Popen constructor. Example:
|
||||||
|
|
||||||
>>> check_call_output(["ls", "-l", "/dev/null"])
|
>>> check_output(["ls", "-l", "/dev/null"])
|
||||||
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
|
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
|
||||||
|
|
||||||
The stdout argument is not allowed as it is used internally.
|
The stdout argument is not allowed as it is used internally.
|
||||||
To capture standard error in the result, use stderr=subprocess.STDOUT.
|
To capture standard error in the result, use stderr=subprocess.STDOUT.
|
||||||
|
|
||||||
>>> check_call_output(["/bin/sh", "-c",
|
>>> check_output(["/bin/sh", "-c",
|
||||||
"ls -l non_existant_file ; exit 0"],
|
"ls -l non_existant_file ; exit 0"],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
'ls: non_existant_file: No such file or directory\n'
|
'ls: non_existant_file: No such file or directory\n'
|
||||||
|
|
|
@ -72,33 +72,33 @@ class ProcessTestCase(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
self.fail("Expected CalledProcessError")
|
self.fail("Expected CalledProcessError")
|
||||||
|
|
||||||
def test_check_call_output(self):
|
def test_check_output(self):
|
||||||
# check_call_output() function with zero return code
|
# check_output() function with zero return code
|
||||||
output = subprocess.check_call_output(
|
output = subprocess.check_output(
|
||||||
[sys.executable, "-c", "print 'BDFL'"])
|
[sys.executable, "-c", "print 'BDFL'"])
|
||||||
self.assertTrue('BDFL' in output)
|
self.assertTrue('BDFL' in output)
|
||||||
|
|
||||||
def test_check_call_output_nonzero(self):
|
def test_check_output_nonzero(self):
|
||||||
# check_call() function with non-zero return code
|
# check_call() function with non-zero return code
|
||||||
try:
|
try:
|
||||||
subprocess.check_call_output(
|
subprocess.check_output(
|
||||||
[sys.executable, "-c", "import sys; sys.exit(5)"])
|
[sys.executable, "-c", "import sys; sys.exit(5)"])
|
||||||
except subprocess.CalledProcessError, e:
|
except subprocess.CalledProcessError, e:
|
||||||
self.assertEqual(e.returncode, 5)
|
self.assertEqual(e.returncode, 5)
|
||||||
else:
|
else:
|
||||||
self.fail("Expected CalledProcessError")
|
self.fail("Expected CalledProcessError")
|
||||||
|
|
||||||
def test_check_call_output_stderr(self):
|
def test_check_output_stderr(self):
|
||||||
# check_call_output() function stderr redirected to stdout
|
# check_output() function stderr redirected to stdout
|
||||||
output = subprocess.check_call_output(
|
output = subprocess.check_output(
|
||||||
[sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
|
[sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
self.assertTrue('BDFL' in output)
|
self.assertTrue('BDFL' in output)
|
||||||
|
|
||||||
def test_check_call_output_stdout_arg(self):
|
def test_check_output_stdout_arg(self):
|
||||||
# check_call_output() function stderr redirected to stdout
|
# check_output() function stderr redirected to stdout
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_call_output(
|
output = subprocess.check_output(
|
||||||
[sys.executable, "-c", "print 'will not be run'"],
|
[sys.executable, "-c", "print 'will not be run'"],
|
||||||
stdout=sys.stdout)
|
stdout=sys.stdout)
|
||||||
except ValueError, e:
|
except ValueError, e:
|
||||||
|
|
|
@ -60,7 +60,7 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
- Added the subprocess.check_call_output() convenience function to get output
|
- Added the subprocess.check_output() convenience function to get output
|
||||||
from a subprocess on success or raise an exception on error.
|
from a subprocess on success or raise an exception on error.
|
||||||
|
|
||||||
- Issue #1055234: cgi.parse_header(): Fixed parsing of header parameters to
|
- Issue #1055234: cgi.parse_header(): Fixed parsing of header parameters to
|
||||||
|
|
Loading…
Reference in New Issue