cython
1import cython2from cython import cfunc, cclass, ccall3
4@cython.test_assert_path_exists('//CFuncDefNode')5@cython.cfunc6def ftang():7x = 08
9@cython.test_assert_path_exists('//CFuncDefNode')10@cfunc
11def fpure(a):12return a*213
14def test():15"""16>>> test()
174
18"""
19ftang()20return fpure(2)21
22with cfunc:23@cython.test_assert_path_exists('//CFuncDefNode')24def fwith1(a):25return a*326
27@cython.test_assert_path_exists('//CFuncDefNode')28def fwith2(a):29return a*430
31@cython.test_assert_path_exists(32'//CFuncDefNode',33'//LambdaNode',34'//GeneratorDefNode',35'//GeneratorBodyDefNode',36)37def f_with_genexpr(a):38f = lambda x: x+139return (f(x) for x in a)40
41
42with cclass:43@cython.test_assert_path_exists('//CClassDefNode')44class Egg(object):45pass46@cython.test_assert_path_exists('//CClassDefNode')47class BigEgg(object):48@cython.test_assert_path_exists('//CFuncDefNode')49@cython.cfunc50def f(self, a):51return a*1052
53def test_with():54"""55>>> test_with()
56(3, 4, 50)
57"""
58return fwith1(1), fwith2(1), BigEgg().f(5)59
60@cython.test_assert_path_exists('//CClassDefNode')61@cython.cclass62class PureFoo(object):63a = cython.declare(cython.double)64
65def __init__(self, a):66self.a = a67
68def __call__(self):69return self.a70
71@cython.test_assert_path_exists('//CFuncDefNode')72@cython.cfunc73def puremeth(self, a):74return a*275
76def test_method():77"""78>>> test_method()
794
80True
81"""
82x = PureFoo(2)83print(x.puremeth(2))84if cython.compiled:85print(isinstance(x(), float))86else:87print(True)88return89
90@cython.ccall91def ccall_sqr(x):92return x*x93
94@cclass
95class Overridable(object):96@ccall97def meth(self):98return 099
100def test_ccall():101"""102>>> test_ccall()
10325
104>>> ccall_sqr(5)
10525
106"""
107return ccall_sqr(5)108
109def test_ccall_method(x):110"""111>>> test_ccall_method(Overridable())
1120
113>>> Overridable().meth()
1140
115>>> class Foo(Overridable):
116... def meth(self):
117... return 1
118>>> test_ccall_method(Foo())
1191
120>>> Foo().meth()
1211
122"""
123return x.meth()124
125@cython.cfunc126@cython.returns(cython.p_int)127@cython.locals(xptr=cython.p_int)128def typed_return(xptr):129return xptr130
131def test_typed_return():132"""133>>> test_typed_return()
134"""
135x = cython.declare(int, 5)136assert typed_return(cython.address(x))[0] is x137
138
139def test_genexpr_in_cdef(l):140"""141>>> gen = test_genexpr_in_cdef([1, 2, 3])
142>>> list(gen)
143[2, 3, 4]
144>>> list(gen)
145[]
146"""
147return f_with_genexpr(l)148