diff options
author | Aurelien Campeas <aurelien.campeas@logilab.fr> | 2011-09-21 18:10:25 +0200 |
---|---|---|
committer | Aurelien Campeas <aurelien.campeas@logilab.fr> | 2011-09-21 18:10:25 +0200 |
commit | 8b8a58ca17f97eda64ca7a253d3cc11893508ac1 (patch) | |
tree | 60d716007d9a3dd71d876a4303bea152ce30d858 | |
parent | 28f99d3b81a90c73b7f5c5a7a8c2b2665fe86053 (diff) | |
download | logilab-common-8b8a58ca17f97eda64ca7a253d3cc11893508ac1.tar.gz |
fix py3k compat (closes #75290)
New:
- update compat module for callable() and method_type()
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | compat.py | 21 | ||||
-rw-r--r-- | decorators.py | 10 |
3 files changed, 26 insertions, 11 deletions
@@ -1,6 +1,12 @@ ChangeLog for logilab.common ============================ + -- +* only install unittest2 when python version < 2.7 (closes: #76068) +* daemon: make pidfile world-readable (closes #75968) +* update compat module for callable() and method_type() +* decorators: fix monkeypatch py3k compat (closes #75290) + 2011-09-08 -- 0.56.2 * daemon: call initgroups/setgid before setuid (closes #74173) @@ -32,6 +32,7 @@ __docformat__ = "restructuredtext en" import os import sys +import types from warnings import warn import __builtin__ as builtins # 2to3 will tranform '__builtin__' to 'builtins' @@ -50,14 +51,22 @@ else: def str_encode(string, encoding): return str(string) -# XXX shouldn't we remove this and just let 2to3 do his job ? +# XXX callable builti-in seems back in all python versions try: - callable = callable -except NameError:# callable removed from py3k - import collections + callable = builtins.callable +except AttributeError: + from collections import Callable def callable(something): - return isinstance(something, collections.Callable) - del collections + return isinstance(something, Callable) + del Callable + +# See also http://bugs.python.org/issue11776 +if sys.version_info[0] == 3: + def method_type(callable, instance, klass): + return types.MethodType(callable, klass) +else: + # alias types otherwise + method_type = types.MethodType if sys.version_info < (3, 0): raw_input = raw_input diff --git a/decorators.py b/decorators.py index 7bb08fc..91d7492 100644 --- a/decorators.py +++ b/decorators.py @@ -18,10 +18,10 @@ """ A few useful function/method decorators. """ __docformat__ = "restructuredtext en" -import types -import sys, re from time import clock, time +from logilab.common.compat import callable, method_type + # XXX rewrite so we can use the decorator syntax when keyarg has to be specified def _is_generator_function(callableobj): @@ -175,8 +175,8 @@ class iclassmethod(object): self.func = func def __get__(self, instance, objtype): if instance is None: - return types.MethodType(self.func, objtype, objtype.__class__) - return types.MethodType(self.func, instance, objtype) + return method_type(self.func, objtype, objtype.__class__) + return method_type(self.func, instance, objtype) def __set__(self, instance, value): raise AttributeError("can't set attribute") @@ -234,7 +234,7 @@ def monkeypatch(klass, methodname=None): 'you should provide an explicit `methodname`' % func) if callable(func): - setattr(klass, name, types.MethodType(func, None, klass)) + setattr(klass, name, method_type(func, None, klass)) else: # likely a property # this is quite borderline but usage already in the wild ... |