summaryrefslogtreecommitdiff
path: root/tests/run/cfuncptr.pyx
blob: cb3b321847dc6f3e1da9ee0c1ab6a93a16e9d256 (plain)
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
# mode: run


cdef int grail():
    cdef int (*spam)()
    spam = &grail
    spam = grail
    assert spam is grail
    assert spam == grail
    assert spam == &grail


ctypedef int funcptr_t()

cdef funcptr_t* get_grail():
    return &grail


def test_assignments():
    """
    >>> test_assignments()
    """
    grail()


def test_return_value():
    """
    >>> test_return_value()
    True
    """
    g = get_grail()
    return g == &grail


def call_cfuncptr():
    """
    >>> call_cfuncptr()
    """
    cdef int (*spam)()
    spam = grail
    spam()

cdef int exceptminus2(int bad) except -2:
    if bad:
        raise RuntimeError
    else:
        return 0

def call_exceptminus2_through_exceptstar_pointer(bad):
    """
    >>> call_exceptminus2_through_exceptstar_pointer(True)
    Traceback (most recent call last):
    ...
    RuntimeError
    >>> call_exceptminus2_through_exceptstar_pointer(False)
    0
    """
    cdef int (*fptr)(int) except *  # GH4770 - should not be treated as except? -1
    fptr = exceptminus2
    return fptr(bad)

def call_exceptminus2_through_exceptmaybeminus2_pointer(bad):
    """
    >>> call_exceptminus2_through_exceptmaybeminus2_pointer(True)
    Traceback (most recent call last):
    ...
    RuntimeError
    >>> call_exceptminus2_through_exceptmaybeminus2_pointer(False)
    0
    """
    cdef int (*fptr)(int) except ?-2  # exceptions should be compatible
    fptr = exceptminus2
    return fptr(bad)

cdef int noexcept_func():  # noexcept
    return 0

def call_noexcept_func_except_star():
    """
    >>> call_noexcept_func_except_star()
    0
    """
    cdef int (*fptr)() except *
    fptr = noexcept_func  # exception specifications are compatible
    return fptr()

def call_noexcept_func_except_check():
    """
    >>> call_noexcept_func_except_check()
    0
    """
    cdef int (*fptr)() except ?-1
    fptr = noexcept_func  # exception specifications are compatible
    return fptr()