[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.

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changeset091a372460a9
branchstable
phasepublic
hiddenno
parent revision#2520ee079cb5 [daemon] set $HOME when dropping privileges (closes #81297)
child revision#eac434b712b7 0.57.1
files modified by this revision
ChangeLog
compat.py
decorators.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1319534009 -7200
# Tue Oct 25 11:13:29 2011 +0200
# Branch stable
# Node ID 091a372460a9b65be2d202d5ecdd9e1e6bd4c110
# Parent 2520ee079cb58ae866602b41fe94a635bb61ab19
[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.

diff --git a/ChangeLog b/ChangeLog
@@ -1,10 +1,12 @@
1  ChangeLog for logilab.common
2  ============================
3 
4   --
5      * daemon: change $HOME after dropping privileges (closes #81297)
6 +    * compat: method_type for py3k use instance of the class to have a
7 +      real instance method (closes: #79268)
8 
9  2011-10-12  --  0.57.0
10      * only install unittest2 when python version < 2.7 (closes: #76068)
11 
12      * daemon: make pidfile world-readable (closes #75968)
diff --git a/compat.py b/compat.py
@@ -61,11 +61,12 @@
13      del Callable
14 
15  # See also http://bugs.python.org/issue11776
16  if sys.version_info[0] == 3:
17      def method_type(callable, instance, klass):
18 -        return types.MethodType(callable, klass)
19 +        # api change. klass is no more considered
20 +        return types.MethodType(callable, instance)
21  else:
22      # alias types otherwise
23      method_type = types.MethodType
24 
25  if sys.version_info < (3, 0):
diff --git a/decorators.py b/decorators.py
@@ -16,10 +16,11 @@
26  # You should have received a copy of the GNU Lesser General Public License along
27  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>.
28  """ A few useful function/method decorators. """
29  __docformat__ = "restructuredtext en"
30 
31 +import sys
32  from time import clock, time
33 
34  from logilab.common.compat import callable, method_type
35 
36  # XXX rewrite so we can use the decorator syntax when keyarg has to be specified
@@ -270,11 +271,11 @@
37              name = methodname or func.__name__
38          except AttributeError:
39              raise AttributeError('%s has no __name__ attribute: '
40                                   'you should provide an explicit `methodname`'
41                                   % func)
42 -        if callable(func):
43 +        if callable(func) and sys.version_info < (3, 0):
44              setattr(klass, name, method_type(func, None, klass))
45          else:
46              # likely a property
47              # this is quite borderline but usage already in the wild ...
48              setattr(klass, name, func)