python - Creating a PyCObject pointer in Cython -


a few scipy functions (like scipy.ndimage.interpolation.geometric_transform) can take pointers c functions arguments avoid having call python callable on each point of input array.

in nutshell :

  • define function called my_function somewhere in c module
  • return pycobject &my_function pointer , (optionally) void* pointer pass global data around

the related api method pycobject_fromvoidptranddesc, , can read extending ndimage in c see in action.

i interested in using cython keep code more manageable, i'm not sure how should create such object. any, well,... pointers?

just in cython same thing in c, call pycobject_fromvoidptranddesc directly. here example link ported cython:

###### example.pyx ######  libc.stdlib cimport malloc, free cpython.cobject cimport pycobject_fromvoidptranddesc  cdef int _shift_function(int *output_coordinates, double* input_coordinates,             int output_rank, int input_rank, double *shift_data):     cdef double shift = shift_data[0]     cdef int ii     ii in range(input_rank):         input_coordinates[ii] = output_coordinates[ii] - shift     return 1  cdef void _shift_destructor(void* cobject, void *shift_data):     free(shift_data)  def shift_function(double shift):     """this function callable python."""     cdef double* shift_data = <double*>malloc(sizeof(shift))     shift_data[0] = shift     return pycobject_fromvoidptranddesc(&_shift_function,                                         shift_data,                                         &_shift_destructor) 

performance should identical pure c version.

note cyhton requires operator & function address. also, cython lacks pointer dereference operator *, indexing equivalent used instead (*ptr -> ptr[0]).


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -