Catching floating point errors from linked C code

John Pye john.pye at gmail.com
Thu Jan 25 18:53:27 EST 2007


Hi John

I think you're right, and I need to place FPE 'brackets' around my
code.

The thing that was confusing me was that python has division by zero
exceptions (for which I assumed fenv.h was being used), but when from
python I reach down into my SWIG code and do a 1/0, no SIGFPE is
thrown.

I will experiment with the _controlfp stuff (and linux equivs) and see
how it goes. Thanks for your help.

Cheers
JP


On Jan 25, 5:12 pm, John Nagle <n... at animats.com> wrote:
> John Pye wrote:
> > Hi John,
>
> > On Jan 25, 3:43 pm, John Nagle <n... at animats.com> wrote:
>
> >>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.
>
> > So as far as you know, Python doesn't switch stuff like
> > feenableexcept(FE_EXCEPT_ALL)? Does Python use the <signal.h>
> > internally, or are all its FPEs detected by explicit checks beforehand?    I have no idea what state Python leaves the FPU in, but whatever
> the state is, you can read it, set your own state, and restore
> the previous state before returning to Python.
>
>     The last time I had to do this, I used
>
>         unsigned int oldstate =         // make almost all FPU errors fatal
>                 _controlfp ((~_EM_INVALID) & _CW_DEFAULT, _MCW_EM);
>         ...
>         _controlfp(oldstate);           // restore old state
>
> on Windows 2000.
> 
>                                         John Nagle




More information about the Python-list mailing list