equ*_*ghe 2 python pointers function-pointers cython
我有一个 C 头文件:
typedef struct
{
<normal members>
void (*cb_func)(glp_tree *T, void *info);
void *cb_info;
<normal members>
} glp_iocp;
Run Code Online (Sandbox Code Playgroud)
目前,在我的 pxd 文件中:
ctypedef struct IntOptCP "glp_iocp":
<normal members>
int out_dly # mip.out_dly (milliseconds)
#void (*cb_func)(Tree* tree, void* info)
# mip.cb_func
#void* cb_info # mip.cb_info
<normal members>
Run Code Online (Sandbox Code Playgroud)
在 pyx 文件中,在某些时候,我(基本上)这样做:
cdef class MyClass:
IntOptCP _iocp
<__cintit__ and the like>
def some_method(self):
<manipulation of self._iocp>
controls = dict()
controls = self._iocp
return controls
Run Code Online (Sandbox Code Playgroud)
这很好用。但是,现在我也希望公开cb_func和cb_info。这会破坏对控件的分配。我想要的是两种 python 对象类型(类?),一种 forcb_func和一种 for cb_info,它们的实例可以传递给结构体的cb_func和cb_info参数glp_iocp。
我已经阅读了https://github.com/cython/cython/tree/master/Demos/callback(并使用了 pycapsule),但是,我对 Cython 太缺乏经验/不熟悉,无法了解如何将这些信息用于我的具体案例。
因此,任何的帮助和指点如何(最好的)揭露cb_func和cb_info欢迎。
似乎您可以公开cb_func并cb_info执行类似于此玩具示例的操作:
import numpy as np
cimport numpy as np
ctypedef void (*f_type)(int, double*, double*)
ctypedef struct IntOptCP:
int a
double *b
double *c
f_type f
cdef class MyClass:
cdef IntOptCP _iocp
def exec_f(self):
self._iocp.f(self._iocp.a, self._iocp.b, self._iocp.c)
cdef void myfunc(int a, double *b, double *c):
cdef int i
for i in range(a):
b[i] += 1
c[i] += 1
def main():
cdef f_type f
cdef np.ndarray[np.float64_t, ndim=1] b, c
cdef int a
a = 100
b = np.zeros(a, dtype=np.float64)
c = np.zeros(a, dtype=np.float64)
test = MyClass()
test._iocp.a = a
test._iocp.b = &b[0]
test._iocp.c = &c[0]
test._iocp.f = myfunc
print 'before', a, b, c
test.exec_f()
print 'after', a, b, c
Run Code Online (Sandbox Code Playgroud)