From: Kali Kaneko Date: Thu, 19 Dec 2013 02:31:23 +0000 (-0400) Subject: Fix memoize decorator: raise instead of storing None X-Git-Tag: 0.3.7^2~3^2~1 X-Git-Url: https://leap.se/git/leap_pycommon.git/commitdiff_plain/4879753edfc7380e1e96d882716b60e981a4de53 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. --- 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): """