working with ctypes and complex data structures
Michael Felt
michael at felt.demon.nl
Wed Oct 5 17:03:01 EDT 2016
On 05-Oct-16 22:29, Emile van Sebille wrote:
Thanks for the reply!
After a shirt coffeebreak - back into the fray - and I found the following:
> +76 class cpu_total:
> +77 def __init__(self):
> +78 __perfstat__ = CDLL("libperfstat.a(shr_64.o)")
> +79 prototype = CFUNCTYPE(c_int, c_void_p, c_void_p, c_int,
> c_int)
error #2 - see below
> +80 args = (1, "name", None), (2, "buff", None), (1, "size",
> 0), (1, "count", 1)
error #1. paramater type 2 (the buffer might be where data is being put,
but for the call, the pointer is INPUT)
> +81 cpu_total = prototype(("perfstat_cpu_total",
> __perfstat__), args)
> +82
> +83 buff = perfstat_cpu_total_t()
> +84 cpu_total(buff=buff, size=sizeof(buff))
> +85
The error #2 is #2, because only after I corrected the inputs did I get
Type Exceptions. I had hoped that c_void_p was a "typeless" type, mainly
the right size, but the ctypes interface is more particular when you use
the CFUNCTYPE() - so now I have my boilerplate for what I hope
can be a tutorial for interfacing - by hand - with complex functions,
i.e., using one class as the data aka typedef defintion, and a second
class to work
on it.
In template form:
class perfstat_xxx_t:
_fields_ = ( ... )
class perfstat_xxx:
def init(self):
_perflib = ctypes.CDLL("libperfstat.a(shr_64.o)") # AIX member
in an archive
_fn_xxx = _perflib.xxx
# ALL AIX perfstat routines have 4 arguments: (name, buff,
sizeof_buff, count)
# the buff is a pointer to where the information will be stored
_fn = CFUNCTYPE(c_int, c_void_p, POINTER(xxx_t), c_int, c_int)
_args = (1, "name", None), (1, "buff", None), (1, "size", 0),
(1, "count", 1)
_xxx = _fn(("xxx", _fn_xxx), _args)
_buff = perfstat_cpu_total_t()
_xxx(buff=_buff, size=sizeof(_buff))
self._v = _buff
So, the next question/test will be to move the _fields_ into the
"second" class definition. I just need to find what "self" attribute
that is.
More information about the Python-list
mailing list