[Numpy-discussion] Numpy extension writing problems

Jeremy Sanders jeremy@jeremysanders....
Thu Sep 24 03:50:22 CDT 2009

Hi - I've never written a Python extension before, so I apologise in advance 
for my lack of knowledge. I'm trying to interpret a variable length tuple of 
variable length numpy arrays, convert then to C double arrays and pass them 
to a C++ function. I'm using SIP (as I also need to deal with Qt).

Here is my rather poor code(I realise that variable length C arrays on the 
stack are a gcc extension). a0 is the PyObject* for the tuple. It core dumps 
on PyArray_AsCArray.

        const Py_ssize_t numitems = PyTuple_Size(a0);
        double* data[numitems];
        int sizes[numitems];
        PyObject* objects[numitems];
        int status = 0;
        PyArray_Descr *descr = PyArray_DescrFromType(PyArray_DOUBLE);

        for(Py_ssize_t i = 0; i != numitems; i++)
         data[i] = NULL;

        if( ! PyTuple_Check(a0) )
         goto exit;

        for(Py_ssize_t i = 0; i != numitems; i++)
                objects[i] = PyTuple_GetItem(a0, i);

                npy_intp size;
                int ret = PyArray_AsCArray(&objects[i], (void*)(&data[i]),
                 &size, 1, descr);

                if(ret < 0)
                  status = 1;
                  goto exit;
                sizes[i] = size;

        // this is the actual function to call
        TestKlass::myfunc(data, sizes, numitems);

        for(Py_ssize_t i = 0; i != numitems; i++)
          if( data[i] != NULL )
            PyArray_Free(objects[i], data[i]);

        if(status != 0)
                PyErr_SetString(PyExc_RuntimeError, "conversion error");
                sipIsErr = 1;

Can someone give me a hint on where I'm going wrong? Can't I pass 
PyTuple_GetItem objects to PyArray_AsCArray. Is there a numpy built in 
routine which makes this easier?



More information about the NumPy-Discussion mailing list