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

Cython - error when trying to access contents of pointer to struct

我在 Cython 中有一个 cdefed class,它看起来与此非常相似:

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 文档中弄清楚。

谢天谢地,我在与一个黑客空间的朋友一起做这个项目时解决了这个问题。 问题出在 ctypedef struct apriltag_detector_t 块中。 当我在块中写 pass 时,我认为 Cython 会自动计算出结构的内部内容,并让我访问我需要的元素 - 这里,quad_decimate.

不是这样。 要让 Cython 理解结构的内容,您 必须 告诉它结构中的内容:

ctypedef struct apriltag_detector_t:
    float quad_decimate