[Numpy-discussion] ndarray of object dtype

Robert Kern robert.kern@gmail....
Mon Oct 4 09:55:29 CDT 2010

On Mon, Oct 4, 2010 at 00:41, Ioan Ferencik <ioan.ferencik@tkk.fi> wrote:
> Hello list,
> I am trying to pass elements of a custom defined with C API.
> I have successfully wrapped a type around a C struct.
> I intent to create a list with these objects and process it using numpy C API.
> So i  create an array:
> array = (PyArrayObject *)PyArray_ContiguousFromObject(input,
> PyArray_OBJECT, 0, 0);
> to my understanding each el. in this array is a pointer to my type so
> a cast to this type
> should work.
> this is my custom type
> typedef struct val{
>         PyObject_HEAD
>         float q;
>         float wl;
>         int cssid;
>         int br;
> }hm1dval;
> I am passing only one element for testing purposes.
> in python
> a = [hm1d1.hm1dval() for i in range(0,1)]
> for c in a:
>         c.set_values(q=3.0, wl=2.5, cssid=6, br=7)
> So following code should be valid in C:
> hm1dval s* = PyArray_DATA(array);
> but the members are 0 after casting in spite they were set previously.

The elements of a dtype=object array are PyObject* pointers. In C, an
array of pointers would look like this:

hm1dval **s = PyArray_DATA(array);

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list