C-API implementation to use cython functions in pythran extensions
Created originally on Bitbucket by avmo (Ashwin Vishnu)
Turns out, cython only produces the __pyx_capi__
dictionary with api
modifier only when simple functions are defined, for eg.
cdef api int foo(int f):
return f
Even if I use the api
modifier in class methods, no C API is exported. Then one would need to write wrapper functions which take an instance of the class as an argument. Like this (a modified version of the example in http://docs.cython.org/en/latest/src/userguide/wrapping_CPlusPlus.html)
cdef api class PyRectangle [object cPyRectangle, type cPyRectangle_t]:
cdef Rectangle c_rect # hold a C++ instance which we're wrapping
def __cinit__(self, int x0, int y0, int x1, int y1):
self.c_rect = Rectangle(x0, y0, x1, y1)
cpdef api int get_area(self):
return self.c_rect.getArea()
def get_size(self):
cdef int width, height
self.c_rect.getSize(&width, &height)
return width, height
def move(self, dx, dy):
self.c_rect.move(dx, dy)
cdef api int getArea(Rectangle c_rect):
return c_rect.getArea()
cdef api int get_area_cy(PyRectangle py_rect):
return py_rect.get_area()
The problem with this method is that Pythran won't accept a class as a function parameter.