trying to wrap struct the reference it’s definition as below
foo.c
JavaScript
x
4
1
typedef struct Foo {
2
struct Foo *foo;
3
} Foo;
4
how to model that, for example
foo.py
JavaScript
1
3
1
class Foo(Structure):
2
_fields_ = [('foo', pointer(Foo))]
3
of course python doesn’t interpret that, I could use c_void_p instead of pointer(Foo), and cast it’s value as follow
JavaScript
1
3
1
F = clib.get_foo()
2
cast(F.foo, pointer(Foo)) #although i'm not sure if it would work
3
but, is there a way to model that struct in a python class?
Advertisement
Answer
From [Python.Docs]: ctypes – Incomplete Types:
… . In ctypes, we can define the
cell
class and and set the_fields_
attribute later, after the class statement.
Applying that to the current problem, the code would look smth like:
JavaScript
1
10
10
1
import ctypes as ct
2
3
4
class Foo(ct.Structure):
5
pass
6
7
Foo._fields_ = (
8
("foo_ptr", ct.POINTER(Foo)),
9
)
10