Creating a numpy dtype from a C structure

496 views
Skip to first unread message

bme...@ska.ac.za

unread,
Jul 24, 2015, 10:48:42 AM7/24/15
to python-cffi
I'm writing some code in C++ for speed, which will take an array of structures from Python. I can define the structure type as a C struct using cdef, but I also want to manipulate the array from numpy, so I want to create a numpy dtype that matches the C structure (including any padding). I can probably just do it by hand and put in an assertion that the size matches, but I was wondering if there was already a function to do this automatically. It looks like a ctype has the required information (list of fields, with name, offset and type) to make a generic converter possible.

Matti Picus

unread,
Jul 25, 2015, 1:32:15 PM7/25/15
to python-cffi, bme...@ska.ac.za
You should use the __array_interface__ to interact with numpy arrays in cffi.
http://docs.scipy.org/doc/numpy/reference/arrays.interface.html
Note that pypy currently implements only the data, shape, and version keys, we seem to have a problem with the dtype and strides.
Feel free to file an issue for incompatibilites.
Matti

Armin Rigo

unread,
Jul 29, 2015, 5:32:49 PM7/29/15
to pytho...@googlegroups.com
Hi,

On 24 July 2015 at 16:48, <bme...@ska.ac.za> wrote:
> can probably just do it by hand and put in an assertion that the size
> matches, but I was wondering if there was already a function to do this
> automatically. It looks like a ctype has the required information (list of
> fields, with name, offset and type) to make a generic converter possible.

There is no such converter that I'm aware of. If you write one,
please share it here!


Armin

bme...@ska.ac.za

unread,
Jul 30, 2015, 3:36:20 AM7/30/15
to python-cffi, ar...@tunes.org

I've partially written a solution, but it's a bit ugly because I'm not sure how to introspect a primitive type to determine whether it is signed, unsigned, float etc. Does cffi contain this sort of introspection information? At the moment I'm just making a list of all primitives of each type based on reading the cffi source.

Bruce

Armin Rigo

unread,
Jul 30, 2015, 4:17:21 AM7/30/15
to bme...@ska.ac.za, python-cffi
Hi Bruce,

On 30 July 2015 at 09:36, <bme...@ska.ac.za> wrote:
> I've partially written a solution, but it's a bit ugly because I'm not sure
> how to introspect a primitive type to determine whether it is signed,
> unsigned, float etc. Does cffi contain this sort of introspection
> information? At the moment I'm just making a list of all primitives of each
> type based on reading the cffi source.

Yes, even if not very cleanly, you can know:

tp = ffi.typeof(T)
assert tp.kind == "primitive" # either some int or some float
if tp.cname in ('float', 'double', 'long double'):
print 'it is a float of size', ffi.sizeof(tp)
else:
print 'it is an integer of size', ffi.sizeof(tp)
x = int(ffi.cast(tp, -1))
if x == -1:
print 'it is signed'
elif x == 1:
print 'it is Bool'
else:
assert x > 1
print 'it is unsigned'


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages