]> granicus.if.org Git - clang/commitdiff
[clang.py] Add CachedProperty decorator
authorGregory Szorc <gregory.szorc@gmail.com>
Sun, 19 Aug 2012 21:17:46 +0000 (21:17 +0000)
committerGregory Szorc <gregory.szorc@gmail.com>
Sun, 19 Aug 2012 21:17:46 +0000 (21:17 +0000)
It isn't used anywhere yet.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162190 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/python/clang/cindex.py

index ee85585f89be2bdbb9c6965c3300ce4598cd7470..d571b2ade934f82b4191911a84ff5d50534954dc 100644 (file)
@@ -133,6 +133,31 @@ class TranslationUnitSaveError(Exception):
 
 ### Structures and Utility Classes ###
 
+class CachedProperty(object):
+    """Decorator that lazy-loads the value of a property.
+
+    The first time the property is accessed, the original property function is
+    executed. The value it returns is set as the new value of that instance's
+    property, replacing the original method.
+    """
+
+    def __init__(self, wrapped):
+        self.wrapped = wrapped
+        try:
+            self.__doc__ = wrapped.__doc__
+        except:
+            pass
+
+    def __get__(self, instance, instance_type=None):
+        if instance is None:
+            return self
+
+        value = self.wrapped(instance)
+        setattr(instance, self.wrapped.__name__, value)
+
+        return value
+
+
 class _CXString(Structure):
     """Helper for transforming CXString results."""