Back to index

python3.2  3.2.2
Typedefs | Functions | Variables
fpectlmodule.c File Reference
#include "Python.h"
#include <signal.h>
#include <setjmp.h>

Go to the source code of this file.

Typedefs

typedef void Sigfunc (int)

Functions

static void fpe_reset (Sigfunc *)
PyMODINIT_FUNC PyInit_fpectl (void)
static PyObjectturnon_sigfpe (PyObject *self, PyObject *args)
static PyObjectturnoff_sigfpe (PyObject *self, PyObject *args)
static void sigfpe_handler (int signo)

Variables

static jmp_buf PyFPE_jbuf
static int PyFPE_counter = 0
static Sigfunc sigfpe_handler
static PyObjectfpe_error
static PyMethodDef fpectl_methods []
static struct PyModuleDef

Typedef Documentation

typedef void Sigfunc(int)

Definition at line 88 of file fpectlmodule.c.


Function Documentation

static void fpe_reset ( Sigfunc handler) [static]

Definition at line 113 of file fpectlmodule.c.

{
    /* Reset the exception handling machinery, and reset the signal
     * handler for SIGFPE to the given handler.
     */

/*-- IRIX -----------------------------------------------------------------*/
#if defined(sgi)
    /* See man page on handle_sigfpes -- must link with -lfpe
     * My usage doesn't follow the man page exactly.  Maybe somebody
     * else can explain handle_sigfpes to me....
     * cc -c -I/usr/local/python/include fpectlmodule.c
     * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe
     */
#include <sigfpe.h>
    typedef void user_routine (unsigned[5], int[2]);
    typedef void abort_routine (unsigned long);
    handle_sigfpes(_OFF, 0,
                 (user_routine *)0,
                 _TURN_OFF_HANDLER_ON_ERROR,
                 NULL);
    handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID,
                 (user_routine *)0,
                 _ABORT_ON_ERROR,
                 NULL);
    PyOS_setsig(SIGFPE, handler);

/*-- SunOS and Solaris ----------------------------------------------------*/
#elif defined(sun)
    /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags
       man pages (SunOS or Solaris)
       cc -c -I/usr/local/python/include fpectlmodule.c
       ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm
     */
#include <math.h>
#ifndef _SUNMATH_H
    extern void nonstandard_arithmetic(void);
    extern int ieee_flags(const char*, const char*, const char*, char **);
    extern long ieee_handler(const char*, const char*, sigfpe_handler_type);
#endif

    char *mode="exception", *in="all", *out;
    (void) nonstandard_arithmetic();
    (void) ieee_flags("clearall",mode,in,&out);
    (void) ieee_handler("set","common",(sigfpe_handler_type)handler);
    PyOS_setsig(SIGFPE, handler);

/*-- HPUX -----------------------------------------------------------------*/
#elif defined(__hppa) || defined(hppa)
    /* References:   fpsetmask man page */
    /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */
    /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */
#include <math.h>
    fpsetdefaults();
    PyOS_setsig(SIGFPE, handler);

/*-- IBM AIX --------------------------------------------------------------*/
#elif defined(__AIX) || defined(_AIX)
    /* References:   fp_trap, fp_enable man pages */
#include <fptrap.h>
    fp_trap(FP_TRAP_SYNC);
    fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW);
    PyOS_setsig(SIGFPE, handler);

/*-- DEC ALPHA OSF --------------------------------------------------------*/
#elif defined(__alpha) && defined(__osf__)
    /* References:   exception_intro, ieee man pages */
    /* cc -c -I/usr/local/python/include fpectlmodule.c */
    /* ld -shared -o fpectlmodule.so fpectlmodule.o */
#include <machine/fpu.h>
    unsigned long fp_control =
    IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
    ieee_set_fp_control(fp_control);
    PyOS_setsig(SIGFPE, handler);

/*-- DEC ALPHA LINUX ------------------------------------------------------*/
#elif defined(__alpha) && defined(linux)
#include <asm/fpu.h>
    unsigned long fp_control =
    IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF;
    ieee_set_fp_control(fp_control);
    PyOS_setsig(SIGFPE, handler);

/*-- DEC ALPHA VMS --------------------------------------------------------*/
#elif defined(__ALPHA) && defined(__VMS)
        IEEE clrmsk;
        IEEE setmsk;
        clrmsk.ieee$q_flags =
                IEEE$M_TRAP_ENABLE_UNF |  IEEE$M_TRAP_ENABLE_INE |
                 IEEE$M_MAP_UMZ;
        setmsk.ieee$q_flags =
                IEEE$M_TRAP_ENABLE_INV | IEEE$M_TRAP_ENABLE_DZE |
                IEEE$M_TRAP_ENABLE_OVF;
        sys$ieee_set_fp_control(&clrmsk, &setmsk, 0);
        PyOS_setsig(SIGFPE, handler);

