Cython将扩展模块传递到to_py_call_code错误中的python reults



我刚刚开始熟悉Cython,试图将某些类从C 库包装到Python方法和类。我并不真正了解的是如何将这些扩展模块传递到Python世界中。

这是我的pyx文件中的代码段,它试图公开C 类和方法:

# distutils: language = c++
# distutils: sources = Demuxer.cpp, SharedUtil.cpp, ffmpeg_tpl.c, tpl.c
# distutils: libraries = spam eggs
# distutils: include_dirs = /opt/food/include
from cython.operator cimport dereference as deref
cdef extern from "Demuxer.h":
    cdef cppclass DemuxPkt:
        int streamID
        int tb_num
        int tb_den
        bint splitPoint     
        int ts
        int duration 
        unsigned char* data
        DemuxPkt()
    int initDemuxWithFileImpl(char*)
    int getNextChunkImpl(DemuxPkt*)

和以下是试图将它们包裹到Python World

的代码段
cdef class Demuxer:
    def __cinit__(self, fname, stream=None, length=None):
        #if stream is not None and length is not None:
         #   initDemuxWithFileImpl(stream, length)
        #else:
        initDemuxWithFileImpl(fname)
    cpdef getNextChunk(self):
        cdef DemuxPacket _dpkt = DemuxPacket()
        getNextChunkImpl(_dpkt._thisptr) # Is this correct??
        return _dpkt

cdef class DemuxPacket(object):
    """A packet of encoded data 
    """
    cdef DemuxPkt* _thisptr
    def __cinit__(self, flag):
        #if flag is _cinit_bypass_sentinel:
        #    return
        self._thisptr = new DemuxPkt()
        if self._thisptr == NULL:
            raise MemoryError()
    def __dealloc__(self):
        if self._thisptr != NULL:
            del self._thisptr
    def __call__(self):
        return deref(self._thisptr)()
    cpdef long getMillisecondsTs(self):
        return (self._thisptr.ts*self._thisptr.tb_num)/(self._thisptr.tb_den/1000)
    cpdef long getMillisecondsDuration(self):
        return (self._thisptr.duration*self.struct.tb_num)/(self._thisptr.tb_den/1000)

但是,当我运行Cython时,会出现以下错误:

AttributeError: 'ErrorType' object has no attribute 'to_py_call_code'

我不知道该消息,也不知道如何提前。我使用的Cython的版本为Ubuntu 14.0.4。

的0.25.2

任何建议都将受到赞赏!

预先感谢!

问题在__call__中。您尝试使用DemuxPkt::operator(),但尚未告诉Cython。将其添加到cppclass定义:

cdef extern from "Demuxer.h":
    cdef cppclass DemuxPkt:
        # .. everything else unchanged
        int operator()()

我猜测返回类型是int。显然,这可能不是真的,所以将其更改以匹配真实的返回类型。

相关内容

最新更新