diff options
author | Gregory Szorc <gregory.szorc@gmail.com> | 2012-08-19 21:17:46 +0000 |
---|---|---|
committer | Gregory Szorc <gregory.szorc@gmail.com> | 2012-08-19 21:17:46 +0000 |
commit | b15b15c7f641fd7a71ad94ffe41b162f03d429b0 (patch) | |
tree | cf2a52c1cf32a2ee989dca28b02ce3022e7f4490 /bindings/python | |
parent | 58308d8469cfe3395ede90423e2e80e769f336ce (diff) |
[clang.py] Add CachedProperty decorator
It isn't used anywhere yet.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162190 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings/python')
-rw-r--r-- | bindings/python/clang/cindex.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index ee85585f89..d571b2ade9 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -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.""" |