utils: update CalledProcessError output python compat logic
We can leverage the parent's stderr setting now, but we can't get rid
of the output/stdout cleanup since Python 3.6 still does this.
Bug: None
Test: unittests
Change-Id: I52f2658d95aa4fdd08deabf68ad03599cb5b472a
diff --git a/rh/utils.py b/rh/utils.py
index 86ff691..157e31b 100644
--- a/rh/utils.py
+++ b/rh/utils.py
@@ -84,17 +84,26 @@
"""
def __init__(self, returncode, cmd, stdout=None, stderr=None, msg=None):
- super().__init__(returncode, cmd, stdout)
- # The parent class will set |output|, so delete it.
+ super().__init__(returncode, cmd, stdout, stderr=stderr)
+
+ # The parent class will set |output|, so delete it. If Python ever drops
+ # this output/stdout compat logic, we can drop this to match.
del self.output
- # TODO(vapier): When we're Python 3-only, delete this assignment as the
- # parent handles it for us.
- self.stdout = stdout
- # TODO(vapier): When we're Python 3-only, move stderr to the init above.
- self.stderr = stderr
+ self._stdout = stdout
+
self.msg = msg
@property
+ def stdout(self):
+ """Override parent's usage of .output"""
+ return self._stdout
+
+ @stdout.setter
+ def stdout(self, value):
+ """Override parent's usage of .output"""
+ self._stdout = value
+
+ @property
def cmdstr(self):
"""Return self.cmd as a well shell-quoted string for debugging."""
return '' if self.cmd is None else rh.shell.cmd_to_str(self.cmd)
diff --git a/rh/utils_unittest.py b/rh/utils_unittest.py
index 277abff..bf720a7 100755
--- a/rh/utils_unittest.py
+++ b/rh/utils_unittest.py
@@ -117,6 +117,22 @@
err = rh.utils.CalledProcessError(0, ['mycmd'])
self.assertNotEqual('', repr(err))
+ def test_output(self):
+ """Make sure .output is removed and .stdout works."""
+ e = rh.utils.CalledProcessError(
+ 0, ['true'], stdout='STDOUT', stderr='STDERR')
+ with self.assertRaises(AttributeError):
+ assert e.output is None
+ assert e.stdout == 'STDOUT'
+ assert e.stderr == 'STDERR'
+
+ e.stdout = 'STDout'
+ e.stderr = 'STDerr'
+ with self.assertRaises(AttributeError):
+ assert e.output is None
+ assert e.stdout == 'STDout'
+ assert e.stderr == 'STDerr'
+
class RunCommandTests(unittest.TestCase):
"""Verify behavior of run helper."""