I have put together a few c extensions following the documentation on <a href="http://www.scipy.org/Cookbook/C_Extensions/NumPy_arrays">http://www.scipy.org/Cookbook/C_Extensions/NumPy_arrays</a>. There is however one thing that stumps me.<div>
<br></div><div>To illustrate with a simple code snippet, the test function below multiplies the input numpy double array by two. So far so good. But how about if I want the function to return a tuple of two numpy arrays so I could do 'a, b = myCLib.test(c)' from a Python script? It's straight forward to convert C data structures to Python objects with Py_BuildValue, but how can I do this for numpy arrays instead?<br>
<div><div><br></div><div><div><div>static PyObject *</div><div>test(PyObject *self, PyObject *args)</div><div>{</div><div>    PyArrayObject *py_in, *py_out;</div><div>    double *in, *out;</div><div>    int i, n, dims[2];</div>
<div>    if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &py_in))</div><div>        return NULL;</div><div>    if (NULL == py_in || not_doublevector(py_in)) return NULL;</div><div>    n = py_in->dimensions[0];</div>
<div>    dims[0] = n;</div><div>    dims[1] = 1;</div><div>    py_out = (PyArrayObject *) PyArray_FromDims(1, dims, NPY_DOUBLE);</div><div>    in = pyvector_to_Carrayptrs(py_in);</div><div>    out = pyvector_to_Carrayptrs(py_out);</div>
<div>    for (i=0; i<n; i++) {</div><div>        out[i] = in[i] * 2.0;</div><div>    }</div><div>    return PyArray_Return(py_out);</div><div>}</div></div></div></div></div>