How to catch a floating point error after fpectl has been removed?

114 views Asked by At

Python 3.8.12 removed the fpectl module. This module enabled catching floating point errors as standard Python exceptions.

I have a Python programme which uses a module which, in turns, calls a closed-source C library. The C library sometimes causes a floating point error. In this case, the Python interpreter is killed by the OS and the result is that the programme ends abruptly without leaving a stack trace or without any possibility of recovery.

Because the computations performed by this external C library are optional for my programme, I would like to recover from the floating point error and continue, e.g., by catching an exception. Before, this was possible with the fpectl module:

from external_library import buggy_function
import fpectl

fpectl.turnon_sigfpe()

try:
    buggy_function()
except FloatingPointError:
    print('buggy_function aborted: continuing without it')

fpectl.turnoff_sigfpe()

How can I achieve the same result now that fpectl is not available any longer?

0

There are 0 answers