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