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

Cython-generated unbound class methods cannot be pickled #2972

Closed
pierreglaser opened this issue May 27, 2019 · 1 comment · Fixed by #2969
Closed

Cython-generated unbound class methods cannot be pickled #2972

pierreglaser opened this issue May 27, 2019 · 1 comment · Fixed by #2969

Comments

@pierreglaser
Copy link
Contributor

Building the following module (src/module.py)

class MyClass:

    def __init__(self, a):
        self.a = a

    def my_method(self, b):
        return self.a + b

Using cython with the following setup script:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("src/module.py"))

Generates a class (MyClass), whose unbound methods cannot be pickled:

In [1]: from module import MyClass                                                                                                                                                                                 
                                                                                                                                                                                                                   
In [2]: import pickle                                                                                                                                                                                              
                                                                                                                                                                                                                   
In [3]: pickle.dumps(MyClass.my_method)                                                                                                                                                              
---------------------------------------------------------------------------                                                                                                                                        
PicklingError                             Traceback (most recent call last)                                                                                                                                        
<ipython-input-3-cf09bde7c7ba> in <module>                                                                                                                                                                         
----> 1 pickle.dumps(MyClass.my_method)                                                                                                                                                              
                                                                                                                                                                                                                   
PicklingError: Can't pickle <cyfunction MyClass.my_method at 0x7f8d130e9ea8>: attribute lookup
my_method on module failed
@pierreglaser
Copy link
Contributor Author

pierreglaser commented May 27, 2019

Note that this bug only affects non-extension cython classes:

  • if MyClass is defined as an extension type (using cdef) then MyClass.my_method becomes a method_descriptor, which is picklable by default.
  • otherwise, MyClass.my_method is a cyfunction, with an invalid __reduce__ method.

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

Successfully merging a pull request may close this issue.

2 participants