Commit c326b2da authored by Haoyu Bai's avatar Haoyu Bai

@cython.ccall decorator

parent 0a81c329
...@@ -96,6 +96,7 @@ directive_types = { ...@@ -96,6 +96,7 @@ directive_types = {
'internal' : bool, # cdef class visibility in the module dict 'internal' : bool, # cdef class visibility in the module dict
'infer_types' : bool, # values can be True/None/False 'infer_types' : bool, # values can be True/None/False
'cfunc' : None, # decorators do not take directive value 'cfunc' : None, # decorators do not take directive value
'ccall' : None,
'cclass' : None, 'cclass' : None,
} }
......
...@@ -1401,6 +1401,9 @@ class AdjustDefByDirectives(CythonTransform, SkipDeclarations): ...@@ -1401,6 +1401,9 @@ class AdjustDefByDirectives(CythonTransform, SkipDeclarations):
return node return node
def visit_DefNode(self, node): def visit_DefNode(self, node):
if 'ccall' in self.directives:
node = node.as_cfunction(overridable=True)
return self.visit(node)
if 'cfunc' in self.directives: if 'cfunc' in self.directives:
if self.in_py_class: if self.in_py_class:
error(node.pos, "cfunc directive is not allowed here") error(node.pos, "cfunc directive is not allowed here")
......
...@@ -23,7 +23,7 @@ class _EmptyDecoratorAndManager(object): ...@@ -23,7 +23,7 @@ class _EmptyDecoratorAndManager(object):
def __exit__(self, exc_type, exc_value, traceback): def __exit__(self, exc_type, exc_value, traceback):
pass pass
cclass = cfunc = _EmptyDecoratorAndManager() cclass = ccall = cfunc = _EmptyDecoratorAndManager()
def inline(f, *args, **kwds): def inline(f, *args, **kwds):
if isinstance(f, basestring): if isinstance(f, basestring):
......
import cython import cython
from cython import cfunc, cclass from cython import cfunc, cclass, ccall
@cython.test_assert_path_exists('//CFuncDefNode') @cython.test_assert_path_exists('//CFuncDefNode')
@cython.cfunc @cython.cfunc
...@@ -75,3 +75,38 @@ def test_method(): ...@@ -75,3 +75,38 @@ def test_method():
else: else:
print(True) print(True)
return return
@cython.ccall
def ccall_sqr(x):
return x*x
@cclass
class Overidable(object):
@ccall
def meth(self):
return 0
def test_ccall():
"""
>>> test_ccall()
25
>>> ccall_sqr(5)
25
"""
return ccall_sqr(5)
def test_ccall_method(x):
"""
>>> test_ccall_method(Overidable())
0
>>> Overidable().meth()
0
>>> class Foo(Overidable):
... def meth(self):
... return 1
>>> test_ccall_method(Foo())
1
>>> Foo().meth()
1
"""
return x.meth()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment