Commit 3b16dc63 authored by Vitja Makarov's avatar Vitja Makarov

Support lambda in class and cclass scope, ticket #605.

parent eea8ce91
...@@ -271,9 +271,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -271,9 +271,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code = globalstate['all_the_rest'] code = globalstate['all_the_rest']
self.generate_cached_builtins_decls(env, code) self.generate_cached_builtins_decls(env, code)
# generate lambda function definitions self.generate_lambda_definitions(env, code)
for node in env.lambda_defs:
node.generate_function_definitions(env, code)
# generate normal function definitions # generate normal function definitions
self.body.generate_function_definitions(env, code) self.body.generate_function_definitions(env, code)
code.mark_pos(None) code.mark_pos(None)
......
...@@ -321,7 +321,10 @@ class BlockNode(object): ...@@ -321,7 +321,10 @@ class BlockNode(object):
for entry in entries: for entry in entries:
code.globalstate.add_cached_builtin_decl(entry) code.globalstate.add_cached_builtin_decl(entry)
del entries[:] del entries[:]
def generate_lambda_definitions(self, env, code):
for node in env.lambda_defs:
node.generate_function_definitions(env, code)
class StatListNode(Node): class StatListNode(Node):
# stats a list of StatNode # stats a list of StatNode
...@@ -1211,8 +1214,7 @@ class FuncDefNode(StatNode, BlockNode): ...@@ -1211,8 +1214,7 @@ class FuncDefNode(StatNode, BlockNode):
# Generate closure function definitions # Generate closure function definitions
self.body.generate_function_definitions(lenv, code) self.body.generate_function_definitions(lenv, code)
# generate lambda function definitions # generate lambda function definitions
for node in lenv.lambda_defs: self.generate_lambda_definitions(lenv, code)
node.generate_function_definitions(lenv, code)
is_getbuffer_slot = (self.entry.name == "__getbuffer__" and is_getbuffer_slot = (self.entry.name == "__getbuffer__" and
self.entry.scope.is_c_class_scope) self.entry.scope.is_c_class_scope)
...@@ -3079,6 +3081,7 @@ class PyClassDefNode(ClassDefNode): ...@@ -3079,6 +3081,7 @@ class PyClassDefNode(ClassDefNode):
self.target.analyse_target_expression(env, self.classobj) self.target.analyse_target_expression(env, self.classobj)
def generate_function_definitions(self, env, code): def generate_function_definitions(self, env, code):
self.generate_lambda_definitions(self.scope, code)
self.body.generate_function_definitions(self.scope, code) self.body.generate_function_definitions(self.scope, code)
def generate_execution_code(self, code): def generate_execution_code(self, code):
...@@ -3241,6 +3244,7 @@ class CClassDefNode(ClassDefNode): ...@@ -3241,6 +3244,7 @@ class CClassDefNode(ClassDefNode):
self.body.analyse_expressions(scope) self.body.analyse_expressions(scope)
def generate_function_definitions(self, env, code): def generate_function_definitions(self, env, code):
self.generate_lambda_definitions(self.scope, code)
if self.body: if self.body:
self.body.generate_function_definitions( self.body.generate_function_definitions(
self.entry.type.scope, code) self.entry.type.scope, code)
......
cdef int cdef_CONST = 123
CONST = 456
cdef class Foo:
"""
>>> obj = Foo()
>>> obj.id(123)
123
>>> obj.cconst_mul(1)
123
>>> obj.const_mul(1)
456
>>> obj.foo[0](1)
1
"""
id = lambda self, x: x
cconst_mul = lambda self, x: x * cdef_CONST
const_mul = lambda self, x: x * CONST
foo = (lambda x:x,)
class Bar:
"""
>>> obj = Bar()
>>> obj.id(123)
123
>>> obj.cconst_mul(1)
123
>>> obj.const_mul(1)
456
>>> obj.foo[0](1)
1
"""
id = lambda self, x: x
cconst_mul = lambda self, x: x * cdef_CONST
const_mul = lambda self, x: x * CONST
foo = (lambda x:x,)
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