在Cython中创建PyCObject指针

F.X*_*.X. 6 c python cython scipy

一些SciPy函数(如scipy.ndimage.interpolation.geometric_transform)可以将指向C函数的指针作为参数,以避免在输入数组的每个点上调用Python可调用.

简而言之 :

  • my_function在C模块中定义一个名为somewhere 的函数
  • 返回一个PyCObject&my_function指针和(任选地)一个void*指针到围绕通过一些全局数据

相关的API方法是PyCObject_FromVoidPtrAndDesc,您可以在C中阅读扩展ndimage以查看它的实际效果.

我对使用Cython来保持我的代码更易于管理非常感兴趣,但我不确定我应该如何创建这样的对象.任何,好吧......指针?

Nik*_*kin 1

只需在 Cython 中执行与在 C 中执行的操作相同的操作,PyCObject_FromVoidPtrAndDesc直接调用即可。以下是移植到 Cython 的链接的示例:

###### example.pyx ######

from libc.stdlib cimport malloc, free
from 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
    for 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 is the function callable from python."""
    cdef double* shift_data = <double*>malloc(sizeof(shift))
    shift_data[0] = shift
    return PyCObject_FromVoidPtrAndDesc(&_shift_function,
                                        shift_data,
                                        &_shift_destructor)
Run Code Online (Sandbox Code Playgroud)

性能应与纯 C 版本相同。

请注意,Cyhton 需要运算符&来获取函数地址。此外,Cython 缺少指针取消引用运算符*,而是使用索引等效运算符 ( *ptr-> ptr[0])。