cython

Форк
0
/
cfuncptr.pyx 
94 строки · 1.9 Кб
1
# mode: run
2

3

4
cdef int grail():
5
    cdef int (*spam)()
6
    spam = &grail
7
    spam = grail
8
    assert spam is grail
9
    assert spam == grail
10
    assert spam == &grail
11

12

13
ctypedef int funcptr_t()
14

15
cdef funcptr_t* get_grail():
16
    return &grail
17

18

19
def test_assignments():
20
    """
21
    >>> test_assignments()
22
    """
23
    grail()
24

25

26
def test_return_value():
27
    """
28
    >>> test_return_value()
29
    True
30
    """
31
    g = get_grail()
32
    return g == &grail
33

34

35
def call_cfuncptr():
36
    """
37
    >>> call_cfuncptr()
38
    """
39
    cdef int (*spam)()
40
    spam = grail
41
    spam()
42

43
cdef int exceptminus2(int bad) except -2:
44
    if bad:
45
        raise RuntimeError
46
    else:
47
        return 0
48

49
def call_exceptminus2_through_exceptstar_pointer(bad):
50
    """
51
    >>> call_exceptminus2_through_exceptstar_pointer(True)
52
    Traceback (most recent call last):
53
    ...
54
    RuntimeError
55
    >>> call_exceptminus2_through_exceptstar_pointer(False)
56
    0
57
    """
58
    cdef int (*fptr)(int) except *  # GH4770 - should not be treated as except? -1
59
    fptr = exceptminus2
60
    return fptr(bad)
61

62
def call_exceptminus2_through_exceptmaybeminus2_pointer(bad):
63
    """
64
    >>> call_exceptminus2_through_exceptmaybeminus2_pointer(True)
65
    Traceback (most recent call last):
66
    ...
67
    RuntimeError
68
    >>> call_exceptminus2_through_exceptmaybeminus2_pointer(False)
69
    0
70
    """
71
    cdef int (*fptr)(int) except ?-2  # exceptions should be compatible
72
    fptr = exceptminus2
73
    return fptr(bad)
74

75
cdef int noexcept_func():  # noexcept
76
    return 0
77

78
def call_noexcept_func_except_star():
79
    """
80
    >>> call_noexcept_func_except_star()
81
    0
82
    """
83
    cdef int (*fptr)() except *
84
    fptr = noexcept_func  # exception specifications are compatible
85
    return fptr()
86

87
def call_noexcept_func_except_check():
88
    """
89
    >>> call_noexcept_func_except_check()
90
    0
91
    """
92
    cdef int (*fptr)() except ?-1
93
    fptr = noexcept_func  # exception specifications are compatible
94
    return fptr()
95

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.