| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#ifdef __cplusplus |
| 65 |
extern "C" { |
| 66 |
#endif |
| 67 |
|
| 68 |
#include "Python.h" |
| 69 |
#include <signal.h> |
| 70 |
|
| 71 |
#if defined(__FreeBSD__) |
| 72 |
# include <ieeefp.h> |
| 73 |
#elif defined(__VMS) |
| 74 |
#define __NEW_STARLET |
| 75 |
#include <starlet.h> |
| 76 |
#include <ieeedef.h> |
| 77 |
#endif |
| 78 |
|
| 79 |
#ifndef WANT_SIGFPE_HANDLER |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#include <setjmp.h> |
| 84 |
static jmp_buf PyFPE_jbuf; |
| 85 |
static int PyFPE_counter = 0; |
| 86 |
#endif |
| 87 |
|
| 88 |
typedef void Sigfunc(int); |
| 89 |
static Sigfunc sigfpe_handler; |
| 90 |
static void fpe_reset(Sigfunc *); |
| 91 |
|
| 92 |
static PyObject *fpe_error; |
| 93 |
PyMODINIT_FUNC initfpectl(void); |
| 94 |
static PyObject *turnon_sigfpe (PyObject *self,PyObject *args); |
| 95 |
static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args); |
| 96 |
|
| 97 |
static PyMethodDef fpectl_methods[] = { |
| 98 |
{"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS}, |
| 99 |
{"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS}, |
| 100 |
{0,0} |
| 101 |
}; |
| 102 |
|
| 103 |
static PyObject *turnon_sigfpe(PyObject *self,PyObject *args) |
| 104 |
{ |
| 105 |
|
| 106 |
|
| 107 |
fpe_reset(sigfpe_handler); |
| 108 |
Py_INCREF (Py_None); |
| 109 |
return Py_None; |
| 110 |
} |
| 111 |
|
| 112 |
static void fpe_reset(Sigfunc *handler) |
| 113 |
{ |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#if defined(sgi) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#include <sigfpe.h> |
| 127 |
typedef void user_routine (unsigned[5], int[2]); |
| 128 |
typedef void abort_routine (unsigned long); |
| 129 |
handle_sigfpes(_OFF, 0, |
| 130 |
(user_routine *)0, |
| 131 |
_TURN_OFF_HANDLER_ON_ERROR, |
| 132 |
NULL); |
| 133 |
handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID, |
| 134 |
(user_routine *)0, |
| 135 |
_ABORT_ON_ERROR, |
| 136 |
NULL); |
| 137 |
PyOS_setsig(SIGFPE, handler); |
| 138 |
|
| 139 |
|
| 140 |
#elif defined(sun) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
#include <math.h> |
| 147 |
#ifndef _SUNMATH_H |
| 148 |
extern void nonstandard_arithmetic(void); |
| 149 |
extern int ieee_flags(const char*, const char*, const char*, char **); |
| 150 |
extern long ieee_handler(const char*, const char*, sigfpe_handler_type); |
| 151 |
#endif |
| 152 |
|
| 153 |
char *mode="exception", *in="all", *out; |
| 154 |
(void) nonstandard_arithmetic(); |
| 155 |
(void) ieee_flags("clearall",mode,in,&out); |
| 156 |
(void) ieee_handler("set","common",(sigfpe_handler_type)handler); |
| 157 |
PyOS_setsig(SIGFPE, handler); |
| 158 |
|
| 159 |
|
| 160 |
#elif defined(__hppa) || defined(hppa) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
#include <math.h> |
| 165 |
fpsetdefaults(); |
| 166 |
PyOS_setsig(SIGFPE, handler); |
| 167 |
|
| 168 |
|
| 169 |
#elif defined(__AIX) || defined(_AIX) |
| 170 |
|
| 171 |
#include <fptrap.h> |
| 172 |
fp_trap(FP_TRAP_SYNC); |
| 173 |
fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); |
| 174 |
PyOS_setsig(SIGFPE, handler); |
| 175 |
|
| 176 |
|
| 177 |
#elif defined(__alpha) && defined(__osf__) |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
#include <machine/fpu.h> |
| 182 |
unsigned long fp_control = |
| 183 |
IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF; |
| 184 |
ieee_set_fp_control(fp_control); |
| 185 |
PyOS_setsig(SIGFPE, handler); |
| 186 |
|
| 187 |
|
| 188 |
#elif defined(__alpha) && defined(linux) |
| 189 |
#include <asm/fpu.h> |
| 190 |
unsigned long fp_control = |
| 191 |
IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF; |
| 192 |
ieee_set_fp_control(fp_control); |
| 193 |
PyOS_setsig(SIGFPE, handler); |
| 194 |
|
| 195 |
|
| 196 |
#elif defined(__ALPHA) && defined(__VMS) |
| 197 |
IEEE clrmsk; |
| 198 |
IEEE setmsk; |
| 199 |
clrmsk.ieee$q_flags = |
| 200 |
IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE | |
| 201 |
IEEE$M_MAP_UMZ; |
| 202 |
setmsk.ieee$q_flags = |
| 203 |
IEEE$M_TRAP_ENABLE_INV | IEEE$M_TRAP_ENABLE_DZE | |
| 204 |
IEEE$M_TRAP_ENABLE_OVF; |
| 205 |
sys$ieee_set_fp_control(&clrmsk, &setmsk, 0); |
| 206 |
PyOS_setsig(SIGFPE, handler); |
| 207 |
|
| 208 |
|
| 209 |
#elif defined(__ia64) && defined(__VMS) |
| 210 |
PyOS_setsig(SIGFPE, handler); |
| 211 |
|
| 212 |
|
| 213 |
#elif defined(cray) |
| 214 |
|
| 215 |
#ifdef HAS_LIBMSET |
| 216 |
libmset(-1); |
| 217 |
#endif |
| 218 |
PyOS_setsig(SIGFPE, handler); |
| 219 |
|
| 220 |
|
| 221 |
#elif defined(__FreeBSD__) |
| 222 |
fpresetsticky(fpgetsticky()); |
| 223 |
fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL); |
| 224 |
PyOS_setsig(SIGFPE, handler); |
| 225 |
|
| 226 |
|
| 227 |
#elif defined(linux) |
| 228 |
#ifdef __GLIBC__ |
| 229 |
#include <fpu_control.h> |
| 230 |
#else |
| 231 |
#include <i386/fpu_control.h> |
| 232 |
#endif |
| 233 |
#ifdef _FPU_SETCW |
| 234 |
{ |
| 235 |
fpu_control_t cw = 0x1372; |
| 236 |
_FPU_SETCW(cw); |
| 237 |
} |
| 238 |
#else |
| 239 |
__setfpucw(0x1372); |
| 240 |
#endif |
| 241 |
PyOS_setsig(SIGFPE, handler); |
| 242 |
|
| 243 |
|
| 244 |
#elif defined(_MSC_VER) |
| 245 |
|
| 246 |
|
| 247 |
#include <float.h> |
| 248 |
unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW; |
| 249 |
(void)_controlfp(0, cw); |
| 250 |
PyOS_setsig(SIGFPE, handler); |
| 251 |
|
| 252 |
|
| 253 |
#else |
| 254 |
fputs("Operation not implemented\n", stderr); |
| 255 |
#endif |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args) |
| 260 |
{ |
| 261 |
#ifdef __FreeBSD__ |
| 262 |
fpresetsticky(fpgetsticky()); |
| 263 |
fpsetmask(0); |
| 264 |
#elif defined(__VMS) |
| 265 |
IEEE clrmsk; |
| 266 |
clrmsk.ieee$q_flags = |
| 267 |
IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE | |
| 268 |
IEEE$M_MAP_UMZ | IEEE$M_TRAP_ENABLE_INV | |
| 269 |
IEEE$M_TRAP_ENABLE_DZE | IEEE$M_TRAP_ENABLE_OVF | |
| 270 |
IEEE$M_INHERIT; |
| 271 |
sys$ieee_set_fp_control(&clrmsk, 0, 0); |
| 272 |
#else |
| 273 |
fputs("Operation not implemented\n", stderr); |
| 274 |
#endif |
| 275 |
Py_INCREF(Py_None); |
| 276 |
return Py_None; |
| 277 |
} |
| 278 |
|
| 279 |
static void sigfpe_handler(int signo) |
| 280 |
{ |
| 281 |
fpe_reset(sigfpe_handler); |
| 282 |
if(PyFPE_counter) { |
| 283 |
longjmp(PyFPE_jbuf, 1); |
| 284 |
} else { |
| 285 |
Py_FatalError("Unprotected floating point exception"); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
PyMODINIT_FUNC initfpectl(void) |
| 290 |
{ |
| 291 |
PyObject *m, *d; |
| 292 |
m = Py_InitModule("fpectl", fpectl_methods); |
| 293 |
if (m == NULL) |
| 294 |
return; |
| 295 |
d = PyModule_GetDict(m); |
| 296 |
fpe_error = PyErr_NewException("fpectl.error", NULL, NULL); |
| 297 |
if (fpe_error != NULL) |
| 298 |
PyDict_SetItemString(d, "error", fpe_error); |
| 299 |
} |
| 300 |
|
| 301 |
#ifdef __cplusplus |
| 302 |
} |
| 303 |
#endif |