From 252f1320ba9304820226de6c37a7752612f53146 Mon Sep 17 00:00:00 2001 From: Sylvain Th?nault Date: Tue, 25 Oct 2011 11:13:29 +0200 Subject: [compat] use instance of the class to have a real instance method (closes: #79268) Details: By using `klass` instead of an instance of the class, we bounded the method as a class method. During execution, the monkey-patched method considered `self` as a reference to the class and further use of `self` failed miserably. --- ChangeLog | 2 ++ compat.py | 3 ++- decorators.py | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6bbb6d1..dd7bcf4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ ChangeLog for logilab.common -- * daemon: change $HOME after dropping privileges (closes #81297) + * compat: method_type for py3k use instance of the class to have a + real instance method (closes: #79268) 2011-10-12 -- 0.57.0 * only install unittest2 when python version < 2.7 (closes: #76068) diff --git a/compat.py b/compat.py index 47ef25c..8983ece 100644 --- a/compat.py +++ b/compat.py @@ -63,7 +63,8 @@ except AttributeError: # See also http://bugs.python.org/issue11776 if sys.version_info[0] == 3: def method_type(callable, instance, klass): - return types.MethodType(callable, klass) + # api change. klass is no more considered + return types.MethodType(callable, instance) else: # alias types otherwise method_type = types.MethodType diff --git a/decorators.py b/decorators.py index 2bce3e8..43c3652 100644 --- a/decorators.py +++ b/decorators.py @@ -18,6 +18,7 @@ """ A few useful function/method decorators. """ __docformat__ = "restructuredtext en" +import sys from time import clock, time from logilab.common.compat import callable, method_type @@ -272,7 +273,7 @@ def monkeypatch(klass, methodname=None): raise AttributeError('%s has no __name__ attribute: ' 'you should provide an explicit `methodname`' % func) - if callable(func): + if callable(func) and sys.version_info < (3, 0): setattr(klass, name, method_type(func, None, klass)) else: # likely a property -- cgit v1.2.1