diff options
author | Aurelien Campeas <aurelien.campeas@logilab.fr> | 2011-09-06 16:01:27 +0200 |
---|---|---|
committer | Aurelien Campeas <aurelien.campeas@logilab.fr> | 2011-09-06 16:01:27 +0200 |
commit | b4503f620b731984efce8c97086868c21431d807 (patch) | |
tree | 3401e518a303c16e983ac2f4fc0466b4e810617d /decorators.py | |
parent | d18aef0ff0eb5a3577c232eed317bd2b0001e601 (diff) | |
download | logilab-common-b4503f620b731984efce8c97086868c21431d807.tar.gz |
[decorators] monkeypatch should build a method type closes #73920
Diffstat (limited to 'decorators.py')
-rw-r--r-- | decorators.py | 15 |
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 |