From: Vitja Makarov Date: Fri, 3 Dec 2010 13:24:03 +0000 (+0300) Subject: Support lambda in class and cclass scope, ticket #605. X-Git-Tag: 0.14.alpha0~34 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3b16dc6375b81dc7b1057d1a6ac7d7985eba1191;p=cython.git Support lambda in class and cclass scope, ticket #605. --- diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 0cc03210..39d8b9dc 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -271,9 +271,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code = globalstate['all_the_rest'] self.generate_cached_builtins_decls(env, code) - # generate lambda function definitions - for node in env.lambda_defs: - node.generate_function_definitions(env, code) + self.generate_lambda_definitions(env, code) # generate normal function definitions self.body.generate_function_definitions(env, code) code.mark_pos(None) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index ae557f73..8a7cc5ca 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -321,7 +321,10 @@ class BlockNode(object): for entry in entries: code.globalstate.add_cached_builtin_decl(entry) del entries[:] - + + def generate_lambda_definitions(self, env, code): + for node in env.lambda_defs: + node.generate_function_definitions(env, code) class StatListNode(Node): # stats a list of StatNode @@ -1211,8 +1214,7 @@ class FuncDefNode(StatNode, BlockNode): # Generate closure function definitions self.body.generate_function_definitions(lenv, code) # generate lambda function definitions - for node in lenv.lambda_defs: - node.generate_function_definitions(lenv, code) + self.generate_lambda_definitions(lenv, code) is_getbuffer_slot = (self.entry.name == "__getbuffer__" and self.entry.scope.is_c_class_scope) @@ -3079,6 +3081,7 @@ class PyClassDefNode(ClassDefNode): self.target.analyse_target_expression(env, self.classobj) def generate_function_definitions(self, env, code): + self.generate_lambda_definitions(self.scope, code) self.body.generate_function_definitions(self.scope, code) def generate_execution_code(self, code): @@ -3241,6 +3244,7 @@ class CClassDefNode(ClassDefNode): self.body.analyse_expressions(scope) def generate_function_definitions(self, env, code): + self.generate_lambda_definitions(self.scope, code) if self.body: self.body.generate_function_definitions( self.entry.type.scope, code) diff --git a/tests/run/lambda_class_T605.pyx b/tests/run/lambda_class_T605.pyx new file mode 100644 index 00000000..eb3b8a52 --- /dev/null +++ b/tests/run/lambda_class_T605.pyx @@ -0,0 +1,36 @@ +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,)