4

我有一些来自已经分配和初始化的结构的数据。我可以保证在这些对象的任何生命周期内都不会释放数据。如何将其包装在 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
4

1 回答 1

0

正如这里所讨论的,__init____cinit__方法都使用PyObject_CallAPI 函数,它只能接受 PyObject 类型的参数。因此,正如FAQ中所建议的,您应该在全局工厂方法中初始化 C 属性:

from libc.stdlib cimport malloc

ctypedef struct Point:
    int x
    int y

cdef class _MyWrapper:
    cdef Point* fooless
    def __init__(self, *args, **kwargs):
        raise TypeError("This class cannot be instantiated from Python")

cpdef _MyWrapper create_MyWrapper(int x, int y):
    cdef _MyWrapper w = _MyWrapper.__new__(_MyWrapper)

    cdef Point* p
    p = <Point*>malloc(sizeof(Point))
    p.x = x
    p.y = y

    w.foo = p

    # initialize all other fields explicitly
    # ...

    return w

当然可以自己创建一个专用的初始化方法_MyWrapper,但我认为这会相当不安全,因为用户可能会在类实例化后忘记调用此类方法。

PS:很高兴看看是否存在更简单的解决方案

于 2013-03-11T01:53:00.950 回答