[Pyrex] Structures containing pointers to Python objects
Joachim Saul
saul at gfz-potsdam.de
Mon Feb 2 10:16:46 CET 2004
* Konrad Hinsen [2004-02-01 13:47]:
> Suppose I use the NumPy interface code from the Pyrex FAQ:
>
> cdef extern from "Numeric/arrayobject.h":
>
> struct PyArray_Descr:
> int type_num, elsize
> char type
>
> ctypedef class Numeric.ArrayType [object PyArrayObject]:
> cdef char *data
> cdef int nd
> cdef int *dimensions, *strides
> cdef object base
> cdef PyArray_Descr *descr
> cdef int flags
>
> How do I then write a declaration for a struct that contains a pointer
> to a NumPy array? Pyrex won't accept "ArrayType" or "ArrayType *"
> inside a struct declaration, nor "PyArrayObject *".
Do you actually mean a struct or a C class? For the latter, what I
do is something like
cdef public class cTrace [type Trace_Type, object TraceObject]:
cdef ArrayType _data # note that it is *not* declared public
property data:
def __get__(self):
return self._data
def __set__(self, ArrayType data):
self._data = data
This is partly a workaround, because it is currently not possible
to declare
cdef public ArrayType data
and instead one would have to write
cdef public object data
I therefore use the property 'data' to access the private '_data';
the __set__ takes care of the type checking and in the real-life
code performs some additional tasks. In all examples, [_]data is
actually a pointer to the array.
Is this what you were looking for?
Cheers,
Joachim
More information about the Pyrex
mailing list