diff options
author | Mike Frysinger <vapier@google.com> | 2023-05-26 22:50:12 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2023-06-02 11:03:27 -0400 |
commit | fed77e7ee9e3efe59f77db0d50d31fe8b14a3012 (patch) | |
tree | 703fd33edc4a8e80968bd8b299969ba1d521f68f | |
parent | 4ba994ee8a805ad9516a9d4d1ea8aebba6dc09e5 (diff) | |
download | repohooks-fed77e7ee9e3efe59f77db0d50d31fe8b14a3012.tar.gz |
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
-rw-r--r-- | rh/utils.py | 23 | ||||
-rwxr-xr-x | rh/utils_unittest.py | 16 |
2 files changed, 32 insertions, 7 deletions
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 @@ class CalledProcessError(subprocess.CalledProcessError): """ 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 @@ class CalledProcessErrorTests(unittest.TestCase): 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.""" |