Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Python 3, an unbound method is just a function #2105

merged 1 commit into from Feb 15, 2018


Copy link

@jdemeyer jdemeyer commented Feb 15, 2018

In plain Python 3, there is no "unbound method" type, an unbound method is just the function in the class __dict__:

In [1]: class P:
   ...:     def meth(self): pass

In [2]: P.meth
Out[2]: <function __main__.P.meth>

In [3]: P.meth is P.__dict__["meth"]
Out[3]: True

In Cython, unbound methods instead become instancemethod objects:

In [3]: %%cython
   ...: cimport cython
   ...: cdef class X:
   ...:     @cython.binding(True)
   ...:     def meth(self): pass
   ...: class Y:
   ...:     def meth(self): pass

In [4]: X.meth
Out[4]: <instancemethod meth at 0x3fff79e0a7c8>

In [5]: Y.meth
Out[5]: <instancemethod meth at 0x3fff7a7bbd38>

This uses PyInstanceMethod_New. That seems wrong because the docs say that this is meant for a PyCFunction. Given that Cython functions should be analogous to Python functions, this should be fixed.

Context: this broke SageMath on Python 3 because this causes unbound methods to be no longer hashable (functions are hashable but instancemethods are not).

Copy link
Contributor Author

Failing tests in pure mode...

Copy link
Contributor Author

I wrongly assumed that the pure_py3 test would only be run on Python 3.

@jdemeyer jdemeyer changed the title In Python 3, an unbound method is just the function In Python 3, an unbound method is just a function Feb 15, 2018
Copy link

scoder commented Feb 15, 2018

The only place, where this macro is used, is here, in the descriptor implementation that is pretty much the sole purpose of PyInstanceMethod. So this seems very reasonable. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

2 participants