Catching floating point errors from linked C code

John Nagle nagle at animats.com
Wed Jan 24 23:43:27 EST 2007


John Pye wrote:
> Hi all
> 
> I have some C code that is giving me some 'nan' values in some
> calculations.  The C code is wrapped using SWIG to give me a Python
> module that I am then exercising through a unittest suite.
> 
> It seems that I should expect the C code to throw floating point
> exceptions (SIGFPE) and either the whole thing to abort, or for Python
> to catch the errors and report them. Instead I'm getting neither. I
> want to be able to track down the exact location of problems in my C
> code.
> 
> Is there something that Python does to turn on/off the catching of
> divide-by-zero errors, eg by manipulating <signal.h> signal handlers
> and/or <fenv.h> settings?

    What platform are you on?

    Python is probably running with floating point exceptions disabled,
but you can enable them in your C code, and restoring the floating
point mode when you leave, if you want.  This is probably
only worth doing under a debugger, because otherwise you just end
up with an aborted instance of Python.

				John Nagle



More information about the Python-list mailing list