Commit 01bca828 authored by da-woods's avatar da-woods Committed by Stefan Behnel

Correctly generate function definions in finally clauses (GH-4652)

This creates two copies of the function, one for the exception
case and one for the non-exception case. It's probably inefficient
but the simplest solution.

Fixes https://github.com/cython/cython/issues/4651.
parent 3b721b32
......@@ -7702,6 +7702,8 @@ class TryFinallyStatNode(StatNode):
def generate_function_definitions(self, env, code):
self.body.generate_function_definitions(env, code)
self.finally_clause.generate_function_definitions(env, code)
if self.finally_except_clause:
self.finally_except_clause.generate_function_definitions(env, code)
def put_error_catcher(self, code, temps_to_clean_up, exc_vars,
exc_lineno_cnames=None, exc_filename_cname=None):
......
......@@ -549,3 +549,21 @@ def complex_finally_clause(x, obj):
del l[0], lobj[0]
assert all(i == 3 for i in l), l
return 99
def function_in_finally():
"""
https://github.com/cython/cython/issues/4651 - function definitions in the
except copy of the finally clause weren't generated
>>> function_in_finally()
in try
in func()
finished
"""
try:
print('in try')
finally:
def func():
print('in func()')
func()
print('finished')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment