4
>>> class SampleException(Exception): pass
6
>>> def assert_refcount(rc1, rc2, func):
7
... # test ref-counts, but allow a bit of freedom
8
... assert rc2 <= rc1 + 4, "%s, before: %d, after %d" % (
9
... func.__name__, rc1, rc2)
11
>>> def run_test(repeat, test_func):
12
... initial_refcount = get_refcount(SampleException)
13
... for i in range(repeat):
14
... try: raise SampleException
16
... refcount1 = get_refcount(SampleException)
18
... refcount2 = get_refcount(SampleException)
20
... assert_refcount(refcount1, refcount2, test_func)
21
... assert_refcount(initial_refcount, refcount2, test_func)
22
... refcount3 = get_refcount(SampleException)
23
... assert_refcount(refcount1, refcount3, test_func)
24
... assert_refcount(initial_refcount, refcount3, test_func)
26
>>> run_test(50, test_no_exception_else)
27
>>> run_test(50, test_no_exception)
28
>>> run_test(50, test_exception)
29
>>> run_test(50, test_finally)
33
from cpython.ref cimport PyObject, Py_REFCNT
36
@cython.always_allow_keywords(False)
40
def test_no_exception():
46
def test_no_exception_else():