Catching floating point errors from linked C code

John Nagle nagle at animats.com
Thu Jan 25 01:12:07 EST 2007


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