Hi Russm,
On Fri, 28 Mar 2025 at 09:26, russm <
russ...@gmail.com> wrote:
> I'd rather autogenerate all these accessors instead of having to manually translate it all as boilerplate from C to python. I can get the list of fields from the cdata, but I don't see any way to know "this field is 4 bytes long and will be a python int" or whatever so I can decide what transform is appropriate. Is this possible?
From the ctype object that describes the struct type, the info is
found in the "fields" attribute. Example:
>>> ffi.cdef("struct s { int a; short b; };")
>>> ffi.typeof("struct s")
<ctype 'struct s'>
>>> ffi.typeof("struct s").fields
[('a', <_cffi_backend.CField object at 0x000001A1F01D38A0>), ('b',
<_cffi_backend.CField object at 0x000001A1F01D3720>)]
>>> ct = ffi.typeof("struct s").fields[1][1].type
>>> ct
<ctype 'short'>
>>> ct.kind
'primitive'
>>> ct.cname
'short'
At this point, you probably need to compare ct.cname to known strings
and check ffi.sizeof(ct). The exact kind of primitive type is not
directly exposed, so it could be integer, bool_t, floating-point,
complex number, or char/wchar_t.
Armin Rigo