我有一些来自已经分配和初始化的结构的数据。我可以保证在这些对象的任何生命周期内都不会释放数据。如何将其包装在 Cython 的 Python 对象中?以下内容不起作用,但我希望它能解释我的意图:
from libc.stdlib cimport malloc
ctypedef struct Point:
int x
int y
cdef class _MyWrapper:
cdef Point* foo
def __cinit__(self, Point* foo):
self.foo = foo
def create_eternal_MyWrapper(int x, int y):
cdef Point* p
p = <Point*>malloc(sizeof(Point))
p.x = x
p.y = y
return _MyWrapper(p)
在此运行 cython 的输出:
Error compiling Cython file:
------------------------------------------------------------
...
def create_eternal_MyWrapper(int x, int y):
cdef Point* p
p = <Point*>malloc(sizeof(Point))
p.x = x
p.y = y
return _MyWrapper(p)
^
------------------------------------------------------------
examplecy.pyx:17:23: Cannot convert 'Point *' to Python object