From 4879753edfc7380e1e96d882716b60e981a4de53 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Wed, 18 Dec 2013 22:31:23 -0400 Subject: 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. --- src/leap/common/decorators.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/leap') 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): """ -- cgit v1.2.3