Cython has moved to github.
cython
view tests/run/exceptionrefcount.pyx @ 1351:ef9d2c680684
fixed ref-count bug in try-except handling
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Mon Nov 17 14:35:05 2008 +0100 (3 years ago) |
| parents | |
| children |
line source
1 __doc__ = u"""
2 >>> class SampleException(Exception): pass
3 >>> import sys
5 >>> def assert_refcount(rc1, rc2, func):
6 ... # test ref-counts, but allow a bit of freedom
7 ... assert rc2 <= rc1 + 4, "%s, before: %d, after %d" % (
8 ... func.__name__, rc1, rc2)
10 >>> def run_test(repeat, test_func):
11 ... initial_refcount = sys.getrefcount(SampleException)
12 ... for i in range(repeat):
13 ... try: raise SampleException
14 ... except:
15 ... refcount1 = sys.getrefcount(SampleException)
16 ... test_func()
17 ... refcount2 = sys.getrefcount(SampleException)
18 ...
19 ... assert_refcount(refcount1, refcount2, test_func)
20 ... assert_refcount(initial_refcount, refcount2, test_func)
21 ... refcount3 = sys.getrefcount(SampleException)
22 ... assert_refcount(refcount1, refcount3, test_func)
23 ... assert_refcount(initial_refcount, refcount3, test_func)
25 >>> run_test(50, test_no_exception_else)
26 >>> run_test(50, test_no_exception)
27 >>> run_test(50, test_exception)
28 >>> run_test(50, test_finally)
29 """
31 def test_no_exception():
32 try:
33 a = 1+1
34 except:
35 pass
37 def test_no_exception_else():
38 try:
39 a = 1+1
40 except:
41 pass
42 else:
43 b = 1+1
45 def test_exception():
46 try:
47 raise TypeError
48 except:
49 pass
51 def test_finally():
52 try:
53 a = 1+1
54 finally:
55 b = 1+1
