Catching floating point errors from linked C code

Discussion in 'Python' started by John Pye, Jan 25, 2007.

  1. John Pye

    John Pye Guest

    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?

    Cheers
    JP
     
    John Pye, Jan 25, 2007
    #1
    1. Advertising

  2. John Pye

    John Nagle Guest

    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
     
    John Nagle, Jan 25, 2007
    #2
    1. Advertising

  3. John Pye

    John Pye Guest

    Hi John,

    On Jan 25, 3:43 pm, John Nagle <> 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?

    If writing a python module with SWIG, and the module uses Python
    callbacks, do I need to restore the signal handlers before running the
    callback, or python do that itself?

    (These might be questions better asked on the SWIG list I guess)

    Cheers
    JP
     
    John Pye, Jan 25, 2007
    #3
  4. John Pye

    John Nagle Guest

    John Pye wrote:
    > Hi John,
    >
    > On Jan 25, 3:43 pm, John Nagle <> 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
     
    John Nagle, Jan 25, 2007
    #4
  5. John Pye

    John Pye Guest

    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 <> wrote:
    > John Pye wrote:
    > > Hi John,

    >
    > > On Jan 25, 3:43 pm, John Nagle <> 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
     
    John Pye, Jan 25, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,685
    Ben Jones
    Mar 2, 2006
  2. Ravi

    Floating point formats not linked

    Ravi, Nov 8, 2003, in forum: C Programming
    Replies:
    3
    Views:
    4,337
    Mark McIntyre
    Nov 8, 2003
  3. neha

    Floating Point Formats are not linked

    neha, Aug 1, 2006, in forum: C Programming
    Replies:
    8
    Views:
    850
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,355
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    266
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page