Commit 175f08eb authored by Vitja Makarov's avatar Vitja Makarov

Support lambda in module scope #603

parent 8dca292a
...@@ -2215,6 +2215,8 @@ class DefNode(FuncDefNode): ...@@ -2215,6 +2215,8 @@ class DefNode(FuncDefNode):
def needs_assignment_synthesis(self, env, code=None): def needs_assignment_synthesis(self, env, code=None):
# Should enable for module level as well, that will require more testing... # Should enable for module level as well, that will require more testing...
if self.entry.is_lambda:
return True
if env.is_module_scope: if env.is_module_scope:
if code is None: if code is None:
return env.directives['binding'] return env.directives['binding']
......
...@@ -181,9 +181,6 @@ class PostParse(ScopeTrackingTransform): ...@@ -181,9 +181,6 @@ class PostParse(ScopeTrackingTransform):
def visit_LambdaNode(self, node): def visit_LambdaNode(self, node):
# unpack a lambda expression into the corresponding DefNode # unpack a lambda expression into the corresponding DefNode
if self.scope_type != 'function':
error(node.pos,
"lambda functions are currently only supported in functions")
lambda_id = self.lambda_counter lambda_id = self.lambda_counter
self.lambda_counter += 1 self.lambda_counter += 1
node.lambda_name = EncodedString(u'lambda%d' % lambda_id) node.lambda_name = EncodedString(u'lambda%d' % lambda_id)
...@@ -1366,7 +1363,7 @@ class CreateClosureClasses(CythonTransform): ...@@ -1366,7 +1363,7 @@ class CreateClosureClasses(CythonTransform):
while cscope.is_py_class_scope or cscope.is_c_class_scope: while cscope.is_py_class_scope or cscope.is_c_class_scope:
cscope = cscope.outer_scope cscope = cscope.outer_scope
if not from_closure and self.path: if not from_closure and (self.path or inner_node):
if not inner_node: if not inner_node:
if not node.assmt: if not node.assmt:
raise InternalError, "DefNode does not have assignment node" raise InternalError, "DefNode does not have assignment node"
......
...@@ -75,6 +75,7 @@ class Entry(object): ...@@ -75,6 +75,7 @@ class Entry(object):
# is_cfunction boolean Is a C function # is_cfunction boolean Is a C function
# is_cmethod boolean Is a C method of an extension type # is_cmethod boolean Is a C method of an extension type
# is_unbound_cmethod boolean Is an unbound C method of an extension type # is_unbound_cmethod boolean Is an unbound C method of an extension type
# is_lambda boolean Is a lambda function
# is_type boolean Is a type definition # is_type boolean Is a type definition
# is_cclass boolean Is an extension class # is_cclass boolean Is an extension class
# is_cpp_class boolean Is a C++ class # is_cpp_class boolean Is a C++ class
...@@ -137,6 +138,7 @@ class Entry(object): ...@@ -137,6 +138,7 @@ class Entry(object):
is_cfunction = 0 is_cfunction = 0
is_cmethod = 0 is_cmethod = 0
is_unbound_cmethod = 0 is_unbound_cmethod = 0
is_lambda = 0
is_type = 0 is_type = 0
is_cclass = 0 is_cclass = 0
is_cpp_class = 0 is_cpp_class = 0
...@@ -530,7 +532,7 @@ class Scope(object): ...@@ -530,7 +532,7 @@ class Scope(object):
entry.name = EncodedString(func_cname) entry.name = EncodedString(func_cname)
entry.func_cname = func_cname entry.func_cname = func_cname
entry.signature = pyfunction_signature entry.signature = pyfunction_signature
self.pyfunc_entries.append(entry) entry.is_lambda = True
return entry return entry
def add_lambda_def(self, def_node): def add_lambda_def(self, def_node):
......
# Module scope lambda functions
__doc__ = """
>>> pow2(16)
256
>>> with_closure(0)
0
>>> typed_lambda(1)(2)
3
>>> typed_lambda(1.5)(1.5)
2
>>> cdef_const_lambda()
123
>>> const_lambda()
321
"""
pow2 = lambda x: x * x
with_closure = lambda x:(lambda: x)()
typed_lambda = lambda int x : (lambda int y: x + y)
cdef int xxx = 123
cdef_const_lambda = lambda: xxx
yyy = 321
const_lambda = lambda: yyy
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