|
| 1 | +# mode: run |
| 2 | +# tag: except |
| 3 | + |
| 4 | +# test the code object cache that is being used in exception raising |
| 5 | + |
| 6 | +### low level tests |
| 7 | + |
| 8 | +cdef extern from *: |
| 9 | + # evil hack to access the internal utility function |
| 10 | + ctypedef struct PyCodeObject |
| 11 | + ctypedef struct __Pyx_CodeObjectCacheEntry: |
| 12 | + int code_line |
| 13 | + PyCodeObject* code_object |
| 14 | + int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) |
| 15 | + |
| 16 | +def test_lowlevel_bisect2(*indices): |
| 17 | + """ |
| 18 | + >>> test_lowlevel_bisect2(1, 2, 3, 4, 5, 6) |
| 19 | + [0, 0, 1, 1, 2, 2] |
| 20 | + """ |
| 21 | + cdef __Pyx_CodeObjectCacheEntry* cache = [ |
| 22 | + __Pyx_CodeObjectCacheEntry(2, NULL), |
| 23 | + __Pyx_CodeObjectCacheEntry(4, NULL), |
| 24 | + ] |
| 25 | + return [ __pyx_bisect_code_objects(cache, 2, i) |
| 26 | + for i in indices ] |
| 27 | + |
| 28 | +def test_lowlevel_bisect5(*indices): |
| 29 | + """ |
| 30 | + >>> test_lowlevel_bisect5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) |
| 31 | + [0, 1, 2, 2, 2, 3, 3, 3, 4, 5, 5] |
| 32 | + """ |
| 33 | + cdef __Pyx_CodeObjectCacheEntry* cache = [ |
| 34 | + __Pyx_CodeObjectCacheEntry(1, NULL), |
| 35 | + __Pyx_CodeObjectCacheEntry(2, NULL), |
| 36 | + __Pyx_CodeObjectCacheEntry(5, NULL), |
| 37 | + __Pyx_CodeObjectCacheEntry(8, NULL), |
| 38 | + __Pyx_CodeObjectCacheEntry(9, NULL), |
| 39 | + ] |
| 40 | + return [ __pyx_bisect_code_objects(cache, 5, i) |
| 41 | + for i in indices ] |
| 42 | + |
| 43 | +def test_lowlevel_bisect6(*indices): |
| 44 | + """ |
| 45 | + >>> test_lowlevel_bisect6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) |
| 46 | + [0, 0, 1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6] |
| 47 | + """ |
| 48 | + cdef __Pyx_CodeObjectCacheEntry* cache = [ |
| 49 | + __Pyx_CodeObjectCacheEntry(2, NULL), |
| 50 | + __Pyx_CodeObjectCacheEntry(3, NULL), |
| 51 | + __Pyx_CodeObjectCacheEntry(6, NULL), |
| 52 | + __Pyx_CodeObjectCacheEntry(8, NULL), |
| 53 | + __Pyx_CodeObjectCacheEntry(9, NULL), |
| 54 | + __Pyx_CodeObjectCacheEntry(12, NULL), |
| 55 | + ] |
| 56 | + return [ __pyx_bisect_code_objects(cache, 6, i) |
| 57 | + for i in indices ] |
| 58 | + |
| 59 | +### Python level tests |
| 60 | + |
| 61 | +import sys |
| 62 | + |
| 63 | +def tb(): |
| 64 | + return sys.exc_info()[-1] |
| 65 | + |
| 66 | +def raise_keyerror(): |
| 67 | + raise KeyError |
| 68 | + |
| 69 | +def check_code_object_identity_recursively(tb1, tb2): |
| 70 | + if tb1 is None or tb2 is None: |
| 71 | + return |
| 72 | + code1, code2 = tb1.tb_frame.f_code, tb2.tb_frame.f_code |
| 73 | + if code1 is not code2: |
| 74 | + print('%s != %s' % (code1, code2)) |
| 75 | + check_code_object_identity_recursively(tb1.tb_next, tb2.tb_next) |
| 76 | + |
| 77 | +def assert_simple_code_object_reuse(): |
| 78 | + """ |
| 79 | + >>> try: assert_simple_code_object_reuse() |
| 80 | + ... except KeyError: t1 = tb() |
| 81 | + >>> try: assert_simple_code_object_reuse() |
| 82 | + ... except KeyError: t2 = tb() |
| 83 | + >>> check_code_object_identity_recursively(t1.tb_next, t2.tb_next) |
| 84 | + """ |
| 85 | + raise KeyError |
| 86 | + |
| 87 | +def assert_multi_step_code_object_reuse(recursions=0): |
| 88 | + """ |
| 89 | + >>> for depth in range(5): |
| 90 | + ... try: assert_multi_step_code_object_reuse(depth) |
| 91 | + ... except KeyError: t1 = tb() |
| 92 | + ... try: assert_multi_step_code_object_reuse(depth) |
| 93 | + ... except KeyError: t2 = tb() |
| 94 | + ... check_code_object_identity_recursively(t1.tb_next, t2.tb_next) |
| 95 | + """ |
| 96 | + if recursions: |
| 97 | + assert_multi_step_code_object_reuse(recursions-1) |
| 98 | + else: |
| 99 | + raise_keyerror() |
0 commit comments