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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
PYTHON setup.py build_ext --inplace
PYTHON -c "import a"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
import Cython.Compiler.Options
Cython.Compiler.Options.cimport_from_pyx = True
from distutils.core import setup
setup(
ext_modules = cythonize("*.pyx"),
)
######## a.pyx ########
from b cimport (Bclass, Bfunc, Bstruct, Benum, Benum_value, Btypedef, Py_EQ, Py_NE,
DecoratedClass, cfuncOutside)
cdef Bclass b = Bclass(5)
assert Bfunc(&b.value) == b.value
assert b.anotherValue == 6, b.anotherValue
assert b.asStruct().value == b.value
cdef Btypedef b_type = &b.value
cdef Benum b_enum = Benum_value
cdef int tmp = Py_EQ
cdef DecoratedClass dc = DecoratedClass()
assert dc.cfuncInClass().value == 5
assert dc.cpdefInClass() == 1.0
assert cfuncOutside().value == 2
#from c cimport ClassC
#cdef ClassC c = ClassC()
#print c.value
######## b.pyx ########
from cpython.object cimport Py_EQ, Py_NE
cimport cython
cdef enum Benum:
Benum_value
cdef struct Bstruct:
int value
ctypedef long *Btypedef
cdef class Bclass:
cdef long value
anotherValue: cython.double
def __init__(self, value):
self.value = value
self.anotherValue = value + 1
cdef Bstruct asStruct(self):
return Bstruct(value=self.value)
cdef double getOtherValue(self):
return self.anotherValue
cdef long Bfunc(Btypedef x):
return x[0]
@cython.cclass
class DecoratedClass:
@cython.cfunc
@cython.returns(Bstruct)
def cfuncInClass(self):
return Bstruct(value=5)
@cython.ccall
@cython.returns(cython.double)
def cpdefInClass(self):
return 1.0
@cython.cfunc
@cython.returns(Bstruct)
def cfuncOutside():
return Bstruct(value=2)
######## c.pxd ########
cdef class ClassC:
cdef int value
######## d.pyx ########
ctypedef fused fused_type:
long
double
cdef fused_checker(fused_type i):
if fused_type is long:
return True
else:
return False
def test():
return fused_checker(0)
|