Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ def __init__(self, returncode, cmd, output=None, stderr=None):
def __str__(self):
if self.returncode and self.returncode < 0:
try:
return "Command '%s' died with %r." % (
return "Command %r died with %r." % (
self.cmd, signal.Signals(-self.returncode))
except ValueError:
return "Command '%s' died with unknown signal %d." % (
return "Command %r died with unknown signal %d." % (
self.cmd, -self.returncode)
else:
return "Command '%s' returned non-zero exit status %d." % (
return "Command %r returned non-zero exit status %d." % (
self.cmd, self.returncode)

@property
Expand Down
36 changes: 18 additions & 18 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2428,26 +2428,26 @@ def test_run_abort(self):
p.wait()
self.assertEqual(-p.returncode, signal.SIGABRT)

def test_CalledProcessError_str_signal(self):
def test_CalledProcessError_str(self):
# command string
err = subprocess.CalledProcessError(2, "fake cmd")
self.assertEqual(str(err), "Command 'fake cmd' returned non-zero exit status 2.")

# command string with a single-quote
err = subprocess.CalledProcessError(2, "fake ' cmd")
self.assertEqual(str(err), 'Command "fake \' cmd" returned non-zero exit status 2.')

# command list
err = subprocess.CalledProcessError(2, ["fake", "cmd"])
self.assertEqual(str(err), "Command ['fake', 'cmd'] returned non-zero exit status 2.")

# signal
err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd")
error_string = str(err)
# We're relying on the repr() of the signal.Signals intenum to provide
# the word signal, the signal name and the numeric value.
self.assertIn("signal", error_string.lower())
# We're not being specific about the signal name as some signals have
# multiple names and which name is revealed can vary.
self.assertIn("SIG", error_string)
self.assertIn(str(signal.SIGABRT), error_string)

def test_CalledProcessError_str_unknown_signal(self):
err = subprocess.CalledProcessError(-9876543, "fake cmd")
error_string = str(err)
self.assertIn("unknown signal 9876543.", error_string)
self.assertEqual(str(err), f"Command 'fake cmd' died with {signal.SIGABRT!r}.")

def test_CalledProcessError_str_non_zero(self):
err = subprocess.CalledProcessError(2, "fake cmd")
error_string = str(err)
self.assertIn("non-zero exit status 2.", error_string)
# unknown signal
err = subprocess.CalledProcessError(-9876543, "fake cmd")
self.assertEqual(str(err), "Command 'fake cmd' died with unknown signal 9876543.")

def test_preexec(self):
# DISCLAIMER: Setting environment variables is *not* a good use
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix command quoting in :exc:`subprocess.CalledProcessError`. Contributed by Benjy Wiener.
Loading