[cfe-commits] r162190 - /cfe/trunk/bindings/python/clang/cindex.py

Gregory Szorc gregory.szorc at gmail.com
Sun Aug 19 14:17:46 PDT 2012


Author: gps
Date: Sun Aug 19 16:17:46 2012
New Revision: 162190

URL: http://llvm.org/viewvc/llvm-project?rev=162190&view=rev
Log:
[clang.py] Add CachedProperty decorator

It isn't used anywhere yet.

Modified:
    cfe/trunk/bindings/python/clang/cindex.py

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=162190&r1=162189&r2=162190&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sun Aug 19 16:17:46 2012
@@ -133,6 +133,31 @@
 
 ### 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."""
 





More information about the cfe-commits mailing list