diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2011-04-11 09:04:27 +0200 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2011-04-11 09:04:27 +0200 |
commit | 9044fabeabf34e261ede5c0b918b39a8f65cbb2f (patch) | |
tree | a11d6b130608844dbc33264704a1942dd33146a6 | |
parent | 16b24ec7837e73dce47b6b4e201b81d6a510ade7 (diff) | |
download | logilab-common-9044fabeabf34e261ede5c0b918b39a8f65cbb2f.tar.gz |
test and fix clear_cache to work on cached **properties**. remove debug print
-rw-r--r-- | decorators.py | 7 | ||||
-rw-r--r-- | test/unittest_decorators.py | 13 |
2 files changed, 18 insertions, 2 deletions
diff --git a/decorators.py b/decorators.py index 336af9d..36ebe14 100644 --- a/decorators.py +++ b/decorators.py @@ -38,7 +38,6 @@ class cached_decorator(object): cache = _SingleValueCache(callableobj, self.cacheattr) elif self.keyarg: cache = _MultiValuesKeyArgCache(callableobj, self.keyarg, self.cacheattr) - print 'hop' else: cache = _MultiValuesCache(callableobj, self.cacheattr) return cache.closure() @@ -119,7 +118,11 @@ def cached(callableobj=None, keyarg=None, **kwargs): def clear_cache(obj, funcname): """Function to clear a cache handled by the cached decorator.""" - getattr(obj, funcname).clear(obj) + cls = obj.__class__ + member = getattr(cls, funcname) + if isinstance(member, property): + member = member.fget + member.clear(obj) def copy_cache(obj, funcname, cacheobj): """Copy cache for <funcname> from cacheobj to obj.""" diff --git a/test/unittest_decorators.py b/test/unittest_decorators.py index b3321fc..1f92d55 100644 --- a/test/unittest_decorators.py +++ b/test/unittest_decorators.py @@ -102,5 +102,18 @@ class DecoratorsTC(TestCase): clear_cache(foo, 'foo') self.assertFalse(hasattr(foo, '_foo')) + def test_cached_property(self): + class Foo(object): + @property + @cached(cacheattr=u'_foo') + def foo(self): + """ what's up doc ? """ + foo = Foo() + foo.foo + self.assertEqual(foo._foo, None) + clear_cache(foo, 'foo') + self.assertFalse(hasattr(foo, '_foo')) + + if __name__ == '__main__': unittest_main() |