cython
1# mode: run
2
3
4cdef int grail():
5cdef int (*spam)()
6spam = &grail
7spam = grail
8assert spam is grail
9assert spam == grail
10assert spam == &grail
11
12
13ctypedef int funcptr_t()
14
15cdef funcptr_t* get_grail():
16return &grail
17
18
19def test_assignments():
20"""
21>>> test_assignments()
22"""
23grail()
24
25
26def test_return_value():
27"""
28>>> test_return_value()
29True
30"""
31g = get_grail()
32return g == &grail
33
34
35def call_cfuncptr():
36"""
37>>> call_cfuncptr()
38"""
39cdef int (*spam)()
40spam = grail
41spam()
42
43cdef int exceptminus2(int bad) except -2:
44if bad:
45raise RuntimeError
46else:
47return 0
48
49def call_exceptminus2_through_exceptstar_pointer(bad):
50"""
51>>> call_exceptminus2_through_exceptstar_pointer(True)
52Traceback (most recent call last):
53...
54RuntimeError
55>>> call_exceptminus2_through_exceptstar_pointer(False)
560
57"""
58cdef int (*fptr)(int) except * # GH4770 - should not be treated as except? -1
59fptr = exceptminus2
60return fptr(bad)
61
62def call_exceptminus2_through_exceptmaybeminus2_pointer(bad):
63"""
64>>> call_exceptminus2_through_exceptmaybeminus2_pointer(True)
65Traceback (most recent call last):
66...
67RuntimeError
68>>> call_exceptminus2_through_exceptmaybeminus2_pointer(False)
690
70"""
71cdef int (*fptr)(int) except ?-2 # exceptions should be compatible
72fptr = exceptminus2
73return fptr(bad)
74
75cdef int noexcept_func(): # noexcept
76return 0
77
78def call_noexcept_func_except_star():
79"""
80>>> call_noexcept_func_except_star()
810
82"""
83cdef int (*fptr)() except *
84fptr = noexcept_func # exception specifications are compatible
85return fptr()
86
87def call_noexcept_func_except_check():
88"""
89>>> call_noexcept_func_except_check()
900
91"""
92cdef int (*fptr)() except ?-1
93fptr = noexcept_func # exception specifications are compatible
94return fptr()
95