Skip to content

Commit

Permalink
Fixes CodeWriter for cdef functions aka. CFuncDefNode.
Browse files Browse the repository at this point in the history
Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
  • Loading branch information
sighingnow committed Apr 14, 2020
1 parent ce0806e commit 8e1e586
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
32 changes: 30 additions & 2 deletions Cython/CodeWriter.py
Expand Up @@ -136,7 +136,8 @@ def visit_CSimpleBaseTypeNode(self, node):
self.put("short " * -node.longness)
elif node.longness > 0:
self.put("long " * node.longness)
self.put(node.name)
if node.name is not None:
self.put(node.name)

def visit_CComplexBaseTypeNode(self, node):
self.put(u'(')
Expand Down Expand Up @@ -247,10 +248,37 @@ def visit_FuncDefNode(self, node):
self.visit(node.body)
self.dedent()

def visit_CFuncDefNode(self, node):
if 'inline' in node.modifiers:
return
if node.overridable:
self.startline(u'cpdef ')
else:
self.startline(u'cdef ')
if node.visibility != 'private':
self.put(node.visibility)
self.put(u' ')
if node.api:
self.put(u'api ')

if node.base_type:
self.visit(node.base_type)
if node.base_type.name is not None:
self.put(u' ')

# visit the CFuncDeclaratorNode, but put a `:` at the end of line
self.visit(node.declarator.base)
self.put(u'(')
self.comma_separated_list(node.declarator.args)
self.endline(u'):')

self.indent()
self.visit(node.body)
self.dedent()

def visit_CArgDeclNode(self, node):
if node.base_type.name is not None:
self.visit(node.base_type)
self.put(u" ")
self.visit(node.declarator)
if node.default is not None:
self.put(u" = ")
Expand Down
8 changes: 8 additions & 0 deletions Cython/Tests/TestCodeWriter.py
Expand Up @@ -46,6 +46,14 @@ def f(x = 34, y = 54, z):
pass
""")

def test_cdef(self):
self.t(u"""
cdef f(x, y, z):
pass
cdef public void (x = 34, y = 54, z):
pass
""")

def test_longness_and_signedness(self):
self.t(u"def f(unsigned long long long long long int y):\n pass")

Expand Down

0 comments on commit 8e1e586

Please sign in to comment.