From: Stefan Behnel Date: Fri, 15 Apr 2011 14:14:28 +0000 (+0200) Subject: make auto_cpdef play nicely with stararg functions X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f8735fb85d4c7d45aeed08487e8e14c2cc8e7d13;p=cython.git make auto_cpdef play nicely with stararg functions --- diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 280dc926..e92e97dc 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -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: diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 327f8e33..f99f6999 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -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) diff --git a/tests/run/auto_cpdef.py b/tests/run/auto_cpdef.py index bd6da412..97b8a76c 100644 --- a/tests/run/auto_cpdef.py +++ b/tests/run/auto_cpdef.py @@ -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']