1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
# mode: run
# tag: pickle
PYTHON main.py build_ext -i
######################### lib/__init__.py #########################
######################### lib/cy.pyx #########################
# cython: binding=True
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 #########################
# cython:language_level=3
cdef class WithoutCPDef:
pass
cdef class WithCPDef:
cpdef str hello(self)
cdef class WithCDefWrapper:
pass
cpdef _helloC(object caller)
######################### main.py #########################
#!/usr/bin/env python3
from Cython.Build import cythonize
from distutils.core import setup
setup(
ext_modules = cythonize(["lib/*.pyx"]),
)
import pickle as pkl
import os
from lib.cy import WithoutC, WithCPDef, WithCDefWrapper
def tryThis(obj):
print("Pickling %s ..." % obj.__class__.__name__)
try:
with open("test.pkl", "wb") as fid:
pkl.dump(obj, fid)
print("\t... OK")
except Exception as e:
print("\t... KO: %s" % str(e))
try:
for t in WithoutC(), WithCPDef(), WithCDefWrapper():
tryThis(t)
finally:
if os.path.exists("test.pkl"):
os.remove("test.pkl")
|