diff --git a/subprocess2.py b/subprocess2.py index 9ae485d2e..c40d1112c 100644 --- a/subprocess2.py +++ b/subprocess2.py @@ -48,7 +48,11 @@ class CalledProcessError(subprocess.CalledProcessError): ' '.join(self.cmd), self.returncode) if self.cwd: out += ' in ' + self.cwd - return '\n'.join(filter(None, (out, self.stdout, self.stderr))) + if self.stdout: + out += '\n' + self.stdout.decode('utf-8', 'ignore') + if self.stderr: + out += '\n' + self.stderr.decode('utf-8', 'ignore') + return out class CygwinRebaseError(CalledProcessError): diff --git a/tests/subprocess2_test.py b/tests/subprocess2_test.py index 4028b866a..017bebc3a 100755 --- a/tests/subprocess2_test.py +++ b/tests/subprocess2_test.py @@ -142,6 +142,15 @@ class SmokeTests(unittest.TestCase): with self.assertRaises(ValueError): subp.check_output(TEST_COMMAND, stdout=subp.PIPE) + def test_print_exception(self): + cmd = TEST_COMMAND + ['--fail', '--stdout'] + with self.assertRaises(subprocess2.CalledProcessError) as e: + subprocess2.check_output(cmd) + exception_str = str(e.exception) + self.assertIn(' '.join(cmd), exception_str) + self.assertIn(str(e.exception.returncode), exception_str) + self.assertIn(e.exception.stdout.decode('utf-8', 'ignore'), exception_str) + @_run_test() def test_check_output_throw_stdout(self, c, cmd, un, subp): with self.assertRaises(subp.CalledProcessError) as e: