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
|
# mode: run
# tag: except
# test the code object cache that is being used in exception raising
### low level tests
cimport cython
cdef extern from *:
# evil hack to access the internal utility function
ctypedef struct PyCodeObject
ctypedef struct __Pyx_CodeObjectCacheEntry:
int code_line
PyCodeObject* code_object
int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line)
def test_lowlevel_bisect2(*indices):
"""
>>> test_lowlevel_bisect2(1, 2, 3, 4, 5, 6)
[0, 0, 1, 1, 2, 2]
"""
cdef __Pyx_CodeObjectCacheEntry* cache = [
__Pyx_CodeObjectCacheEntry(2, NULL),
__Pyx_CodeObjectCacheEntry(4, NULL),
]
return [ __pyx_bisect_code_objects(cache, 2, i)
for i in indices ]
def test_lowlevel_bisect5(*indices):
"""
>>> test_lowlevel_bisect5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
[0, 1, 2, 2, 2, 3, 3, 3, 4, 5, 5]
"""
cdef __Pyx_CodeObjectCacheEntry* cache = [
__Pyx_CodeObjectCacheEntry(1, NULL),
__Pyx_CodeObjectCacheEntry(2, NULL),
__Pyx_CodeObjectCacheEntry(5, NULL),
__Pyx_CodeObjectCacheEntry(8, NULL),
__Pyx_CodeObjectCacheEntry(9, NULL),
]
return [ __pyx_bisect_code_objects(cache, 5, i)
for i in indices ]
def test_lowlevel_bisect6(*indices):
"""
>>> test_lowlevel_bisect6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
[0, 0, 1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6]
"""
cdef __Pyx_CodeObjectCacheEntry* cache = [
__Pyx_CodeObjectCacheEntry(2, NULL),
__Pyx_CodeObjectCacheEntry(3, NULL),
__Pyx_CodeObjectCacheEntry(6, NULL),
__Pyx_CodeObjectCacheEntry(8, NULL),
__Pyx_CodeObjectCacheEntry(9, NULL),
__Pyx_CodeObjectCacheEntry(12, NULL),
]
return [ __pyx_bisect_code_objects(cache, 6, i)
for i in indices ]
### Python level tests
import sys
def tb():
return sys.exc_info()[-1]
def raise_keyerror():
raise KeyError
def check_code_object_identity_recursively(tb1, tb2):
if tb1 is None or tb2 is None:
return
code1, code2 = tb1.tb_frame.f_code, tb2.tb_frame.f_code
if code1 is not code2:
print('%s != %s' % (code1, code2))
check_code_object_identity_recursively(tb1.tb_next, tb2.tb_next)
def assert_simple_code_object_reuse():
"""
>>> try: assert_simple_code_object_reuse()
... except KeyError: t1 = tb()
>>> try: assert_simple_code_object_reuse()
... except KeyError: t2 = tb()
>>> check_code_object_identity_recursively(t1.tb_next, t2.tb_next)
"""
raise KeyError
def assert_multi_step_code_object_reuse(recursions=0):
"""
>>> for depth in range(5):
... try: assert_multi_step_code_object_reuse(depth)
... except KeyError: t1 = tb()
... try: assert_multi_step_code_object_reuse(depth)
... except KeyError: t2 = tb()
... check_code_object_identity_recursively(t1.tb_next, t2.tb_next)
"""
if recursions:
assert_multi_step_code_object_reuse(recursions-1)
else:
raise_keyerror()
def assert_simple_code_object_reuse_fused(cython.floating dummy):
"""
DISABLED: searching for code objects based on C lineno breaks for specializations
>> try: assert_simple_code_object_reuse_fused["float"](1.0)
... except KeyError: t1 = tb()
>> try: assert_simple_code_object_reuse_fused["double"](1.0)
... except KeyError: t2 = tb()
>> check_code_object_identity_recursively(t1.tb_next, t2.tb_next)
"""
raise KeyError
def assert_multi_step_code_object_reuse_fused(recursions=0, cython.floating dummy = 2.0):
"""
DISABLED: searching for code objects based on C lineno breaks for specializations
>> for depth in range(5):
... try: assert_multi_step_code_object_reuse_fused(depth, 1.0)
... except KeyError: t1 = tb()
... try: assert_multi_step_code_object_reuse_fused(depth, 1.0)
... except KeyError: t2 = tb()
... check_code_object_identity_recursively(t1.tb_next, t2.tb_next)
"""
if recursions:
assert_multi_step_code_object_reuse(recursions-1)
else:
raise_keyerror()
|