[Pyrex] Access to nested structures and arrays

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Mar 19 22:50:47 UTC 2007


Pierre GM wrote:

> cdef class modelflags:
>     cdef int *c_list[8]
>
>     def __setitem__(self, idx, val):
>         cdef int tmpval
>         tmpval = val
>         self.c_list[i] = &tmpval

This doesn't look right -- you're putting a pointer to
a stack-allocated int in the array. Don't you just want
the ints directly in the list? i.e.

 > cdef class modelflags:
 >     cdef int c_list[8]
 >
 >     def __setitem__(self, i, val):
 >         self.c_list[i] = val

> cdef setup(self, c_struct  *base, long npar):
> 	...
>         self.parametric_flags.c_list[0] = base.parametric[0]

> AttributeError: 'modelflags' object has no attribute c_list.

Can you post the whole code? I suspect what you're actually
using doesn't match the snippet above, since with modelflags
declared as you claim, you shouldn't have got that message.

>>Perhaps if you think of things in terms of providing a python
>>interface to manipulating the underlying c structure,
> 
> Er, yes, but I have no idea how.

You're on the right track with using methods like __getitem__
and __setitem__ to access the C data, rather than converting
it en masse.

--
Greg



More information about the Pyrex mailing list