Skip to content

Commit

Permalink
Merge pull request #513 from skirpichev/gmp-error-handler-497
Browse files Browse the repository at this point in the history
Add signal handler to catch SIGFPE on the GMP 6.3+
  • Loading branch information
casevh authored Sep 14, 2024
2 parents c7629dd + dfaae94 commit eefc737
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/gmpy2.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
#include <math.h>
#include <float.h>
#include <ctype.h>
#include <signal.h>

/*
* we do have a dependence on Python's internals, specifically:
Expand Down Expand Up @@ -567,6 +568,23 @@ static struct PyModuleDef moduledef = {
NULL
};

void
gmp_abort_handler(int i)
{
if (__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR >= 3) {
if (gmp_errno & GMP_ERROR_DIVISION_BY_ZERO) {
printf("gmp: divide by zero\n");
}
else if (gmp_errno & GMP_ERROR_SQRT_OF_NEGATIVE) {
printf("gmp: root of negative\n");
}
else {
printf("gmp: out of memory\n");
}
}
abort();
}

PyMODINIT_FUNC PyInit_gmpy2(void)
{
PyObject *result = NULL;
Expand Down Expand Up @@ -947,5 +965,7 @@ PyMODINIT_FUNC PyInit_gmpy2(void)
/* LCOV_EXCL_STOP */
}

PyOS_setsig(SIGFPE, gmp_abort_handler);

return gmpy_module;
}

0 comments on commit eefc737

Please sign in to comment.