From: cfalcon <cf...@ce...> - 2016-01-18 16:46:01
|
addSafe and removeSafe methods of SafeEvaluator class do not modify the _originalSafeDict attribute used in resetmoSafe to restore the dict, so the modification of the safe_dict are not permanent. Add a flag, "permanent" by default False, in those methods to do also the modification in the _originalSafeDict, when the flag is True. --- lib/taurus/core/util/safeeval.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/taurus/core/util/safeeval.py b/lib/taurus/core/util/safeeval.py index 77cd312..ee9d5f6 100644 --- a/lib/taurus/core/util/safeeval.py +++ b/lib/taurus/core/util/safeeval.py @@ -72,16 +72,23 @@ class SafeEvaluator: """safe eval""" return eval(expr, {"__builtins__":None}, self.safe_dict) - def addSafe(self,safedict): + def addSafe(self,safedict, permanent=False): """The values in safedict will be evaluable (whitelisted) The safedict is as follows: {"eval_name":object, ...}. The evaluator will interpret eval_name as object. """ self.safe_dict.update(safedict) - - def removeSafe(self, name): + if permanent: + self._originalSafeDict.update(safedict) + + def removeSafe(self, name, permanent=False): """Removes an object from the whitelist""" self.safe_dict.pop(name) - + if permanent: + try: + self._originalSafeDict.pop(name) + except KeyError: + pass + def resetSafe(self): """restores the safe dict with wich the evaluator was instantiated""" self.safe_dict = self._originalSafeDict.copy() -- 2.4.0 |