summaryrefslogtreecommitdiff
path: root/decorators.py
diff options
context:
space:
mode:
authorAurelien Campeas <aurelien.campeas@logilab.fr>2011-09-06 16:01:27 +0200
committerAurelien Campeas <aurelien.campeas@logilab.fr>2011-09-06 16:01:27 +0200
commitb4503f620b731984efce8c97086868c21431d807 (patch)
tree3401e518a303c16e983ac2f4fc0466b4e810617d /decorators.py
parentd18aef0ff0eb5a3577c232eed317bd2b0001e601 (diff)
downloadlogilab-common-b4503f620b731984efce8c97086868c21431d807.tar.gz
[decorators] monkeypatch should build a method type closes #73920
Diffstat (limited to 'decorators.py')
-rw-r--r--decorators.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/decorators.py b/decorators.py
index 39144d1..7bb08fc 100644
--- a/decorators.py
+++ b/decorators.py
@@ -209,7 +209,7 @@ def locked(acquire, release):
def monkeypatch(klass, methodname=None):
- """Decorator extending class with the decorated function
+ """Decorator extending class with the decorated callable
>>> class A:
... pass
>>> @monkeypatch(A)
@@ -227,6 +227,17 @@ def monkeypatch(klass, methodname=None):
12
"""
def decorator(func):
- setattr(klass, methodname or func.__name__, func)
+ try:
+ name = methodname or func.__name__
+ except AttributeError:
+ raise AttributeError('%s has no __name__ attribute: '
+ 'you should provide an explicit `methodname`'
+ % func)
+ if callable(func):
+ setattr(klass, name, types.MethodType(func, None, klass))
+ else:
+ # likely a property
+ # this is quite borderline but usage already in the wild ...
+ setattr(klass, name, func)
return func
return decorator