diff options
author | Aurelien Campeas <aurelien.campeas@logilab.fr> | 2010-04-16 16:10:24 +0200 |
---|---|---|
committer | Aurelien Campeas <aurelien.campeas@logilab.fr> | 2010-04-16 16:10:24 +0200 |
commit | 5a59bb0cd7df2de64ed0071921498768707b07fc (patch) | |
tree | bcdeb79c686bbc618730e3448f6ea098e93a21c3 | |
parent | 68d0bf4da2c30b522bec4ce00f53d612333449e4 (diff) | |
download | logilab-common-5a59bb0cd7df2de64ed0071921498768707b07fc.tar.gz |
[decorators] fix docstring handling and add a test #23389
-rw-r--r-- | decorators.py | 4 | ||||
-rw-r--r-- | test/unittest_decorators.py | 16 |
2 files changed, 18 insertions, 2 deletions
diff --git a/decorators.py b/decorators.py index 9e5e085..cb3070f 100644 --- a/decorators.py +++ b/decorators.py @@ -14,7 +14,6 @@ import sys, re def cached(callableobj, keyarg=None): """Simple decorator to cache result of method call.""" - #print callableobj, keyarg, callableobj.func_code.co_argcount if callableobj.func_code.co_argcount == 1 or keyarg == 0: def cache_wrapper1(self, *args): @@ -27,6 +26,7 @@ def cached(callableobj, keyarg=None): value = callableobj(self, *args) setattr(self, cache, value) return value + cache_wrapper1.__doc__ = callableobj.__doc__ return cache_wrapper1 elif keyarg: @@ -47,6 +47,7 @@ def cached(callableobj, keyarg=None): #print 'miss', self, cache, key _cache[key] = callableobj(self, *args, **kwargs) return _cache[key] + cache_wrapper2.__doc__ = callableobj.__doc__ return cache_wrapper2 def cache_wrapper3(self, *args): @@ -64,6 +65,7 @@ def cached(callableobj, keyarg=None): #print 'miss' _cache[args] = callableobj(self, *args) return _cache[args] + cache_wrapper3.__doc__ = callableobj.__doc__ return cache_wrapper3 def clear_cache(obj, funcname): diff --git a/test/unittest_decorators.py b/test/unittest_decorators.py index b2fd8b5..942fe12 100644 --- a/test/unittest_decorators.py +++ b/test/unittest_decorators.py @@ -2,7 +2,7 @@ """ from logilab.common.testlib import TestCase, unittest_main -from logilab.common.decorators import monkeypatch +from logilab.common.decorators import monkeypatch, cached class DecoratorsTC(TestCase): @@ -26,6 +26,20 @@ class DecoratorsTC(TestCase): inst = MyClass() self.assertEquals(inst.foo(4), 16) + def test_cached_preserves_docstrings(self): + class Foo(object): + @cached + def foo(self): + """ what's up doc ? """ + def bar(self, zogzog): + """ what's up doc ? """ + bar = cached(bar, 1) + @cached + def quux(self, zogzog): + """ what's up doc ? """ + self.assertEquals(Foo.foo.__doc__, """ what's up doc ? """) + self.assertEquals(Foo.bar.__doc__, """ what's up doc ? """) + self.assertEquals(Foo.quux.__doc__, """ what's up doc ? """) if __name__ == '__main__': unittest_main() |