Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting struct elements in Cython

Tags:

cython

Amazingly I can't seem to find a single example of getting elements of a struct, by name (both on the web and in the cython examples).

So I'm receiving a pointer to a struct out of a C function, and would want to access those elements one by one and repackage them into a python list/dict.

maybe:

structPointer['propertyName']

or

structPointer.propertyName  

I want to get the effect of structName->propertyName.

like image 387
deepblue Avatar asked Jan 24 '11 10:01

deepblue


1 Answers

Your second syntax is the correct one, but you have to have an extern declaration for the struct type:

cdef extern from "someheader.h":
   struct properties_t:
      int value1
      int value2
   properties_t* getthem()

cdef void foo():
   cdef properties_t* prop
   prop = getthem()
   i = prop.value1
like image 71
sth Avatar answered Sep 25 '22 16:09

sth