1import cython
2from cython import cfunc, cclass, ccall
3
4@cython.test_assert_path_exists('//CFuncDefNode')
5@cython.cfunc
6def ftang():
7    x = 0
8
9@cython.test_assert_path_exists('//CFuncDefNode')
10@cfunc
11def fpure(a):
12    return a*2
13
14def test():
15    """
16    >>> test()
17    4
18    """
19    ftang()
20    return fpure(2)
21
22with cfunc:
23    @cython.test_assert_path_exists('//CFuncDefNode')
24    def fwith1(a):
25        return a*3
26
27    @cython.test_assert_path_exists('//CFuncDefNode')
28    def fwith2(a):
29        return a*4
30
31    @cython.test_assert_path_exists(
32        '//CFuncDefNode',
33        '//LambdaNode',
34        '//GeneratorDefNode',
35        '//GeneratorBodyDefNode',
36    )
37    def f_with_genexpr(a):
38        f = lambda x: x+1
39        return (f(x) for x in a)
40
41
42with cclass:
43    @cython.test_assert_path_exists('//CClassDefNode')
44    class Egg(object):
45        pass
46    @cython.test_assert_path_exists('//CClassDefNode')
47    class BigEgg(object):
48        @cython.test_assert_path_exists('//CFuncDefNode')
49        @cython.cfunc
50        def f(self, a):
51            return a*10
52
53def test_with():
54    """
55    >>> test_with()
56    (3, 4, 50)
57    """
58    return fwith1(1), fwith2(1), BigEgg().f(5)
59
60@cython.test_assert_path_exists('//CClassDefNode')
61@cython.cclass
62class PureFoo(object):
63    a = cython.declare(cython.double)
64
65    def __init__(self, a):
66        self.a = a
67
68    def __call__(self):
69        return self.a
70
71    @cython.test_assert_path_exists('//CFuncDefNode')
72    @cython.cfunc
73    def puremeth(self, a):
74        return a*2
75
76def test_method():
77    """
78    >>> test_method()
79    4
80    True
81    """
82    x = PureFoo(2)
83    print(x.puremeth(2))
84    if cython.compiled:
85        print(isinstance(x(), float))
86    else:
87        print(True)
88    return
89
90@cython.ccall
91def ccall_sqr(x):
92    return x*x
93
94@cclass
95class Overidable(object):
96    @ccall
97    def meth(self):
98        return 0
99
100def test_ccall():
101    """
102    >>> test_ccall()
103    25
104    >>> ccall_sqr(5)
105    25
106    """
107    return ccall_sqr(5)
108
109def test_ccall_method(x):
110    """
111    >>> test_ccall_method(Overidable())
112    0
113    >>> Overidable().meth()
114    0
115    >>> class Foo(Overidable):
116    ...    def meth(self):
117    ...        return 1
118    >>> test_ccall_method(Foo())
119    1
120    >>> Foo().meth()
121    1
122    """
123    return x.meth()
124
125@cython.cfunc
126@cython.returns(cython.p_int)
127@cython.locals(xptr=cython.p_int)
128def typed_return(xptr):
129    return xptr
130
131def test_typed_return():
132    """
133    >>> test_typed_return()
134    """
135    x = cython.declare(int, 5)
136    assert typed_return(cython.address(x))[0] is x
137
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    """
147    return f_with_genexpr(l)
148