Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print all fields of ctypes "Structure" with introspection

test.c:

#include <stdio.h>
#include <stdlib.h>

struct s {
    char a;
    int b;
    float c;
    double d;
};

struct s *create_struct()
{
    struct s *res = malloc(sizeof(struct s));
    res->a = 1; res->b = 2; res->c = 3.0f; res->d = 4.0;
    return res;
}

test.py:

from ctypes import *

class S(Structure):
    _fields_ = [
        ('a', c_byte),
        ('b', c_int),
        ('c', c_float),
        ('d', c_double)
    ]

lib = CDLL('./test.so')

create_struct = lib.create_struct
create_struct.restype = POINTER(S)
create_struct.argtypes = []

s_ptr = create_struct()
s = s_ptr.contents

print s._fields_[0][0], s.a
print s._fields_[1][0], s.b
print s._fields_[2][0], s.c
print s._fields_[3][0], s.d
print s.__dict__

output:

a 1
b 2
c 3.0
d 4.0
{}

I'd like to adapt the python script above to print each field of my s structure without having to do explicitly for each field. From what I understand, this can be done using the __dict__ attribute but mine is empty. Is there any way to do this for a class that extends ctypes.Structure?

like image 490
Josh Avatar asked Jan 08 '14 03:01

Josh


3 Answers

How about using getattr?

>>> from ctypes import *
>>>
>>> class S(Structure):
...     _fields_ = [
...         ('a', c_byte),
...         ('b', c_int),
...         ('c', c_float),
...         ('d', c_double)
...     ]
...
>>> s = S(1, 2, 3, 4.0)
>>>
>>> for field_name, field_type in s._fields_:
...     print field_name, getattr(s, field_name)
...
a 1
b 2
c 3.0
d 4.0

UPDATE

If there is a bitfield in the structure (or union), iterating _fields_ yield a tuple of 3 items which will cause ValueError. To prevent that you need to adjust the code:

...

for field in s._fields_:
    print field[0], getattr(s, field[0])
like image 188
falsetru Avatar answered Oct 18 '22 20:10

falsetru


I've just come up with this, for a project I'm working on that has several C structures I want to be able to pretty-print (in a Jupyter notebook, in my case):

>>> # https://stackoverflow.com/a/62011887/76452
>>>
>>> from ctypes import Structure, c_byte, c_int, c_float, c_double
>>>
>>>
>>> class MyStructure(Structure):
...    
...    def __repr__(self) -> str:
...        values = ", ".join(f"{name}={value}"
...                          for name, value in self._asdict().items())
...        return f"<{self.__class__.__name__}: {values}>"
>>>
>>>    def _asdict(self) -> dict:
...        return {field[0]: getattr(self, field[0])
...                for field in self._fields_}
>>> 
>>>
>>> class S(MyStructure):
...     _fields_ = (
...         ('a', c_byte),
...         ('b', c_int),
...         ('c', c_float),
...         ('d', c_double)
...     )
>>>
>>> s = S(1, 2, 3.0, 4.0)
>>> s
<S: a=1, b=2, c=3.0, d=4.0>
like image 25
tobych Avatar answered Oct 18 '22 21:10

tobych


Here is @falsetru's answer as a __str__ method on the ctypes class:

def __str__(self):
    return "{}: {{{}}}".format(self.__class__.__name__,
                               ", ".join(["{}: {}".format(field[0],
                                                          getattr(self,
                                                                  field[0]))
                                          for field in self._fields_]))
like image 1
Jim Hunziker Avatar answered Oct 18 '22 20:10

Jim Hunziker