小编a.D*_*pel的帖子

如何使用Cython在C Struct周围编写完整的Python包装器?

我正在使用Cython为Python的C库编写一个高级接口.
我有一个扩展类型A,它使用指向更复杂的C上下文结构的指针初始化库c_context.指针保存在A.
A还有一个def函数,它又创建另一个扩展类型B,用库函数调用初始化另一个C结构.进行后续库调用需要此结构B.
B需要c_context指针从A其通过我的扩展类型内包裹py_context,以便将其传递到__cinit__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): …
Run Code Online (Sandbox Code Playgroud)

c python wrapper cython

9
推荐指数
1
解决办法
818
查看次数

标签 统计

c ×1

cython ×1

python ×1

wrapper ×1