diff options
author | Kali Kaneko <kali@leap.se> | 2013-12-18 22:31:23 -0400 |
---|---|---|
committer | Kali Kaneko <kali@leap.se> | 2013-12-18 22:31:23 -0400 |
commit | 4879753edfc7380e1e96d882716b60e981a4de53 (patch) | |
tree | ab32ecf8aa3726de1fda01773d1ce51a448b4160 /src/leap/common | |
parent | f498b0eca553176f69f7be4eb42206f579b9be8b (diff) |
Fix memoize decorator: raise instead of storing None
With this fix, we will re-raise an exception that happens
while evaluating the callable, instead of storing None as value.
Diffstat (limited to 'src/leap/common')
-rw-r--r-- | src/leap/common/decorators.py | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/leap/common/decorators.py b/src/leap/common/decorators.py index ec6171a..e708fc4 100644 --- a/src/leap/common/decorators.py +++ b/src/leap/common/decorators.py @@ -56,6 +56,15 @@ class _memoized(object): :tyoe args: tuple :type kwargs: dict """ + def ret_or_raise(value): + """ + Returns the value except if it is an exception, + in which case it's raised. + """ + if isinstance(value, Exception): + raise value + return value + if self.is_method: # forget about `self` as key key_args = args[1:] @@ -74,15 +83,16 @@ class _memoized(object): if key in self.cache: logger.debug("Got value from cache...") - return self.cache[key] + value = self.cache[key] + return ret_or_raise(value) else: try: value = self.func(*args, **kwargs) except Exception as exc: logger.error("Exception while calling function: %r" % (exc,)) - value = None + value = exc self.cache[key] = value - return value + return ret_or_raise(value) def __repr__(self): """ |