Commit 7ad1d093 authored by Edward Lemur's avatar Edward Lemur Committed by LUCI CQ

subprocess2: Fix CalledProcessError str() method on Python 3.

stdout and stderr are bytes in Python 3 and must be decoded before
printing the exception.

Bug: 1060409
Change-Id: Ib855f6104919734b7505aa5e978ebe394a1e9db6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2097595Reviewed-by: 's avatarJosip Sokcevic <sokcevic@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent 552ddbf3
......@@ -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):
......
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment