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 | fbd4f4768c9a88d8cb482cddfe05e8a63a46812e (patch) | |
| tree | a9e1f5f7d0ac60f79bebf29662af879bc176f373 /clang/bindings/python | |
| parent | a3b7a802cc5312831dc73edb4d2c88d6ded412e3 (diff) | |
| download | bcm5719-llvm-fbd4f4768c9a88d8cb482cddfe05e8a63a46812e.tar.gz bcm5719-llvm-fbd4f4768c9a88d8cb482cddfe05e8a63a46812e.zip | |
[clang.py] Add CachedProperty decorator
It isn't used anywhere yet.
llvm-svn: 162190
Diffstat (limited to 'clang/bindings/python')
| -rw-r--r-- | clang/bindings/python/clang/cindex.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index ee85585f89b..d571b2ade93 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/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.""" |

