cpython: f2d13349ea5d (original) (raw)
Mercurial > cpython
changeset 101638:f2d13349ea5d
Issue #27167: Clarify the subprocess.CalledProcessError error message text when the child process died due to a signal. [#27167]
Gregory P. Smith greg@krypto.org [Google Inc.] | |
---|---|
date | Fri, 03 Jun 2016 06:14:06 +0000 |
parents | 015b86646d8e |
children | 1022d09d11e5 |
files | Lib/subprocess.py Lib/test/test_subprocess.py Misc/NEWS |
diffstat | 3 files changed, 37 insertions(+), 4 deletions(-)[+] [-] Lib/subprocess.py 19 Lib/test/test_subprocess.py 19 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -372,9 +372,11 @@ class SubprocessError(Exception): pass class CalledProcessError(SubprocessError):
- """This exception is raised when a process run by check_call() or
- 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, negative
- if it represents a signal number.
+ check_output() will also store the output in the output attribute. """ def init(self, returncode, cmd, output=None, stderr=None): @@ -384,7 +386,16 @@ class CalledProcessError(SubprocessError self.stderr = stderr def str(self):
return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)[](#l1.22)
if self.returncode and self.returncode < 0:[](#l1.23)
try:[](#l1.24)
return "Command '%s' died with %r." % ([](#l1.25)
self.cmd, signal.Signals(-self.returncode))[](#l1.26)
except ValueError:[](#l1.27)
return "Command '%s' died with unknown signal %d." % ([](#l1.28)
self.cmd, -self.returncode)[](#l1.29)
else:[](#l1.30)
return "Command '%s' returned non-zero exit status %d." % ([](#l1.31)
self.cmd, self.returncode)[](#l1.32)
--- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1427,6 +1427,25 @@ class POSIXProcessTestCase(BaseTestCase) p.wait() self.assertEqual(-p.returncode, signal.SIGABRT)
- def test_CalledProcessError_str_signal(self):
err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")[](#l2.8)
error_string = str(err)[](#l2.9)
# We're relying on the repr() of the signal.Signals intenum to provide[](#l2.10)
# the word signal, the signal name and the numeric value.[](#l2.11)
self.assertIn("signal", error_string.lower())[](#l2.12)
self.assertIn("SIGABRT", error_string)[](#l2.13)
self.assertIn(str(signal.SIGABRT), error_string)[](#l2.14)
- def test_CalledProcessError_str_unknown_signal(self):
err = subprocess.CalledProcessError(-9876543, "fake cmd")[](#l2.17)
error_string = str(err)[](#l2.18)
self.assertIn("unknown signal 9876543.", error_string)[](#l2.19)
- def test_CalledProcessError_str_non_zero(self):
err = subprocess.CalledProcessError(2, "fake cmd")[](#l2.22)
error_string = str(err)[](#l2.23)
self.assertIn("non-zero exit status 2.", error_string)[](#l2.24)
+ def test_preexec(self): # DISCLAIMER: Setting environment variables is not a good use # of a preexec_fn. This is merely a test.
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,9 @@ Core and Builtins Library ------- +- Issue #27167: Clarify the subprocess.CalledProcessError error message text