Permalink
Browse files

Make sure backtraces from process errors show stderr and stdout

  • Loading branch information...
dabrahams committed Jan 2, 2013
1 parent 1169431 commit b1a09ea8b79c633102646487535cc4ace8b1486e
Showing with 16 additions and 4 deletions.
  1. +16 −4 util/quiet_process.py
View
@@ -1,4 +1,4 @@
-from subprocess import Popen as _Popen, PIPE, STDOUT, call as _call, check_call as _check_call, CalledProcessError
+from subprocess import Popen as _Popen, PIPE, STDOUT, call as _call, check_call as _check_call, CalledProcessError as _CalledProcessError
import sys
def _quiet_args(kwargs):
@@ -21,17 +21,29 @@ def __del__(self):
def call(*args, **kw):
return _call(*args, **_quiet_args(kw))
+class CalledProcessError(_CalledProcessError):
+ def __init__(self, returncode, cmd, output=None, stderr=None):
+ super(CalledProcessError,self).__init__(returncode, cmd, output)
+ self.stderr = stderr
+ self.stdout = output
+
+ def __str__(self):
+ return "Command '%s' returned non-zero exit status %d" % (
+ self.cmd, self.returncode) + '''
+### stderr:
+''' + (self.stderr if self.stderr else '') + '''
+### stdout:
+''' + (self.stdout if self.stdout else '')
+
def check_call(*popenargs, **kwargs):
# intentionally don't return anything. _check_call throws if the
# result would be nonzero, so the return value is meaningless
p = Popen(*popenargs, **kwargs)
retcode = p.wait()
if retcode != 0:
- sys.stdout.write(p.stdout.read())
- sys.stderr.write(p.stderr.read())
cmd = kwargs.get("args") or popenargs[0]
- raise CalledProcessError(retcode, cmd)
+ raise CalledProcessError(retcode, cmd, output = p.stdout.read(), stderr = p.stderr.read())
try:
from subprocess import check_output as _check_output

0 comments on commit b1a09ea

Please sign in to comment.