make auto_cpdef play nicely with stararg functions
authorStefan Behnel <scoder@users.berlios.de>
Fri, 15 Apr 2011 14:14:28 +0000 (16:14 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Fri, 15 Apr 2011 14:14:28 +0000 (16:14 +0200)
Cython/Compiler/Nodes.py
Cython/Compiler/ParseTreeTransforms.py
tests/run/auto_cpdef.py

index 280dc926e93b0e785073247020d3cf3079ff4fe5..e92e97dc04c81ba01f3fbce8714754a55220fcca 100644 (file)
@@ -2032,6 +2032,17 @@ class DefNode(FuncDefNode):
                             api = False,
                             directive_locals = getattr(cfunc, 'directive_locals', {}))
 
+    def is_cdef_func_compatible(self):
+        """Determines if the function's signature is compatible with a
+        cdef function.  This can be used before calling
+        .as_cfunction() to see if that will be successful.
+        """
+        if self.needs_closure:
+            return False
+        if self.star_arg or self.starstar_arg:
+            return False
+        return True
+
     def analyse_declarations(self, env):
         self.is_classmethod = self.is_staticmethod = False
         if self.decorators:
index 327f8e331971cf918fe528024a45ff42ff0488df..f99f699988b606db73cf0431befbd7fef08eb083 100644 (file)
@@ -1425,9 +1425,8 @@ class AlignFunctionDefinitions(CythonTransform):
                     error(pxd_def.pos, "previous declaration here")
                 return None
             node = node.as_cfunction(pxd_def)
-        elif (self.scope.is_module_scope
-              and not node.needs_closure
-              and self.directives['auto_cpdef']):
+        elif (self.scope.is_module_scope and self.directives['auto_cpdef']
+              and node.is_cdef_func_compatible()):
             node = node.as_cfunction(scope=self.scope)
         # Enable this when nested cdef functions are allowed.
         # self.visitchildren(node)
index bd6da412f14040e77b174d3951d14134b560daa0..97b8a76c588bf199e83fd0446e89a4fd1c4b8657 100644 (file)
@@ -22,3 +22,18 @@ def call_str(arg):
     STR
     """
     return str(arg)
+
+
+def stararg_func(*args):
+    """
+    >>> stararg_func(1, 2)
+    (1, 2)
+    """
+    return args
+
+def starstararg_func(**kwargs):
+    """
+    >>> starstararg_func(a=1)
+    1
+    """
+    return kwargs['a']