我有一个名为ctcorrgen的类,它正在进行一些数字处理,并通过指向内部数组的常量指针一次返回一行结果。我想将这个内部数组包装成一个只读Numpy数组并返回它,如下所示:
static inline PyObject* ctcorrgen_yield_row(object &object) {
// extract corrgen base
ctcorrgen &corrgen = extract<ctcorrgen&>(object);
// get row of data
const cfloat* row = corrgen.yield_row();
if (row == nullptr) {
return detail::none();
} else {
// build read-only array around data
npy_intp len = corrgen.framesize();
return PyArray_New(
&PyArray_Type, 1, &len, NPY_COMPLEX64, NULL, (void*)row, 0,
NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED, NULL
);
}
}
我的问题是,如何设置新的array对象引用它所创建的对象,以便在处理完数组之前它不会被垃圾收集(从而破坏底层缓冲区)?我使用的是boost::python,但我怀疑这需要比我更直接地了解pythonc-API。你知道吗
好吧,我想我找到了答案,对任何在我之后的人来说。你知道吗
查看PyArrayObject的定义:
关键是base pointer:
结合pyu New的这一段:
因此,我认为这样的方法是建立所需关系的合适方法(对于Numpy>;=1.7)
相关问题 更多 >
编程相关推荐