如何通过ctypes对python中包含对它的定义的引用进行建模?
尝试将struct包含在它的定义中,如下所示
foo.c的
typedef struct Foo { struct Foo *foo; } Foo;
例如,如何建模
foo.py
class Foo(Structure): _fields_ = [('foo', pointer(Foo))]
当然python没有解释,我可以使用c_void_p而不是指针(Foo),并将其值转换为如下
F = clib.get_foo() cast(F.foo, pointer(Foo)) #although i'm not sure if it would work
但是,有没有办法在python类中对该结构进行建模?
从[Python]:不完整的类型 :
…… 在ctypes中 ,我们可以定义
cell
类,并在类语句之后设置_fields_
属性。
如果我们将其应用于当前问题,代码将看起来像:
from ctypes import Structure, POINTER class Foo(Structure): pass Foo._fields_ = [ ("foo_ptr", POINTER(Foo)), ]