# HG changeset patch # User Marcin Kuzminski # Date 1362573361 -3600 # Node ID f134d125fb6789ded69d5bcafbf53167203333e0 # Parent 2e703c3ef0b8ea339c7656213ca357c43348eaf0 refactored lazy properties little bit for ThreadecLocal to work better diff -r 2e703c3ef0b8 -r f134d125fb67 rhodecode/lib/vcs/utils/lazy.py --- a/rhodecode/lib/vcs/utils/lazy.py Wed Mar 06 02:03:16 2013 +0100 +++ b/rhodecode/lib/vcs/utils/lazy.py Wed Mar 06 13:36:01 2013 +0100 @@ -1,3 +1,14 @@ +class _Missing(object): + + def __repr__(self): + return 'no value' + + def __reduce__(self): + return '_missing' + +_missing = _Missing() + + class LazyProperty(object): """ Decorator for easier creation of ``property`` from potentially expensive to @@ -24,8 +35,11 @@ def __get__(self, obj, klass=None): if obj is None: return self - result = obj.__dict__[self.__name__] = self._func(obj) - return result + value = obj.__dict__.get(self.__name__, _missing) + if value is _missing: + value = self._func(obj) + obj.__dict__[self.__name__] = value + return value import threading @@ -41,5 +55,8 @@ if not hasattr(obj, '__tl_dict__'): obj.__tl_dict__ = threading.local().__dict__ - result = obj.__tl_dict__[self.__name__] = self._func(obj) - return result + value = obj.__tl_dict__.get(self.__name__, _missing) + if value is _missing: + value = self._func(obj) + obj.__tl_dict__[self.__name__] = value + return value