Python CFFI convert structure to dictionary
Asked Answered
O

3

7

There is a way to initialize structure with dictionary:

fooData= {'y': 1, 'x': 2}
fooStruct = ffi.new("foo_t*", fooData)
fooBuffer = ffi.buffer(fooStruct)

Is there some ready function to do the conversion?

fooStruct = ffi.new("foo_t*")
(ffi.buffer(fooStruct))[:] = fooBuffer
fooData= convert_to_python( fooStruct[0] )    

Do I have to use ffi.typeof("foo_t").fields by myself?

I come up with this code so far:

def __convert_struct_field( s, fields ):
    for field,fieldtype in fields:
        if fieldtype.type.kind == 'primitive':
            yield (field,getattr( s, field ))
        else:
            yield (field, convert_to_python( getattr( s, field ) ))

def convert_to_python(s):
    type=ffi.typeof(s)
    if type.kind == 'struct':
        return dict(__convert_struct_field( s, type.fields ) )
    elif type.kind == 'array':
        if type.item.kind == 'primitive':
            return [ s[i] for i in range(type.length) ]
        else:
            return [ convert_to_python(s[i]) for i in range(type.length) ]
    elif type.kind == 'primitive':
        return int(s)

Is there a faster way?

Ochlocracy answered 7/12, 2013 at 17:51 Comment(2)
There is no built-in way, no. Usually it's not needed: just use the cdata object directly, without converting it to dictionaries and lists first.Hager
@ArminRigo I need to convert it to JSON.Ochlocracy
O
4

Arpegius' solution works fine for me, and is quite elegant. I implemented a solution based on Selso's suggestion to use inspect. dir() can substitute inspect.

from inspect import getmembers
from cffi import FFI
ffi = FFI()
from pprint import pprint

def cdata_dict(cd):
    if isinstance(cd, ffi.CData):
        try:
            return ffi.string(cd)
        except TypeError:
            try:
                return [cdata_dict(x) for x in cd]
            except TypeError:
                return {k: cdata_dict(v) for k, v in getmembers(cd)}
    else:
        return cd

foo = ffi.new("""
struct Foo {
    char name[6];
    struct {
        int a, b[3];
    } item;
} *""",{
    'name': b"Foo",
    'item': {'a': 3, 'b': [1, 2, 3]}
})
pprint(cdata_dict(foo))

Output:

{'item': {'a': 3, 'b': [1, 2, 3]}, 'name': b'Foo'}
Otilia answered 17/2, 2019 at 12:17 Comment(1)
Wow I am glad that after six years some one is still itrested in this question. Unfortunatly, for perfomance reasons, we changed to PyPy and used only cffi structures and others serialization methods.Ochlocracy
V
1

This code infortunately does not work for me, as some struct members are "pointer" types, it leads to storing "none" in the dict.

I am a Python noob, but maybe the inspect module would be another starting point, and a shorter way to print "simple" data. Then we would iterate over the result in order to unroll data structure.

For example with the following example :

struct foo {
int a;
char b[10];
};

Using inspect.getmembers( obj ) I have the following result :

[('a', 10), ('b', <cdata 'char[10]' 0x7f0be10e2824>)]
Vitals answered 25/4, 2018 at 12:48 Comment(0)
H
0

Your code is fine.

Even if there was a built-in way in CFFI, it would not be what you need here. Indeed, you can say ffi.new("foo_t*", {'p': p1}) where p1 is another cdata, but you cannot recursively pass a dictionary containing more dictionaries. The same would be true in the opposite direction: you would get a dictionary that maps field names to "values", but the values themselves would be more cdata objects anyway, and not recursively more dictionaries.

Hager answered 8/12, 2013 at 21:50 Comment(1)
Wow I did not known, even do not test it. Hopefully there was no structures with structure fields, passing to C. Anyway I leave this question open, maybe some one make that functionality in next version of cffi.Ochlocracy

© 2022 - 2024 — McMap. All rights reserved.