working with ctypes and complex data structures

Michael Felt michael at felt.demon.nl
Sun Oct 2 13:50:55 EDT 2016


I am trying to understand the documentation re: ctypes and interfacing 
with existing libraries.

I am reading the documentation, and also other sites that have largely 
just copied the documentation - as well as the "free chapter" at 
O'Reilly (Python Cookbook).

I am missing anything on CFields.

My test (template) is:

"""Internal ctypes definitions for AIX libperfstat"""
from sys import maxsize
from ctypes import Structure, Union
from ctypes import c_bool, c_char, c_wchar, c_byte, c_ubyte, c_short, 
c_ushort
from ctypes import c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong
from ctypes import c_float, c_double, c_longdouble
from ctypes import c_char_p, c_wchar_p, c_void_p
from ctypes import sizeof

IDENTIFIER_LENGTH = 64

class time_t(Structure):
     """
#ifndef _TIME_T
#define _TIME_T
#ifdef  _LINUX_SOURCE_COMPAT
typedef long int        time_t;
#else   /* !_LINUX_SOURCE_COMPAT */
#ifdef __64BIT__
typedef long            time_t;
#else
typedef int             time_t;
#endif
#endif  /* !_LINUX_SOURCE_COMPAT */
#endif
"""
     if (maxsize > 2**32):
         _fields_ = [("v", c_long)]
     else:
         _fields_ = [("v", c_int)]

class perfstat_cpu_total_t(Structure):
     """
typedef struct { /* global cpu information */
         int ncpus;                /* number of active logical processors */
         int ncpus_cfg;             /* number of configured processors */
         char description[IDENTIFIER_LENGTH]; /* processor description 
(type/official name) */
         u_longlong_t buffer1[15];   /*  several variables being skipped 
for now */
         time_t lbolt;              /* number of ticks since last reboot */
         u_longlong_t loadavg[3];  /* (1<<SBITS) times the average 
number of runnables processes during the last
  1, 5 and 15 minutes.    */
         u_longlong_t buffer2[29];   /*  several variables being skipped 
for now */
         int ncpus_high;           /* index of highest processor online */
         u_longlong_t puser;       /* raw number of physical processor 
tics in user mode */
         u_longlong_t psys;        /* raw number of physical processor 
tics in system mode */
         u_longlong_t pidle;       /* raw number of physical processor 
tics idle */
         u_longlong_t pwait;       /* raw number of physical processor 
tics waiting for I/O */
         u_longlong_t buffer2[29];   /*  several variables being skipped 
for now */
} perfstat_cpu_total_t;
"""
     _fields_ = [
      ("ncpus", c_int),
      ("ncpus_cfg", c_int),
      ("description", c_char * IDENTIFIER_LENGTH),
      ("buffer1", c_ulonglong * 15),
      ("lbolt", time_t),
      ("loadavg", c_ulonglong * 3),
      ("buffer2", c_ulonglong * 29),
      ("ncpus_high", c_int),
      ("puser", c_ulonglong),
      ("psys", c_ulonglong),
      ("pidle", c_ulonglong),
      ("pwait", c_ulonglong),
      ("buffer3", c_ulonglong * 12)
     ]

xxx = time_t
print xxx
print xxx.v
print sizeof(xxx)
# print sizeof(xxx.v)

xxx = perfstat_cpu_total_t
print xxx
print xxx.lbolt
print sizeof(xxx)
# print sizeof(xxx.lbolt)
xxx = xxx.lbolt
print xxx

print xxx.value

result:

michael at x071:[/data/prj/python/cloud-init/aix-cloud-init-0.7.8.1/new]python 
test*
<class '__main__.time_t'>
<Field type=c_long, ofs=0, size=8>
8
<class '__main__.perfstat_cpu_total_t'>
<Field type=time_t, ofs=192, size=8>
592
<Field type=time_t, ofs=192, size=8>
Traceback (most recent call last):
   File "test_1.py", line 79, in <module>
     print xxx.value
AttributeError: '_ctypes.CField' object has no attribute 'value'

Question:

a) where is documentation on "CField"'s? I have not been successful with 
google - :(
b) what I am not understanding - as the basic documentation shows 
FOO.value as the way to set/get the value of a _field_

+++

I assume I am not understanding something from the base documentation at 
https://docs.python.org/2/library/ctypes.html#Structured-data-types

Hints, improved links, etc are appreciated!



More information about the Python-list mailing list