[Matrix-SIG] Advice on an how to handle errors in extension module.

Robin Becker robin@jessikat.demon.co.uk
Thu, 10 Jun 1999 19:29:17 +0100


In article <Pine.LNX.4.10.9906101133010.8120-100000@us2.mayo.edu>,
Travis Oliphant <Oliphant.Travis@mayo.edu> writes
>
>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 http://oliphant.netpedia.net
>
>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
the real problem here (I assume) is that the fortran code doesn't know
about errors in the integrand function. Well written fortran used to
have error returns etc etc., but the quadpack doesn't. The correct way
to do this would be to rewrite the calls to the function to allow for an
error return. Alternatively what I did for the tcl dll caller was to
allow for an error return using a setjump in the main interface. How are
you dealing with errors in the quadpack code?
>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
>
>
>_______________________________________________
>Matrix-SIG maillist  -  Matrix-SIG@python.org
>http://www.python.org/mailman/listinfo/matrix-sig

-- 
Robin Becker