free函数的Cython编译错误(无法将Python对象参数转换为“FooBar*”类型)

2024-06-07 16:47:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Cython(0.15.2)为Python(2.6.5)创建一个扩展。我已经创建了一个pxd文件和一个pyx文件。以下是我的pyx文件的内容:

cimport capifuncs

cdef class myArray:
    cdef capifuncs.myArray *_my_array
    def __cinit__(self, size):
        self._my_array = capifuncs.array_new(size)
        if (self._my_array is NULL):
            raise MemoryError()

    def __dealloc__(self):
        if self._my_array is not NULL:
            capifuncs.array_free(self._my_array)

    def __bool__(self):
        return not capifuncs.IsEmpty(self._my_array)


    ##############################
    #        Array methods       #
    ##############################

    cpdef getItem(self, unsigned int pos):
        if capifuncs.IsEmpty(self._my_array):
            raise IndexError("Array is empty")
        #if ():
        #    raise IndexError("Array bounds exceeded")

        return capifuncs.array_get_item(self._my_array, pos)


    cpdef setItem(self, unsigned int pos, double val):
        if capifuncs.IsEmpty(self._my_array):
            raise IndexError("Array is empty")
        #if ():
        #    raise IndexError("Array bounds exceeded")

        capifuncs.array_set_item(self._my_array, pos, val)




# Free functions
cpdef long someCAPIFuncCall(capifuncs.FooBar *fb, capifuncs.myArray *arr, long start, long stop):
    return capifuncs.doSomethingInteresting(fb, arr, start, stop)

如果我注释掉free(即非成员)函数定义语句,代码将正确编译并生成扩展。但是,如果我取消注释并尝试编译该文件,则会收到以下错误消息:

cafuncs.pyx:64:23: Cannot convert Python object argument to type 'FooBar *'

这是什么原因,我该如何解决?


Tags: 文件posselfreturnifismydef

热门问题