Support lambda in class and cclass scope, ticket #605.
authorVitja Makarov <vitja.makarov@gmail.com>
Fri, 3 Dec 2010 13:24:03 +0000 (16:24 +0300)
committerVitja Makarov <vitja.makarov@gmail.com>
Fri, 3 Dec 2010 13:24:03 +0000 (16:24 +0300)
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py
tests/run/lambda_class_T605.pyx [new file with mode: 0644]

index 0cc0321051be10fd58b2c69ceaa10a3f0b14cadb..39d8b9dc5b6d5be92a96f95c42af3084b4beca99 100644 (file)
@@ -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)
index ae557f734424a6d1f4b7de3dc98d1a3591333caf..8a7cc5ca4fc5dadcbf2f4efe1d2a960c2728a13c 100644 (file)
@@ -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 (file)
index 0000000..eb3b8a5
--- /dev/null
@@ -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,)