[Numpy-discussion] Help about C, Py_INCREF and throwing Exceptions from C

Jon Saenz jsaenz at wm.lc.ehu.es
Wed Sep 6 05:40:10 EDT 2000


Hello, all.

I am finishing a C module which computes a kernel-based probability
density function estimation using a kernel-based approach for
unidimensional and multidimensional PDFs.

The idea is: Given a Numpy array expdata, with the experimental data in a
d-dimensional space (d=1,2,3), we want to make an estimation of the PDF at
grid points 'xpoints' in the same d-dimensional space using a bandwidth h:

Python code:
import KPDF

pdf=KPDF.MPDFEpanechnikov(expdata,xpoints,h)

The module is written in C, and it needs to return a Numpy array shaped
(xpoints.shape[0],).

So, internally, the function which creates the array reads:
int dims[1];
PyArrayObject *rarray; /* returned array */
PyArrayObject *xpoints;

dims[0]=xpoints->dimensions[0];
rarray=(PyArrayObject*)PyArray_FromDims(1,dims,PyArray_DOUBLE);
if (rarray==NULL)
   return NULL;
/* More code follows */
return PyArray_Return(rarray);

I am assuming that I DO NOT have to call Py_INCREF() explicitly in my
function before returning the array to Python, because it is actually
called by PyArray_FromDims(). I would like to know whether it is actually
that way.

I am also assuming that, in case malloc() is unable to allocate memory
inside PyArray_FromDims(), it sets the corresponding exception and my only
TO-DO in my function is returning NULL to the caller.

I would appreciate replies these questions by gurus. I have Read The Funny
Manuals on extending (regular and numerical) Python, but I think these
points are not specially clear, at least, I am doubtful after reading
them.

Thanks in advance.



Jon Saenz.				| Tfno: +34 946012470
Depto. Fisica Aplicada II               | Fax:  +34 944648500
Facultad de Ciencias.   \\ Universidad del Pais Vasco \\
Apdo. 644   \\ 48080 - Bilbao  \\ SPAIN





More information about the NumPy-Discussion mailing list