python 3.6: cdef classes with cpdef methods cannot be pickled #1606

Open
mhooreman opened this Issue Feb 15, 2017 · 0 comments

Projects

None yet

1 participant

@mhooreman
mhooreman commented Feb 15, 2017 edited

Hello,

In python 3.6.0, the cdef classes with cpdef methods cannot be pickled.
The problem doesn't appears with python 3.5.3.

I'm using cython 0.25.2.

You can find below a package structure to reproduce it.

Thanks a lot for your help.
Best regards.

Package structure:

├── lib
│   ├── cy.pxd
│   ├── cy.pyx
│   ├── __init__.py
|── main.py

Code:

######################### lib/cy.pxd #########################

cdef class WithoutC:
    def hello(self):
        return "Hello, World"
cdef class WithCPDef:
    cpdef str hello(self):
        return "Hello, World"
cdef class WithCDefWrapper:
    def hello(self):
        return _helloC(self)
cpdef _helloC(object caller):
    return "Hello, World"

######################### lib/cy.pxd #########################

cdef class WithoutCPDef:
    pass
cdef class WithCPDef:
    cpdef str hello(self)
cdef class WithCDefWrapper:
    pass
cpdef _helloC(object caller)

######################### lib/init.py #########################

import pyximport
import numpy
pyximport.install(
    pyximport=True,
    pyimport=False,
    build_dir=None,
    build_in_temp=True,
    reload_support=False,
    load_py_module_on_import_failure=False,
    inplace=False,
    language_level=3,
    setup_args={'include_dirs': numpy.get_include()}
)

######################### main.py #########################

#!/usr/bin/env python3

import pickle as pkl
import os
from lib.cy import WithoutC, WithCPDef, WithCDefWrapper
def tryThis(obj):
    print("Pickling %s ..." % obj.__class__.__name__)
    try:
        pkl.dump(obj, open("test.pkl", "wb"))
        print("\t... OK")
    except Exception as e:
        print("\t... KO: %s" % str(e))
for t in WithoutC(), WithCPDef(), WithCDefWrapper():
    tryThis(t)
if os.path.exists("test.pkl"):
    os.remove("test.pkl")

Output with python 3.5.3:

Pickling WithoutC ...
	... OK
Pickling WithCPDef ...
	... OK
Pickling WithCDefWrapper ...
	... OK

Output with python 3.6.0:

Pickling WithoutC ...
	... OK
Pickling WithCPDef ...
	... KO: can't pickle lib.cy.WithCPDef objects
Pickling WithCDefWrapper ...
	... OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment