diff options
author | Jake Hunsaker <jhunsake@redhat.com> | 2019-10-23 14:24:59 -0400 |
---|---|---|
committer | Jake Hunsaker <jhunsake@redhat.com> | 2019-10-24 10:30:15 -0400 |
commit | 903958306830ea00ac47e3ea353eaa5d8abbacb6 (patch) | |
tree | 5c1772009f6d0204377117613930ba797a95a973 | |
parent | ca711063e27192c3021c8cb6eb46f3850dd02486 (diff) | |
download | sos-903958306830ea00ac47e3ea353eaa5d8abbacb6.tar.gz |
[Predicate] Override __bool__ to allow py3 evaluation
The check in `Plugin.test_predicate()` relies on a 'is not None' test,
which on py2 invokes a call to `SoSPredicate.__nonzero__()` which in
turns runs our evaluation of the predicate. On py3 however, this test is
an explicit check to see if the object is `NoneType`. As such,
`__nonzero__()` never runs and the predicate defaults to always
evaluating ad `True`. This effectively removed any gating for command
execution on py3.
By overriding `SoSPredicate.__bool__()` to wrap `__nonzero__()` we can
ensure that predicate evaluation is performed properly on both py2 and
py3 runtimes.
Closes: #1839
Resolves: #1840
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
-rw-r--r-- | sos/plugins/__init__.py | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index ed55ea38..7ed75a5c 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -203,6 +203,11 @@ class SoSPredicate(object): return ((self._eval_kmods() and self._eval_services() and self._eval_packages()) and not self.dry_run) + def __bool__(self): + # Py3 evaluation ends in a __bool__() call where py2 ends in a call + # to __nonzero__(). Wrap the latter here, to support both versions + return self.__nonzero__() + def __init__(self, owner, dry_run=False, kmods=[], services=[], packages=[], required={}): """Initialise a new SoSPredicate object. |