Commit c4e00da2 authored by Stefan Behnel's avatar Stefan Behnel

prevent default arguments from unnecessarily ending up in closures

parent edfebe98
......@@ -361,6 +361,18 @@ class EnvTransform(CythonTransform):
self.visitchildren(node)
return node
def visit_CArgDeclNode(self, node):
# default arguments are evaluated in the outer scope
if node.default:
attrs = [ attr for attr in node.child_attrs if attr != 'default' ]
self.visitchildren(node, attrs)
self.env_stack.append((node, self.current_env().outer_scope))
self.visitchildren(node, ('default',))
self.env_stack.pop()
else:
self.visitchildren(node)
return node
class MethodDispatcherTransform(EnvTransform):
"""
......
# mode: run
# tag: closures
cimport cython
@cython.test_fail_if_path_exists(
'//NameNode[@entry.in_closure = True]',
'//NameNode[@entry.from_closure = True]')
def test_func_default():
"""
>>> func = test_func_default()
>>> func()
1
>>> func(2)
2
"""
def default():
return 1
def func(arg=default()):
return arg
return func
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