1cimport cython
2
3from libc.string cimport strstr
4
5cdef cfunc(a,b,c,d):
6    return (a,b,c,d)
7
8cpdef cpfunc(a,b,c,d):
9    return (a,b,c,d)
10
11cdef optargs(a, b=2, c=3):
12    return (a,b,c)
13
14ctypedef int (*cfuncptr_type)(int a, int b)
15cdef int cfuncptr(int a, int b):
16    print a, b
17
18cdef cfuncptr_type get_cfuncptr():
19    return cfuncptr
20
21
22sideeffect = []
23cdef side_effect(x):
24    sideeffect.append(x)
25    return x
26
27
28@cython.test_fail_if_path_exists('//GeneralCallNode')
29@cython.test_assert_path_exists('//SimpleCallNode')
30def cfunc_all_keywords():
31    """
32    >>> cfunc_all_keywords()
33    (1, 2, 3, 4)
34    """
35    return cfunc(a=1, b=2, c=3, d=4)
36
37
38@cython.test_fail_if_path_exists('//GeneralCallNode')
39@cython.test_assert_path_exists('//SimpleCallNode')
40def cfunc_some_keywords():
41    """
42    >>> cfunc_some_keywords()
43    (1, 2, 3, 4)
44    """
45    return cfunc(1, 2, c=3, d=4)
46
47
48@cython.test_fail_if_path_exists('//GeneralCallNode')
49@cython.test_assert_path_exists('//SimpleCallNode')
50def cfunc_some_keywords_unordered():
51    """
52    >>> cfunc_some_keywords_unordered()
53    (1, 2, 3, 4)
54    """
55    return cfunc(1, 2, d=4, c=3)
56
57
58@cython.test_fail_if_path_exists('//GeneralCallNode')
59@cython.test_assert_path_exists('//SimpleCallNode')
60def cfunc_some_keywords_unordered_sideeffect():
61    """
62    >>> del sideeffect[:]
63    >>> cfunc_some_keywords_unordered_sideeffect()
64    (1, 2, 3, 4)
65    >>> sideeffect
66    [4, 3]
67    """
68    return cfunc(1, 2, d=side_effect(4), c=side_effect(3))
69
70
71@cython.test_fail_if_path_exists('//GeneralCallNode')
72@cython.test_assert_path_exists('//SimpleCallNode')
73def cpfunc_all_keywords():
74    """
75    >>> cpfunc_all_keywords()
76    (1, 2, 3, 4)
77    """
78    return cpfunc(a=1, b=2, c=3, d=4)
79
80
81@cython.test_fail_if_path_exists('//GeneralCallNode')
82@cython.test_assert_path_exists('//SimpleCallNode')
83def cpfunc_some_keywords():
84    """
85    >>> cpfunc_some_keywords()
86    (1, 2, 3, 4)
87    """
88    return cpfunc(1, 2, c=3, d=4)
89
90
91@cython.test_fail_if_path_exists('//GeneralCallNode')
92@cython.test_assert_path_exists('//SimpleCallNode')
93def cpfunc_some_keywords_unordered():
94    """
95    >>> cpfunc_some_keywords_unordered()
96    (1, 2, 3, 4)
97    """
98    return cpfunc(1, 2, d=4, c=3)
99
100
101@cython.test_fail_if_path_exists('//GeneralCallNode')
102@cython.test_assert_path_exists('//SimpleCallNode')
103def cpfunc_some_keywords_unordered_sideeffect():
104    """
105    >>> del sideeffect[:]
106    >>> cpfunc_some_keywords_unordered_sideeffect()
107    (1, 2, 3, 4)
108    >>> sideeffect
109    [4, 3]
110    """
111    return cpfunc(1, 2, d=side_effect(4), c=side_effect(3))
112
113
114@cython.test_fail_if_path_exists('//GeneralCallNode')
115@cython.test_assert_path_exists('//SimpleCallNode')
116def libc_strstr():
117    """
118    >>> libc_strstr()
119    (True, True, True, True, True)
120    """
121    return (
122        strstr("xabcy", "abc") is not NULL,
123        strstr("abc", "xabcy") is NULL,
124        strstr(needle="abc", haystack="xabcz") is not NULL,
125        strstr(needle="xabcz", haystack="abc") is NULL,
126        strstr(haystack="abc", needle="xabcz") is NULL,
127        )
128
129
130@cython.test_fail_if_path_exists('//GeneralCallNode')
131@cython.test_assert_path_exists('//SimpleCallNode')
132def cdef_optargs():
133    """
134    >>> cdef_optargs()
135    (11, 2, 3)
136    (11, 2, 3)
137    (11, 12, 3)
138    (11, 12, 3)
139    (11, 12, 3)
140    (11, 12, 3)
141    (11, 12, 3)
142    (11, 12, 13)
143    (11, 12, 13)
144    (11, 12, 13)
145    (11, 12, 13)
146    (11, 12, 13)
147    (11, 12, 13)
148    (11, 12, 13)
149    """
150    print(optargs(11))
151    print(optargs(a=11))
152
153    print(optargs(11,   12))
154    print(optargs(11,   b=12))
155    print(optargs(a=11, b=12))
156    print(optargs(b=12, a=11))
157    print(optargs(a=11, b=12))
158
159    print(optargs(11,   12,   13))
160    print(optargs(11,   12,   c=13))
161    print(optargs(11,   c=13, b=12))
162    print(optargs(a=11, b=12, c=13))
163    print(optargs(b=12, a=11, c=13))
164    print(optargs(b=12, c=13, a=11))
165    print(optargs(c=13, a=11, b=12))
166
167
168@cython.test_fail_if_path_exists('//GeneralCallNode')
169@cython.test_assert_path_exists('//SimpleCallNode')
170def cdef_funcptr():
171    """
172    >>> cdef_funcptr()
173    1 2
174    1 2
175    1 2
176    1 2
177    """
178    cdef cfuncptr_type cfunc_ptr = get_cfuncptr()
179    cfunc_ptr(1, 2)
180    cfunc_ptr(1, b=2)
181    cfunc_ptr(a=1, b=2)
182    cfunc_ptr(b=2, a=1)
183
184
185'''
186# This works but currently brings up C compiler warnings
187# because the format string is not a literal C string.
188
189from libc.stdio cimport snprintf
190
191@cython.test_fail_if_path_exists('//GeneralCallNode')
192@cython.test_assert_path_exists('//SimpleCallNode')
193def varargs():
194    """
195    >>> print(varargs())
196    abc
197    """
198    cdef char[10] buffer
199    retval = snprintf(buffer, template="abc", size=10)
200    if retval < 0:
201        raise MemoryError()
202    return buffer[:retval].decode('ascii')
203'''
204
205
206cdef class ExtType:
207    cdef cmeth(self, a, b, c, d):
208        return (a,b,c,d)
209
210    @cython.test_fail_if_path_exists('//GeneralCallNode')
211    @cython.test_assert_path_exists('//SimpleCallNode')
212    def call_cmeth(self, ExtType ext):
213        """
214        >>> x = ExtType()
215        >>> x.call_cmeth(x)
216        (1, 2, 3, 4)
217        (1, 2, 3, 4)
218        (1, 2, 3, 4)
219        EXT
220        (1, 2, 3, 4)
221        (1, 2, 3, 4)
222        (1, 2, 3, 4)
223        """
224        print self.cmeth(1,2,3,4)
225        print self.cmeth(1,2,c=3,d=4)
226        print self.cmeth(a=1,b=2,c=3,d=4)
227        print "EXT"
228        print ext.cmeth(1,2,3,4)
229        print ext.cmeth(1,2,c=3,d=4)
230        print ext.cmeth(a=1,b=2,c=3,d=4)
231
232    cpdef cpmeth(self, a, b, c, d):
233        return (a,b,c,d)
234
235    @cython.test_fail_if_path_exists('//GeneralCallNode')
236    @cython.test_assert_path_exists('//SimpleCallNode')
237    def call_cpmeth(self, ExtType ext):
238        """
239        >>> x = ExtType()
240        >>> x.call_cpmeth(x)
241        (1, 2, 3, 4)
242        (1, 2, 3, 4)
243        (1, 2, 3, 4)
244        EXT
245        (1, 2, 3, 4)
246        (1, 2, 3, 4)
247        (1, 2, 3, 4)
248        """
249        print self.cpmeth(1,2,3,4)
250        print self.cpmeth(1,2,c=3,d=4)
251        print self.cpmeth(a=1,b=2,c=3,d=4)
252        print "EXT"
253        print ext.cpmeth(1,2,3,4)
254        print ext.cpmeth(1,2,c=3,d=4)
255        print ext.cpmeth(a=1,b=2,c=3,d=4)
256
257    cdef optargs(self, a=1, b=2):
258        return (a,b)
259
260    @cython.test_fail_if_path_exists('//GeneralCallNode')
261    @cython.test_assert_path_exists('//SimpleCallNode')
262    def call_optargs(self, ExtType ext):
263        """
264        >>> x = ExtType()
265        >>> x.call_optargs(x)
266        (3, 4)
267        (3, 4)
268        (3, 4)
269        (1, 2)
270        (3, 2)
271        (3, 2)
272        EXT
273        (3, 4)
274        (3, 4)
275        (3, 4)
276        (1, 2)
277        (3, 2)
278        (3, 2)
279        """
280        print self.optargs(3,4)
281        print self.optargs(3,b=4)
282        print self.optargs(a=3,b=4)
283        print self.optargs()
284        print self.optargs(3)
285        print self.optargs(a=3)
286        #print self.optargs(b=4)
287        print "EXT"
288        print ext.optargs(3,4)
289        print ext.optargs(3,b=4)
290        print ext.optargs(a=3,b=4)
291        print ext.optargs()
292        print ext.optargs(3)
293        print ext.optargs(a=3)
294        #print ext.optargs(b=4)
295
296    cpdef cpmeth_optargs(self, a=1, b=2):
297        return (a,b)
298
299    @cython.test_fail_if_path_exists('//GeneralCallNode')
300    @cython.test_assert_path_exists('//SimpleCallNode')
301    def call_cpmeth_optargs(self, ExtType ext):
302        """
303        >>> x = ExtType()
304        >>> x.call_cpmeth_optargs(x)
305        (3, 4)
306        (3, 4)
307        (3, 4)
308        (1, 2)
309        (3, 2)
310        (3, 2)
311        EXT
312        (3, 4)
313        (3, 4)
314        (3, 4)
315        (1, 2)
316        (3, 2)
317        (3, 2)
318        """
319        print self.cpmeth_optargs(3,4)
320        print self.cpmeth_optargs(3,b=4)
321        print self.cpmeth_optargs(a=3,b=4)
322        print self.cpmeth_optargs()
323        print self.cpmeth_optargs(3)
324        print self.cpmeth_optargs(a=3)
325        #print self.cpmeth_optargs(b=4)
326        print "EXT"
327        print ext.cpmeth_optargs(3,4)
328        print ext.cpmeth_optargs(3,b=4)
329        print ext.cpmeth_optargs(a=3,b=4)
330        print ext.cpmeth_optargs()
331        print ext.cpmeth_optargs(3)
332        print ext.cpmeth_optargs(a=3)
333        #print ext.cpmeth_optargs(b=4)
334
335    cpdef cpmeth_optargs1(self, a=1):
336        return a
337
338    @cython.test_fail_if_path_exists('//GeneralCallNode')
339    @cython.test_assert_path_exists('//SimpleCallNode')
340    def call_cpmeth_optargs1(self, ExtType ext):
341        """
342        >>> x = ExtType()
343        >>> x.call_cpmeth_optargs1(x)
344        1
345        3
346        3
347        EXT
348        1
349        3
350        3
351        """
352        print self.cpmeth_optargs1()
353        print self.cpmeth_optargs1(3)
354        print self.cpmeth_optargs1(a=3)
355        print "EXT"
356        print ext.cpmeth_optargs1()
357        print ext.cpmeth_optargs1(3)
358        print ext.cpmeth_optargs1(a=3)
359