/*-- HP IA64 VMS --------------------------------------------------------*/
#elif defined(__ia64) && defined(__VMS)
    PyOS_setsig(SIGFPE, handler);

/*-- Cray Unicos ----------------------------------------------------------*/
#elif defined(cray)
    /* UNICOS delivers SIGFPE by default, but no matherr */
#ifdef HAS_LIBMSET
    libmset(-1);
#endif
    PyOS_setsig(SIGFPE, handler);

/*-- FreeBSD ----------------------------------------------------------------*/
#elif defined(__FreeBSD__)
    fpresetsticky(fpgetsticky());
    fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL);
    PyOS_setsig(SIGFPE, handler);

/*-- Linux ----------------------------------------------------------------*/
#elif defined(linux)
#ifdef __GLIBC__
#include <fpu_control.h>
#else
#include <i386/fpu_control.h>
#endif
#ifdef _FPU_SETCW
    {
        fpu_control_t cw = 0x1372;
        _FPU_SETCW(cw);
    }
#else
    __setfpucw(0x1372);
#endif
    PyOS_setsig(SIGFPE, handler);

/*-- Microsoft Windows, NT ------------------------------------------------*/
#elif defined(_MSC_VER)
    /* Reference: Visual C++ Books Online 4.2,
       Run-Time Library Reference, _control87, _controlfp */
#include <float.h>
    unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW;
    (void)_controlfp(0, cw);
    PyOS_setsig(SIGFPE, handler);

/*-- Give Up --------------------------------------------------------------*/
#else
    fputs("Operation not implemented\n", stderr);
#endif

}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 302 of file fpectlmodule.c.

{
    PyObject *m, *d;
    m = PyModule_Create(&fpectlmodule);
    if (m == NULL)
        return NULL;
    d = PyModule_GetDict(m);
    fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
    if (fpe_error != NULL)
        PyDict_SetItemString(d, "error", fpe_error);
    return m;
}

Here is the call graph for this function:

static void sigfpe_handler ( int  signo) [static]

Definition at line 280 of file fpectlmodule.c.

{
    fpe_reset(sigfpe_handler);
    if(PyFPE_counter) {
        longjmp(PyFPE_jbuf, 1);
    } else {
        Py_FatalError("Unprotected floating point exception");
    }
}

Here is the call graph for this function:

static PyObject * turnoff_sigfpe ( PyObject self,
PyObject args 
) [static]

Definition at line 260 of file fpectlmodule.c.

{
#ifdef __FreeBSD__
    fpresetsticky(fpgetsticky());
    fpsetmask(0);
#elif defined(__VMS)
        IEEE clrmsk;
         clrmsk.ieee$q_flags =
                IEEE$M_TRAP_ENABLE_UNF |  IEEE$M_TRAP_ENABLE_INE |
                IEEE$M_MAP_UMZ | IEEE$M_TRAP_ENABLE_INV |
                IEEE$M_TRAP_ENABLE_DZE | IEEE$M_TRAP_ENABLE_OVF |
                IEEE$M_INHERIT;
        sys$ieee_set_fp_control(&clrmsk, 0, 0);
#else
    fputs("Operation not implemented\n", stderr);
#endif
    Py_INCREF(Py_None);
    return Py_None;
}
static PyObject * turnon_sigfpe ( PyObject self,
PyObject args 
) [static]

Definition at line 104 of file fpectlmodule.c.

{
    /* Do any architecture-specific one-time only initialization here. */

    fpe_reset(sigfpe_handler);
    Py_INCREF (Py_None);
    return Py_None;
}

Here is the call graph for this function:


Variable Documentation

PyObject* fpe_error [static]

Definition at line 92 of file fpectlmodule.c.

Initial value:
 {
    {"turnon_sigfpe",            (PyCFunction) turnon_sigfpe,            METH_VARARGS},
    {"turnoff_sigfpe",           (PyCFunction) turnoff_sigfpe,           METH_VARARGS},
    {0,0}
}

Definition at line 98 of file fpectlmodule.c.

int PyFPE_counter = 0 [static]

Definition at line 85 of file fpectlmodule.c.

jmp_buf PyFPE_jbuf [static]

Definition at line 84 of file fpectlmodule.c.

struct PyModuleDef [static]
Initial value:
 {
        PyModuleDef_HEAD_INIT,
        "fpectl",
        NULL,
        -1,
        fpectl_methods,
        NULL,
        NULL,
        NULL,
        NULL
}

Definition at line 290 of file fpectlmodule.c.

Definition at line 89 of file fpectlmodule.c.