summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2015-03-20 10:23:50 -0500
committerBenjamin Peterson <benjamin@python.org>2015-03-20 10:23:50 -0500
commit31666908f9c95fbcf80a1fe1960d350760fce67b (patch)
tree2d9d657ddb23e3143f6cbc2e1f4c9acccba5cc1b
parent37aaab859b0ddcdf38f51add80c3f1e37fffe97d (diff)
parentce2b04e7da617ae039a85c56fe3e9ff3e118feb0 (diff)
downloadsix-31666908f9c95fbcf80a1fe1960d350760fce67b.tar.gz
Merged in bartvm/six/create_unbound_method (pull request #64)
Introduces a wrapper to create unbound methods.
-rw-r--r--documentation/index.rst7
-rw-r--r--six.py6
-rw-r--r--test_six.py14
3 files changed, 27 insertions, 0 deletions
diff --git a/documentation/index.rst b/documentation/index.rst
index a8632e7..d838e38 100644
--- a/documentation/index.rst
+++ b/documentation/index.rst
@@ -232,6 +232,13 @@ functions and methods is the stdlib :mod:`py3:inspect` module.
requires the *obj*'s class to be passed.
+.. function:: create_unbound_method(func, cls)
+
+ Return an unbound method object wrapping *func*. In Python 2, this will return
+ a :func:`py3:types.MethodType` object. In Python 3 unbound methods do not
+ exist and this wrapper will return *func*.
+
+
.. class:: Iterator
A class for making portable iterators. The intention is that it be subclassed
diff --git a/six.py b/six.py
index 5e969d3..618c641 100644
--- a/six.py
+++ b/six.py
@@ -531,6 +531,9 @@ if PY3:
create_bound_method = types.MethodType
+ def create_unbound_method(func, cls):
+ return func
+
Iterator = object
else:
def get_unbound_function(unbound):
@@ -539,6 +542,9 @@ else:
def create_bound_method(func, obj):
return types.MethodType(func, obj, obj.__class__)
+ def create_unbound_method(func, cls):
+ return types.MethodType(func, None, cls)
+
class Iterator(object):
def next(self):
diff --git a/test_six.py b/test_six.py
index 118ccf6..060a966 100644
--- a/test_six.py
+++ b/test_six.py
@@ -456,6 +456,20 @@ def test_create_bound_method():
assert b() is x
+def test_create_unbound_method():
+ class X(object):
+ pass
+
+ def f(self):
+ return self
+ u = six.create_unbound_method(f, X)
+ py.test.raises(TypeError, u)
+ if six.PY2:
+ assert isinstance(u, types.MethodType)
+ x = X()
+ assert f(x) is x
+
+
if six.PY3:
def test_b():