【发布时间】:2016-10-25 08:47:41
【问题描述】:
我正在使用 Cython 为 Python 的 C 库编写高级接口。
我有一个扩展类型A,它使用指向更复杂的C 上下文结构c_context 的指针来初始化库。指针保存在A 中。A 还有一个def 函数,该函数反过来创建另一个扩展类型B,使用库函数调用初始化另一个C 结构。在B 中进行的后续库调用需要此结构。B 需要来自A 的c_context 指针,它由我包装在扩展类型py_context 中,以便将其传递给@ 987654333@来自B:
#lib.pxd (C library definitions)
cdef extern from "lib.h":
ctypedef struct c_context:
pass
#file py_context.pxd
from lib cimport c_context
cdef class py_context:
cdef c_context *context
cdef create(cls, c_context *context)
cdef c_context* get(self)
#file py_context.pyx
def class py_context:
@staticmethod
cdef create(cls, c_context *c):
cls = py_nfc_context()
cls.context = c
return cls
cdef c_context* get(self):
return self.context
使用正确的 C 上下文传递包装器非常有效。
现在我需要再次从py_context 中取出C 结构并将其保存在B 中。我将cdef c_context get(self) 添加到py_context.pxd/pyx。
从 Bs __cinit__ 调用 py_context.get() 会导致:AttributeError: py_context object has no attribute get.
似乎我不知道什么时候在 Cython 中调用 cdef 函数。
所以我的问题是:再次从我的包装类中提取 C 结构的最佳方法是什么?
【问题讨论】: