diff options
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | documentation/index.rst | 8 | ||||
-rw-r--r-- | six.py | 5 | ||||
-rw-r--r-- | test_six.py | 11 |
4 files changed, 26 insertions, 0 deletions
@@ -6,6 +6,8 @@ This file lists the changes in each six version. Development version ------------------- +- Add create_bound_method() wrapper. + - Issue #23: Allow multiple base classes to be passed to with_metaclass. - Issue #24: Add six.moves.range alias. This exactly the same as the current diff --git a/documentation/index.rst b/documentation/index.rst index a1513b5..ee92f89 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -199,6 +199,14 @@ functions and methods is the stdlib :mod:`py3:inspect` module. *kwargs* are passed through to the underlying method. +.. function:: create_bound_method(func, obj) + + Return a method object wrapping *func* and bound to *obj*. On both Python 2 + and 3, this will return a :func:`py3:types.MethodType` object. The reason + this wrapper exists is that on Python 2, the ``MethodType`` constructor + requires the *obj*'s class to be passed. + + .. class:: Iterator A class for making portable iterators. The intention is that it be subclassed @@ -253,11 +253,16 @@ if PY3: def get_unbound_function(unbound): return unbound + create_bound_method = types.MethodType + Iterator = object else: def get_unbound_function(unbound): return unbound.im_func + def create_bound_method(func, obj): + return types.MethodType(func, obj, obj.__class__) + class Iterator(object): def next(self): diff --git a/test_six.py b/test_six.py index d1957ab..8fcd4d4 100644 --- a/test_six.py +++ b/test_six.py @@ -306,6 +306,17 @@ def test_callable(): assert not six.callable("string") +def test_create_bound_method(): + class X(object): + pass + def f(self): + return self + x = X() + b = six.create_bound_method(f, x) + assert isinstance(b, types.MethodType) + assert b() is x + + if six.PY3: def test_b(): |