changeset 3475:f134d125fb67 beta

refactored lazy properties little bit for ThreadecLocal to work better
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 06 Mar 2013 13:36:01 +0100
parents 2e703c3ef0b8
children 258e0353ca51
files rhodecode/lib/vcs/utils/lazy.py
diffstat 1 files changed, 21 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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