Advice on an how to handle errors in extension module.

Travis Oliphant olipt at
Thu Jun 10 18:45:06 CEST 1999

I'm writing some Python-C interfaces to some mathematical libraries in
FORTRAN.  In particular, I'm working on interfacing QUADPACK with Python.
The code is basically complete and an old version can be found in 
Multipack at

The problem is that the FORTRAN code requires that there be an (external)
function defined which computes the integrand.  This function must be of a
certain type.  The function I use is a just wrapper that handles calling
the user defined Python function (stored in a static global variable).

The question I have is that if a Python error occurs while evaluation of
the user-defined function, I'm not sure what to do inside this wrapper
function which can only return a double.  Currently, I'm printing the
error (if it hasn't been printed before) and returning a zero.  This won't
stop the integrator from callling the function again but it will have the
effect of treating the function as if it were zero.

This actually works really well for 1-D integration.  Lately, though, I've
just made the C-interface so that it can be re-entrant (so the Python
function itself could call an integration routine --- allows for easy
multiple integration).  But, this exposes the inelegance of my solution
for handling a Python error.

I'm just fishing for ideas from more knowledgeable people on this list.

Travis Oliphant

More information about the Python-list mailing list