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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
cdef int CHKERR(int ierr) except -1:
if ierr==0: return 0
raise RuntimeError
cdef int obj2int(object ob) except *:
return ob
def foo(a):
"""
>>> foo(0)
>>> foo(1)
Traceback (most recent call last):
RuntimeError
"""
cdef int i = obj2int(a)
CHKERR(i)
cdef int* except_expr(bint fire) except <int*>-1:
if fire:
raise RuntimeError
def test_except_expr(bint fire):
"""
>>> test_except_expr(False)
>>> test_except_expr(True)
Traceback (most recent call last):
...
RuntimeError
"""
except_expr(fire)
cdef double except_big_result(bint fire) except 100000000000000000000000000000000:
if fire:
raise RuntimeError
def test_except_big_result(bint fire):
"""
>>> test_except_big_result(False)
>>> test_except_big_result(True)
Traceback (most recent call last):
...
RuntimeError
"""
except_big_result(fire)
cdef unsigned short except_promotion_compare(bint fire) except *:
if fire:
raise RuntimeError
def test_except_promotion_compare(bint fire):
"""
>>> test_except_promotion_compare(False)
>>> test_except_promotion_compare(True)
Traceback (most recent call last):
...
RuntimeError
"""
except_promotion_compare(fire)
cdef int cdef_function_that_raises():
raise RuntimeError
cdef int cdef_noexcept_function_that_raises() noexcept:
raise RuntimeError
def test_except_raise_by_default():
"""
>>> test_except_raise_by_default()
Traceback (most recent call last):
...
RuntimeError
"""
cdef_function_that_raises()
def test_noexcept():
"""
>>> test_noexcept()
"""
cdef_noexcept_function_that_raises()
cdef int* cdef_ptr_func(int* input, int failure_mode):
# should have except NULL? by default
# failure mode is 0, 1, or 2
if failure_mode == 0:
return input # don't fail
elif failure_mode == 1:
return NULL # no exception
else:
raise RuntimeError("help!")
ctypedef int* (*cdef_ptr_func_ptr)(int*, int) except? NULL
def test_ptr_func(int failure_mode):
"""
>>> test_ptr_func(0)
100
>>> test_ptr_func(1)
NULL
>>> test_ptr_func(2)
exception
"""
# check that the signature is what we think it is
cdef cdef_ptr_func_ptr fptr = cdef_ptr_func
cdef int a = 100
try:
out = fptr(&a, failure_mode)
if out:
return out[0]
else:
print("NULL")
except RuntimeError:
print("exception")
def test_ptr_func2(int failure_mode):
"""
>>> test_ptr_func(0)
100
>>> test_ptr_func(1)
NULL
>>> test_ptr_func(2)
exception
"""
# as above, but don't go through a function pointer
cdef int a = 100
try:
out = cdef_ptr_func(&a, failure_mode)
if out:
return out[0]
else:
print("NULL")
except RuntimeError:
print("exception")
|