Cython - 尝试访问指向结构体的指针内容时出错

3

我在Cython中有一个cdef类,它看起来非常类似于这个:

cdef class AprilTagDetector:
    cdef capriltag.apriltag_detector_t* _apriltag_detector

    def __cinit__(self):
        self._apriltag_detector = capriltag.apriltag_detector_create();
        # standard null checks
    # standard __dealloc__(self) here

    property quad_decimate:
        def __get__(self):
            return self._apriltag_detector.quad_decimate

相应的.pxd文件如下:

cdef extern from "apriltag.h":
    # The detector itself
    ctypedef struct apriltag_detector_t:
        pass

    # Detector constructor and destructor
    apriltag_detector_t* apriltag_detector_create()
    void apriltag_detector_destroy(apriltag_detector_t* td);

问题在于,当我尝试编译这段代码时,它会报错:
property quad_decimate:
    def __get__(self):
        return self._apriltag_detector.quad_decimate             ^
------------------------------------------------------------

apriltags.pyx:47:14: Cannot convert 'apriltag_detector_t *' to Python object

这里发生了什么事?我从Cython文档中无法弄清楚。
1个回答

1
我在一个黑客空间和朋友一起工作时,幸运地发现了这个项目中的问题。 问题出现在ctypedef struct apriltag_detector_t块中。 当我在块中写下pass时,我以为Cython会自动解析结构体的内部内容,并让我访问我所需要的元素-这里是quad_decimate
但实际情况并非如此。 要让Cython理解结构体的内容,你必须告诉它结构体中有什么,像这样:你必须
ctypedef struct apriltag_detector_t:
    float quad_decimate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接