cython

changeset 1351:ef9d2c680684 cython-0.10.1

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 fa14f80b335c
children 60b249ff81eb
files Cython/Compiler/Nodes.py tests/run/exceptionrefcount.pyx
line diff
1.1 --- a/Cython/Compiler/Nodes.py Mon Nov 10 14:15:31 2008 +0100 1.2 +++ b/Cython/Compiler/Nodes.py Mon Nov 17 14:35:05 2008 +0100 1.3 @@ -3882,6 +3882,8 @@ 1.4 self.else_clause.generate_execution_code(code) 1.5 code.putln( 1.6 "}") 1.7 + for var in Naming.exc_save_vars: 1.8 + code.put_xdecref_clear(var, py_object_type) 1.9 code.put_goto(try_end_label) 1.10 code.put_label(our_error_label) 1.11 code.put_var_xdecrefs_clear(self.cleanup_list)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/run/exceptionrefcount.pyx Mon Nov 17 14:35:05 2008 +0100 2.3 @@ -0,0 +1,55 @@ 2.4 +__doc__ = u""" 2.5 +>>> class SampleException(Exception): pass 2.6 +>>> import sys 2.7 + 2.8 +>>> def assert_refcount(rc1, rc2, func): 2.9 +... # test ref-counts, but allow a bit of freedom 2.10 +... assert rc2 <= rc1 + 4, "%s, before: %d, after %d" % ( 2.11 +... func.__name__, rc1, rc2) 2.12 + 2.13 +>>> def run_test(repeat, test_func): 2.14 +... initial_refcount = sys.getrefcount(SampleException) 2.15 +... for i in range(repeat): 2.16 +... try: raise SampleException 2.17 +... except: 2.18 +... refcount1 = sys.getrefcount(SampleException) 2.19 +... test_func() 2.20 +... refcount2 = sys.getrefcount(SampleException) 2.21 +... 2.22 +... assert_refcount(refcount1, refcount2, test_func) 2.23 +... assert_refcount(initial_refcount, refcount2, test_func) 2.24 +... refcount3 = sys.getrefcount(SampleException) 2.25 +... assert_refcount(refcount1, refcount3, test_func) 2.26 +... assert_refcount(initial_refcount, refcount3, test_func) 2.27 + 2.28 +>>> run_test(50, test_no_exception_else) 2.29 +>>> run_test(50, test_no_exception) 2.30 +>>> run_test(50, test_exception) 2.31 +>>> run_test(50, test_finally) 2.32 +""" 2.33 + 2.34 +def test_no_exception(): 2.35 + try: 2.36 + a = 1+1 2.37 + except: 2.38 + pass 2.39 + 2.40 +def test_no_exception_else(): 2.41 + try: 2.42 + a = 1+1 2.43 + except: 2.44 + pass 2.45 + else: 2.46 + b = 1+1 2.47 + 2.48 +def test_exception(): 2.49 + try: 2.50 + raise TypeError 2.51 + except: 2.52 + pass 2.53 + 2.54 +def test_finally(): 2.55 + try: 2.56 + a = 1+1 2.57 + finally: 2.58 + b = 1+1