Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Modules/signalmodule.c

    r2 r388  
    88
    99#ifdef MS_WINDOWS
     10#include <Windows.h>
     11#ifdef HAVE_PROCESS_H
    1012#include <process.h>
    1113#endif
    12 
     14#endif
     15
     16#ifdef HAVE_SIGNAL_H
    1317#include <signal.h>
    14 
     18#endif
     19#ifdef HAVE_SYS_STAT_H
    1520#include <sys/stat.h>
     21#endif
    1622#ifdef HAVE_SYS_TIME_H
    1723#include <sys/time.h>
     
    2935#ifndef NSIG
    3036# if defined(_NSIG)
    31 #  define NSIG _NSIG            /* For BSD/SysV */
     37#  define NSIG _NSIG            /* For BSD/SysV */
    3238# elif defined(_SIGMAX)
    33 #  define NSIG (_SIGMAX + 1)    /* For QNX */
     39#  define NSIG (_SIGMAX + 1)    /* For QNX */
    3440# elif defined(SIGMAX)
    35 #  define NSIG (SIGMAX + 1)     /* For djgpp */
     41#  define NSIG (SIGMAX + 1)     /* For djgpp */
    3642# else
    37 #  define NSIG 64               /* Use a reasonable default value */
     43#  define NSIG 64               /* Use a reasonable default value */
    3844# endif
    3945#endif
     
    7783
    7884static struct {
    79         int tripped;
    80         PyObject *func;
     85    int tripped;
     86    PyObject *func;
    8187} Handlers[NSIG];
    8288
     
    121127    r = PyTuple_New(2);
    122128    if (r == NULL)
    123         return NULL;
     129    return NULL;
    124130
    125131    if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
    126         Py_DECREF(r);   
    127         return NULL;
     132    Py_DECREF(r);
     133    return NULL;
    128134    }
    129135
     
    131137
    132138    if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
    133         Py_DECREF(r);
    134         return NULL;
     139    Py_DECREF(r);
     140    return NULL;
    135141    }
    136142
     
    144150signal_default_int_handler(PyObject *self, PyObject *args)
    145151{
    146         PyErr_SetNone(PyExc_KeyboardInterrupt);
    147         return NULL;
     152    PyErr_SetNone(PyExc_KeyboardInterrupt);
     153    return NULL;
    148154}
    149155
     
    158164checksignals_witharg(void * unused)
    159165{
    160         return PyErr_CheckSignals();
     166    return PyErr_CheckSignals();
     167}
     168
     169static void
     170trip_signal(int sig_num)
     171{
     172    Handlers[sig_num].tripped = 1;
     173    if (is_tripped)
     174        return;
     175    /* Set is_tripped after setting .tripped, as it gets
     176       cleared in PyErr_CheckSignals() before .tripped. */
     177    is_tripped = 1;
     178    Py_AddPendingCall(checksignals_witharg, NULL);
     179    if (wakeup_fd != -1)
     180        write(wakeup_fd, "\0", 1);
    161181}
    162182
     
    164184signal_handler(int sig_num)
    165185{
     186    int save_errno = errno;
     187
     188#if defined(WITH_THREAD) && defined(WITH_PTH)
     189    if (PyThread_get_thread_ident() != main_thread) {
     190        pth_raise(*(pth_t *) main_thread, sig_num);
     191    }
     192    else
     193#endif
     194    {
    166195#ifdef WITH_THREAD
    167 #ifdef WITH_PTH
    168         if (PyThread_get_thread_ident() != main_thread) {
    169                 pth_raise(*(pth_t *) main_thread, sig_num);
    170                 return;
    171         }
    172 #endif
    173         /* See NOTES section above */
    174         if (getpid() == main_pid) {
    175 #endif
    176                 Handlers[sig_num].tripped = 1;
    177                 /* Set is_tripped after setting .tripped, as it gets
    178                    cleared in PyErr_CheckSignals() before .tripped. */
    179                 is_tripped = 1;
    180                 Py_AddPendingCall(checksignals_witharg, NULL);
    181                 if (wakeup_fd != -1)
    182                         write(wakeup_fd, "\0", 1);
    183 #ifdef WITH_THREAD
    184         }
    185 #endif
     196    /* See NOTES section above */
     197    if (getpid() == main_pid)
     198#endif
     199    {
     200        trip_signal(sig_num);
     201    }
     202
     203#ifndef HAVE_SIGACTION
    186204#ifdef SIGCHLD
    187         if (sig_num == SIGCHLD) {
    188                 /* To avoid infinite recursion, this signal remains
    189                    reset until explicit re-instated.
    190                    Don't clear the 'func' field as it is our pointer
    191                    to the Python handler... */
    192                 return;
    193         }
    194 #endif
    195         PyOS_setsig(sig_num, signal_handler);
     205    /* To avoid infinite recursion, this signal remains
     206       reset until explicit re-instated.
     207       Don't clear the 'func' field as it is our pointer
     208       to the Python handler... */
     209    if (sig_num != SIGCHLD)
     210#endif
     211    /* If the handler was not set up with sigaction, reinstall it.  See
     212     * Python/pythonrun.c for the implementation of PyOS_setsig which
     213     * makes this true.  See also issue8354. */
     214    PyOS_setsig(sig_num, signal_handler);
     215#endif
     216    }
     217
     218    /* Issue #10311: asynchronously executing signal handlers should not
     219       mutate errno under the feet of unsuspecting C code. */
     220    errno = save_errno;
    196221}
    197222
     
    201226signal_alarm(PyObject *self, PyObject *args)
    202227{
    203         int t;
    204         if (!PyArg_ParseTuple(args, "i:alarm", &t))
    205                 return NULL;
    206         /* alarm() returns the number of seconds remaining */
    207         return PyInt_FromLong((long)alarm(t));
     228    int t;
     229    if (!PyArg_ParseTuple(args, "i:alarm", &t))
     230        return NULL;
     231    /* alarm() returns the number of seconds remaining */
     232    return PyInt_FromLong((long)alarm(t));
    208233}
    209234
     
    218243signal_pause(PyObject *self)
    219244{
    220         Py_BEGIN_ALLOW_THREADS
    221         (void)pause();
    222         Py_END_ALLOW_THREADS
    223         /* make sure that any exceptions that got raised are propagated
    224         * back into Python
    225         */
    226         if (PyErr_CheckSignals())
    227                 return NULL;
    228 
    229         Py_INCREF(Py_None);
    230         return Py_None;
     245    Py_BEGIN_ALLOW_THREADS
     246    (void)pause();
     247    Py_END_ALLOW_THREADS
     248    /* make sure that any exceptions that got raised are propagated
     249    * back into Python
     250    */
     251    if (PyErr_CheckSignals())
     252        return NULL;
     253
     254    Py_INCREF(Py_None);
     255    return Py_None;
    231256}
    232257PyDoc_STRVAR(pause_doc,
     
    241266signal_signal(PyObject *self, PyObject *args)
    242267{
    243         PyObject *obj;
    244         int sig_num;
    245         PyObject *old_handler;
    246         void (*func)(int);
    247         if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
    248                 return NULL;
     268    PyObject *obj;
     269    int sig_num;
     270    PyObject *old_handler;
     271    void (*func)(int);
     272    if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
     273        return NULL;
     274#ifdef MS_WINDOWS
     275    /* Validate that sig_num is one of the allowable signals */
     276    switch (sig_num) {
     277        case SIGABRT: break;
     278#ifdef SIGBREAK
     279        /* Issue #10003: SIGBREAK is not documented as permitted, but works
     280           and corresponds to CTRL_BREAK_EVENT. */
     281        case SIGBREAK: break;
     282#endif
     283        case SIGFPE: break;
     284        case SIGILL: break;
     285        case SIGINT: break;
     286        case SIGSEGV: break;
     287        case SIGTERM: break;
     288        default:
     289            PyErr_SetString(PyExc_ValueError, "invalid signal value");
     290            return NULL;
     291    }
     292#endif
    249293#ifdef WITH_THREAD
    250         if (PyThread_get_thread_ident() != main_thread) {
    251                 PyErr_SetString(PyExc_ValueError,
    252                                 "signal only works in main thread");
    253                 return NULL;
    254         }
    255 #endif
    256         if (sig_num < 1 || sig_num >= NSIG) {
    257                 PyErr_SetString(PyExc_ValueError,
    258                                 "signal number out of range");
    259                 return NULL;
    260         }
    261         if (obj == IgnoreHandler)
    262                 func = SIG_IGN;
    263         else if (obj == DefaultHandler)
    264                 func = SIG_DFL;
    265         else if (!PyCallable_Check(obj)) {
    266                 PyErr_SetString(PyExc_TypeError,
     294    if (PyThread_get_thread_ident() != main_thread) {
     295        PyErr_SetString(PyExc_ValueError,
     296                        "signal only works in main thread");
     297        return NULL;
     298    }
     299#endif
     300    if (sig_num < 1 || sig_num >= NSIG) {
     301        PyErr_SetString(PyExc_ValueError,
     302                        "signal number out of range");
     303        return NULL;
     304    }
     305    if (obj == IgnoreHandler)
     306        func = SIG_IGN;
     307    else if (obj == DefaultHandler)
     308        func = SIG_DFL;
     309    else if (!PyCallable_Check(obj)) {
     310        PyErr_SetString(PyExc_TypeError,
    267311"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
    268                 return NULL;
    269         }
    270         else
    271                 func = signal_handler;
    272         if (PyOS_setsig(sig_num, func) == SIG_ERR) {
    273                 PyErr_SetFromErrno(PyExc_RuntimeError);
    274                 return NULL;
    275         }
    276         old_handler = Handlers[sig_num].func;
    277         Handlers[sig_num].tripped = 0;
    278         Py_INCREF(obj);
    279         Handlers[sig_num].func = obj;
    280         return old_handler;
     312                return NULL;
     313    }
     314    else
     315        func = signal_handler;
     316    if (PyOS_setsig(sig_num, func) == SIG_ERR) {
     317        PyErr_SetFromErrno(PyExc_RuntimeError);
     318        return NULL;
     319    }
     320    old_handler = Handlers[sig_num].func;
     321    Handlers[sig_num].tripped = 0;
     322    Py_INCREF(obj);
     323    Handlers[sig_num].func = obj;
     324    if (old_handler != NULL)
     325        return old_handler;
     326    else
     327        Py_RETURN_NONE;
    281328}
    282329
     
    296343signal_getsignal(PyObject *self, PyObject *args)
    297344{
    298         int sig_num;
    299         PyObject *old_handler;
    300         if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
    301                 return NULL;
    302         if (sig_num < 1 || sig_num >= NSIG) {
    303                 PyErr_SetString(PyExc_ValueError,
    304                                 "signal number out of range");
    305                 return NULL;
    306         }
    307         old_handler = Handlers[sig_num].func;
    308         Py_INCREF(old_handler);
    309         return old_handler;
     345    int sig_num;
     346    PyObject *old_handler;
     347    if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
     348        return NULL;
     349    if (sig_num < 1 || sig_num >= NSIG) {
     350        PyErr_SetString(PyExc_ValueError,
     351                        "signal number out of range");
     352        return NULL;
     353    }
     354    old_handler = Handlers[sig_num].func;
     355    if (old_handler != NULL) {
     356        Py_INCREF(old_handler);
     357        return old_handler;
     358    }
     359    else {
     360        Py_RETURN_NONE;
     361    }
    310362}
    311363
     
    329381signal_siginterrupt(PyObject *self, PyObject *args)
    330382{
    331         int sig_num;
    332         int flag;
    333 
    334         if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
    335                 return NULL;
    336         if (sig_num < 1 || sig_num >= NSIG) {
    337                 PyErr_SetString(PyExc_ValueError,
    338                                 "signal number out of range");
    339                 return NULL;
    340         }
    341         if (siginterrupt(sig_num, flag)<0) {
    342                 PyErr_SetFromErrno(PyExc_RuntimeError);
    343                 return NULL;
    344         }
    345 
    346         Py_INCREF(Py_None);
    347         return Py_None;
     383    int sig_num;
     384    int flag;
     385
     386    if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
     387        return NULL;
     388    if (sig_num < 1 || sig_num >= NSIG) {
     389        PyErr_SetString(PyExc_ValueError,
     390                        "signal number out of range");
     391        return NULL;
     392    }
     393    if (siginterrupt(sig_num, flag)<0) {
     394        PyErr_SetFromErrno(PyExc_RuntimeError);
     395        return NULL;
     396    }
     397
     398    Py_INCREF(Py_None);
     399    return Py_None;
    348400}
    349401
     
    353405signal_set_wakeup_fd(PyObject *self, PyObject *args)
    354406{
    355         struct stat buf;
    356         int fd, old_fd;
    357         if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
    358                 return NULL;
     407    struct stat buf;
     408    int fd, old_fd;
     409    if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
     410        return NULL;
    359411#ifdef WITH_THREAD
    360         if (PyThread_get_thread_ident() != main_thread) {
    361                 PyErr_SetString(PyExc_ValueError,
    362                                 "set_wakeup_fd only works in main thread");
    363                 return NULL;
    364         }
    365 #endif
    366         if (fd != -1 && fstat(fd, &buf) != 0) {
    367                 PyErr_SetString(PyExc_ValueError, "invalid fd");
    368                 return NULL;
    369         }
    370         old_fd = wakeup_fd;
    371         wakeup_fd = fd;
    372         return PyLong_FromLong(old_fd);
     412    if (PyThread_get_thread_ident() != main_thread) {
     413        PyErr_SetString(PyExc_ValueError,
     414                        "set_wakeup_fd only works in main thread");
     415        return NULL;
     416    }
     417#endif
     418    if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) {
     419        PyErr_SetString(PyExc_ValueError, "invalid fd");
     420        return NULL;
     421    }
     422    old_fd = wakeup_fd;
     423    wakeup_fd = fd;
     424    return PyLong_FromLong(old_fd);
    373425}
    374426
     
    386438PySignal_SetWakeupFd(int fd)
    387439{
    388         int old_fd = wakeup_fd;
    389         if (fd < 0)
    390                 fd = -1;
    391         wakeup_fd = fd;
    392         return old_fd;
     440    int old_fd = wakeup_fd;
     441    if (fd < 0)
     442        fd = -1;
     443    wakeup_fd = fd;
     444    return old_fd;
    393445}
    394446
     
    404456
    405457    if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
    406         return NULL;
     458    return NULL;
    407459
    408460    timeval_from_double(first, &new.it_value);
     
    410462    /* Let OS check "which" value */
    411463    if (setitimer(which, &new, &old) != 0) {
    412         PyErr_SetFromErrno(ItimerError);
    413         return NULL;
     464    PyErr_SetFromErrno(ItimerError);
     465    return NULL;
    414466    }
    415467
     
    437489
    438490    if (!PyArg_ParseTuple(args, "i:getitimer", &which))
    439         return NULL;
     491    return NULL;
    440492
    441493    if (getitimer(which, &old) != 0) {
    442         PyErr_SetFromErrno(ItimerError);
    443         return NULL;
     494    PyErr_SetFromErrno(ItimerError);
     495    return NULL;
    444496    }
    445497
     
    457509static PyMethodDef signal_methods[] = {
    458510#ifdef HAVE_ALARM
    459         {"alarm",               signal_alarm, METH_VARARGS, alarm_doc},
     511    {"alarm",                   signal_alarm, METH_VARARGS, alarm_doc},
    460512#endif
    461513#ifdef HAVE_SETITIMER
     
    463515#endif
    464516#ifdef HAVE_GETITIMER
    465         {"getitimer",       signal_getitimer, METH_VARARGS, getitimer_doc},
    466 #endif
    467         {"signal",              signal_signal, METH_VARARGS, signal_doc},
    468         {"getsignal",           signal_getsignal, METH_VARARGS, getsignal_doc},
    469         {"set_wakeup_fd",       signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
     517    {"getitimer",       signal_getitimer, METH_VARARGS, getitimer_doc},
     518#endif
     519    {"signal",                  signal_signal, METH_VARARGS, signal_doc},
     520    {"getsignal",               signal_getsignal, METH_VARARGS, getsignal_doc},
     521    {"set_wakeup_fd",           signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
    470522#ifdef HAVE_SIGINTERRUPT
    471         {"siginterrupt",        signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
     523    {"siginterrupt",            signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
    472524#endif
    473525#ifdef HAVE_PAUSE
    474         {"pause",               (PyCFunction)signal_pause,
    475         METH_NOARGS,pause_doc},
    476 #endif
    477         {"default_int_handler", signal_default_int_handler,
    478         METH_VARARGS, default_int_handler_doc},
    479         {NULL,                  NULL}           /* sentinel */
     526    {"pause",                   (PyCFunction)signal_pause,
     527    METH_NOARGS,pause_doc},
     528#endif
     529    {"default_int_handler", signal_default_int_handler,
     530    METH_VARARGS, default_int_handler_doc},
     531    {NULL,                      NULL}           /* sentinel */
    480532};
    481533
     
    520572initsignal(void)
    521573{
    522         PyObject *m, *d, *x;
    523         int i;
     574    PyObject *m, *d, *x;
     575    int i;
    524576
    525577#ifdef WITH_THREAD
    526         main_thread = PyThread_get_thread_ident();
    527         main_pid = getpid();
    528 #endif
    529 
    530         /* Create the module and add the functions */
    531         m = Py_InitModule3("signal", signal_methods, module_doc);
    532         if (m == NULL)
    533                 return;
    534 
    535         /* Add some symbolic constants to the module */
    536         d = PyModule_GetDict(m);
    537 
    538         x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
    539         if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
    540                 goto finally;
    541 
    542         x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
    543         if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
    544                 goto finally;
    545 
    546         x = PyInt_FromLong((long)NSIG);
    547         if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
    548                 goto finally;
    549         Py_DECREF(x);
    550 
    551         x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
    552         if (!x)
    553                 goto finally;
    554         Py_INCREF(IntHandler);
    555 
    556         Handlers[0].tripped = 0;
    557         for (i = 1; i < NSIG; i++) {
    558                 void (*t)(int);
    559                 t = PyOS_getsig(i);
    560                 Handlers[i].tripped = 0;
    561                 if (t == SIG_DFL)
    562                         Handlers[i].func = DefaultHandler;
    563                 else if (t == SIG_IGN)
    564                         Handlers[i].func = IgnoreHandler;
    565                 else
    566                         Handlers[i].func = Py_None; /* None of our business */
    567                 Py_INCREF(Handlers[i].func);
    568         }
    569         if (Handlers[SIGINT].func == DefaultHandler) {
    570                 /* Install default int handler */
    571                 Py_INCREF(IntHandler);
    572                 Py_DECREF(Handlers[SIGINT].func);
    573                 Handlers[SIGINT].func = IntHandler;
    574                 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
    575         }
     578    main_thread = PyThread_get_thread_ident();
     579    main_pid = getpid();
     580#endif
     581
     582    /* Create the module and add the functions */
     583    m = Py_InitModule3("signal", signal_methods, module_doc);
     584    if (m == NULL)
     585        return;
     586
     587    /* Add some symbolic constants to the module */
     588    d = PyModule_GetDict(m);
     589
     590    x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
     591    if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
     592        goto finally;
     593
     594    x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
     595    if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
     596        goto finally;
     597
     598    x = PyInt_FromLong((long)NSIG);
     599    if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
     600        goto finally;
     601    Py_DECREF(x);
     602
     603    x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
     604    if (!x)
     605        goto finally;
     606    Py_INCREF(IntHandler);
     607
     608    Handlers[0].tripped = 0;
     609    for (i = 1; i < NSIG; i++) {
     610        void (*t)(int);
     611        t = PyOS_getsig(i);
     612        Handlers[i].tripped = 0;
     613        if (t == SIG_DFL)
     614            Handlers[i].func = DefaultHandler;
     615        else if (t == SIG_IGN)
     616            Handlers[i].func = IgnoreHandler;
     617        else
     618            Handlers[i].func = Py_None; /* None of our business */
     619        Py_INCREF(Handlers[i].func);
     620    }
     621    if (Handlers[SIGINT].func == DefaultHandler) {
     622        /* Install default int handler */
     623        Py_INCREF(IntHandler);
     624        Py_DECREF(Handlers[SIGINT].func);
     625        Handlers[SIGINT].func = IntHandler;
     626        old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
     627    }
    576628
    577629#ifdef SIGHUP
    578         x = PyInt_FromLong(SIGHUP);
    579         PyDict_SetItemString(d, "SIGHUP", x);
    580         Py_XDECREF(x);
     630    x = PyInt_FromLong(SIGHUP);
     631    PyDict_SetItemString(d, "SIGHUP", x);
     632    Py_XDECREF(x);
    581633#endif
    582634#ifdef SIGINT
    583         x = PyInt_FromLong(SIGINT);
    584         PyDict_SetItemString(d, "SIGINT", x);
    585         Py_XDECREF(x);
     635    x = PyInt_FromLong(SIGINT);
     636    PyDict_SetItemString(d, "SIGINT", x);
     637    Py_XDECREF(x);
    586638#endif
    587639#ifdef SIGBREAK
    588         x = PyInt_FromLong(SIGBREAK);
    589         PyDict_SetItemString(d, "SIGBREAK", x);
    590         Py_XDECREF(x);
     640    x = PyInt_FromLong(SIGBREAK);
     641    PyDict_SetItemString(d, "SIGBREAK", x);
     642    Py_XDECREF(x);
    591643#endif
    592644#ifdef SIGQUIT
    593         x = PyInt_FromLong(SIGQUIT);
    594         PyDict_SetItemString(d, "SIGQUIT", x);
    595         Py_XDECREF(x);
     645    x = PyInt_FromLong(SIGQUIT);
     646    PyDict_SetItemString(d, "SIGQUIT", x);
     647    Py_XDECREF(x);
    596648#endif
    597649#ifdef SIGILL
    598         x = PyInt_FromLong(SIGILL);
    599         PyDict_SetItemString(d, "SIGILL", x);
    600         Py_XDECREF(x);
     650    x = PyInt_FromLong(SIGILL);
     651    PyDict_SetItemString(d, "SIGILL", x);
     652    Py_XDECREF(x);
    601653#endif
    602654#ifdef SIGTRAP
    603         x = PyInt_FromLong(SIGTRAP);
    604         PyDict_SetItemString(d, "SIGTRAP", x);
    605         Py_XDECREF(x);
     655    x = PyInt_FromLong(SIGTRAP);
     656    PyDict_SetItemString(d, "SIGTRAP", x);
     657    Py_XDECREF(x);
    606658#endif
    607659#ifdef SIGIOT
    608         x = PyInt_FromLong(SIGIOT);
    609         PyDict_SetItemString(d, "SIGIOT", x);
    610         Py_XDECREF(x);
     660    x = PyInt_FromLong(SIGIOT);
     661    PyDict_SetItemString(d, "SIGIOT", x);
     662    Py_XDECREF(x);
    611663#endif
    612664#ifdef SIGABRT
    613         x = PyInt_FromLong(SIGABRT);
    614         PyDict_SetItemString(d, "SIGABRT", x);
    615         Py_XDECREF(x);
     665    x = PyInt_FromLong(SIGABRT);
     666    PyDict_SetItemString(d, "SIGABRT", x);
     667    Py_XDECREF(x);
    616668#endif
    617669#ifdef SIGEMT
    618         x = PyInt_FromLong(SIGEMT);
    619         PyDict_SetItemString(d, "SIGEMT", x);
    620         Py_XDECREF(x);
     670    x = PyInt_FromLong(SIGEMT);
     671    PyDict_SetItemString(d, "SIGEMT", x);
     672    Py_XDECREF(x);
    621673#endif
    622674#ifdef SIGFPE
    623         x = PyInt_FromLong(SIGFPE);
    624         PyDict_SetItemString(d, "SIGFPE", x);
    625         Py_XDECREF(x);
     675    x = PyInt_FromLong(SIGFPE);
     676    PyDict_SetItemString(d, "SIGFPE", x);
     677    Py_XDECREF(x);
    626678#endif
    627679#ifdef SIGKILL
    628         x = PyInt_FromLong(SIGKILL);
    629         PyDict_SetItemString(d, "SIGKILL", x);
    630         Py_XDECREF(x);
     680    x = PyInt_FromLong(SIGKILL);
     681    PyDict_SetItemString(d, "SIGKILL", x);
     682    Py_XDECREF(x);
    631683#endif
    632684#ifdef SIGBUS
    633         x = PyInt_FromLong(SIGBUS);
    634         PyDict_SetItemString(d, "SIGBUS", x);
    635         Py_XDECREF(x);
     685    x = PyInt_FromLong(SIGBUS);
     686    PyDict_SetItemString(d, "SIGBUS", x);
     687    Py_XDECREF(x);
    636688#endif
    637689#ifdef SIGSEGV
    638         x = PyInt_FromLong(SIGSEGV);
    639         PyDict_SetItemString(d, "SIGSEGV", x);
    640         Py_XDECREF(x);
     690    x = PyInt_FromLong(SIGSEGV);
     691    PyDict_SetItemString(d, "SIGSEGV", x);
     692    Py_XDECREF(x);
    641693#endif
    642694#ifdef SIGSYS
    643         x = PyInt_FromLong(SIGSYS);
    644         PyDict_SetItemString(d, "SIGSYS", x);
    645         Py_XDECREF(x);
     695    x = PyInt_FromLong(SIGSYS);
     696    PyDict_SetItemString(d, "SIGSYS", x);
     697    Py_XDECREF(x);
    646698#endif
    647699#ifdef SIGPIPE
    648         x = PyInt_FromLong(SIGPIPE);
    649         PyDict_SetItemString(d, "SIGPIPE", x);
    650         Py_XDECREF(x);
     700    x = PyInt_FromLong(SIGPIPE);
     701    PyDict_SetItemString(d, "SIGPIPE", x);
     702    Py_XDECREF(x);
    651703#endif
    652704#ifdef SIGALRM
    653         x = PyInt_FromLong(SIGALRM);
    654         PyDict_SetItemString(d, "SIGALRM", x);
    655         Py_XDECREF(x);
     705    x = PyInt_FromLong(SIGALRM);
     706    PyDict_SetItemString(d, "SIGALRM", x);
     707    Py_XDECREF(x);
    656708#endif
    657709#ifdef SIGTERM
    658         x = PyInt_FromLong(SIGTERM);
    659         PyDict_SetItemString(d, "SIGTERM", x);
    660         Py_XDECREF(x);
     710    x = PyInt_FromLong(SIGTERM);
     711    PyDict_SetItemString(d, "SIGTERM", x);
     712    Py_XDECREF(x);
    661713#endif
    662714#ifdef SIGUSR1
    663         x = PyInt_FromLong(SIGUSR1);
    664         PyDict_SetItemString(d, "SIGUSR1", x);
    665         Py_XDECREF(x);
     715    x = PyInt_FromLong(SIGUSR1);
     716    PyDict_SetItemString(d, "SIGUSR1", x);
     717    Py_XDECREF(x);
    666718#endif
    667719#ifdef SIGUSR2
    668         x = PyInt_FromLong(SIGUSR2);
    669         PyDict_SetItemString(d, "SIGUSR2", x);
    670         Py_XDECREF(x);
     720    x = PyInt_FromLong(SIGUSR2);
     721    PyDict_SetItemString(d, "SIGUSR2", x);
     722    Py_XDECREF(x);
    671723#endif
    672724#ifdef SIGCLD
    673         x = PyInt_FromLong(SIGCLD);
    674         PyDict_SetItemString(d, "SIGCLD", x);
    675         Py_XDECREF(x);
     725    x = PyInt_FromLong(SIGCLD);
     726    PyDict_SetItemString(d, "SIGCLD", x);
     727    Py_XDECREF(x);
    676728#endif
    677729#ifdef SIGCHLD
    678         x = PyInt_FromLong(SIGCHLD);
    679         PyDict_SetItemString(d, "SIGCHLD", x);
    680         Py_XDECREF(x);
     730    x = PyInt_FromLong(SIGCHLD);
     731    PyDict_SetItemString(d, "SIGCHLD", x);
     732    Py_XDECREF(x);
    681733#endif
    682734#ifdef SIGPWR
    683         x = PyInt_FromLong(SIGPWR);
    684         PyDict_SetItemString(d, "SIGPWR", x);
    685         Py_XDECREF(x);
     735    x = PyInt_FromLong(SIGPWR);
     736    PyDict_SetItemString(d, "SIGPWR", x);
     737    Py_XDECREF(x);
    686738#endif
    687739#ifdef SIGIO
    688         x = PyInt_FromLong(SIGIO);
    689         PyDict_SetItemString(d, "SIGIO", x);
    690         Py_XDECREF(x);
     740    x = PyInt_FromLong(SIGIO);
     741    PyDict_SetItemString(d, "SIGIO", x);
     742    Py_XDECREF(x);
    691743#endif
    692744#ifdef SIGURG
    693         x = PyInt_FromLong(SIGURG);
    694         PyDict_SetItemString(d, "SIGURG", x);
    695         Py_XDECREF(x);
     745    x = PyInt_FromLong(SIGURG);
     746    PyDict_SetItemString(d, "SIGURG", x);
     747    Py_XDECREF(x);
    696748#endif
    697749#ifdef SIGWINCH
    698         x = PyInt_FromLong(SIGWINCH);
    699         PyDict_SetItemString(d, "SIGWINCH", x);
    700         Py_XDECREF(x);
     750    x = PyInt_FromLong(SIGWINCH);
     751    PyDict_SetItemString(d, "SIGWINCH", x);
     752    Py_XDECREF(x);
    701753#endif
    702754#ifdef SIGPOLL
    703         x = PyInt_FromLong(SIGPOLL);
    704         PyDict_SetItemString(d, "SIGPOLL", x);
    705         Py_XDECREF(x);
     755    x = PyInt_FromLong(SIGPOLL);
     756    PyDict_SetItemString(d, "SIGPOLL", x);
     757    Py_XDECREF(x);
    706758#endif
    707759#ifdef SIGSTOP
    708         x = PyInt_FromLong(SIGSTOP);
    709         PyDict_SetItemString(d, "SIGSTOP", x);
    710         Py_XDECREF(x);
     760    x = PyInt_FromLong(SIGSTOP);
     761    PyDict_SetItemString(d, "SIGSTOP", x);
     762    Py_XDECREF(x);
    711763#endif
    712764#ifdef SIGTSTP
    713         x = PyInt_FromLong(SIGTSTP);
    714         PyDict_SetItemString(d, "SIGTSTP", x);
    715         Py_XDECREF(x);
     765    x = PyInt_FromLong(SIGTSTP);
     766    PyDict_SetItemString(d, "SIGTSTP", x);
     767    Py_XDECREF(x);
    716768#endif
    717769#ifdef SIGCONT
    718         x = PyInt_FromLong(SIGCONT);
    719         PyDict_SetItemString(d, "SIGCONT", x);
    720         Py_XDECREF(x);
     770    x = PyInt_FromLong(SIGCONT);
     771    PyDict_SetItemString(d, "SIGCONT", x);
     772    Py_XDECREF(x);
    721773#endif
    722774#ifdef SIGTTIN
    723         x = PyInt_FromLong(SIGTTIN);
    724         PyDict_SetItemString(d, "SIGTTIN", x);
    725         Py_XDECREF(x);
     775    x = PyInt_FromLong(SIGTTIN);
     776    PyDict_SetItemString(d, "SIGTTIN", x);
     777    Py_XDECREF(x);
    726778#endif
    727779#ifdef SIGTTOU
    728         x = PyInt_FromLong(SIGTTOU);
    729         PyDict_SetItemString(d, "SIGTTOU", x);
    730         Py_XDECREF(x);
     780    x = PyInt_FromLong(SIGTTOU);
     781    PyDict_SetItemString(d, "SIGTTOU", x);
     782    Py_XDECREF(x);
    731783#endif
    732784#ifdef SIGVTALRM
    733         x = PyInt_FromLong(SIGVTALRM);
    734         PyDict_SetItemString(d, "SIGVTALRM", x);
    735         Py_XDECREF(x);
     785    x = PyInt_FromLong(SIGVTALRM);
     786    PyDict_SetItemString(d, "SIGVTALRM", x);
     787    Py_XDECREF(x);
    736788#endif
    737789#ifdef SIGPROF
    738         x = PyInt_FromLong(SIGPROF);
    739         PyDict_SetItemString(d, "SIGPROF", x);
    740         Py_XDECREF(x);
     790    x = PyInt_FromLong(SIGPROF);
     791    PyDict_SetItemString(d, "SIGPROF", x);
     792    Py_XDECREF(x);
    741793#endif
    742794#ifdef SIGXCPU
    743         x = PyInt_FromLong(SIGXCPU);
    744         PyDict_SetItemString(d, "SIGXCPU", x);
    745         Py_XDECREF(x);
     795    x = PyInt_FromLong(SIGXCPU);
     796    PyDict_SetItemString(d, "SIGXCPU", x);
     797    Py_XDECREF(x);
    746798#endif
    747799#ifdef SIGXFSZ
    748         x = PyInt_FromLong(SIGXFSZ);
    749         PyDict_SetItemString(d, "SIGXFSZ", x);
    750         Py_XDECREF(x);
     800    x = PyInt_FromLong(SIGXFSZ);
     801    PyDict_SetItemString(d, "SIGXFSZ", x);
     802    Py_XDECREF(x);
    751803#endif
    752804#ifdef SIGRTMIN
    753         x = PyInt_FromLong(SIGRTMIN);
    754         PyDict_SetItemString(d, "SIGRTMIN", x);
    755         Py_XDECREF(x);
     805    x = PyInt_FromLong(SIGRTMIN);
     806    PyDict_SetItemString(d, "SIGRTMIN", x);
     807    Py_XDECREF(x);
    756808#endif
    757809#ifdef SIGRTMAX
    758         x = PyInt_FromLong(SIGRTMAX);
    759         PyDict_SetItemString(d, "SIGRTMAX", x);
    760         Py_XDECREF(x);
     810    x = PyInt_FromLong(SIGRTMAX);
     811    PyDict_SetItemString(d, "SIGRTMAX", x);
     812    Py_XDECREF(x);
    761813#endif
    762814#ifdef SIGINFO
    763         x = PyInt_FromLong(SIGINFO);
    764         PyDict_SetItemString(d, "SIGINFO", x);
    765         Py_XDECREF(x);
     815    x = PyInt_FromLong(SIGINFO);
     816    PyDict_SetItemString(d, "SIGINFO", x);
     817    Py_XDECREF(x);
    766818#endif
    767819
     
    783835
    784836#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
    785     ItimerError = PyErr_NewException("signal.ItimerError", 
    786          PyExc_IOError, NULL);
     837    ItimerError = PyErr_NewException("signal.ItimerError",
     838     PyExc_IOError, NULL);
    787839    if (ItimerError != NULL)
    788         PyDict_SetItemString(d, "ItimerError", ItimerError);
    789 #endif
    790 
    791         if (!PyErr_Occurred())
    792                 return;
    793 
    794         /* Check for errors */
     840    PyDict_SetItemString(d, "ItimerError", ItimerError);
     841#endif
     842
     843#ifdef CTRL_C_EVENT
     844    x = PyInt_FromLong(CTRL_C_EVENT);
     845    PyDict_SetItemString(d, "CTRL_C_EVENT", x);
     846    Py_DECREF(x);
     847#endif
     848
     849#ifdef CTRL_BREAK_EVENT
     850    x = PyInt_FromLong(CTRL_BREAK_EVENT);
     851    PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
     852    Py_DECREF(x);
     853#endif
     854
     855    if (!PyErr_Occurred())
     856        return;
     857
     858    /* Check for errors */
    795859  finally:
    796         return;
     860    return;
    797861}
    798862
     
    800864finisignal(void)
    801865{
    802         int i;
    803         PyObject *func;
    804 
    805         PyOS_setsig(SIGINT, old_siginthandler);
    806         old_siginthandler = SIG_DFL;
    807 
    808         for (i = 1; i < NSIG; i++) {
    809                 func = Handlers[i].func;
    810                 Handlers[i].tripped = 0;
    811                 Handlers[i].func = NULL;
    812                 if (i != SIGINT && func != NULL && func != Py_None &&
    813                     func != DefaultHandler && func != IgnoreHandler)
    814                         PyOS_setsig(i, SIG_DFL);
    815                 Py_XDECREF(func);
    816         }
    817 
    818         Py_XDECREF(IntHandler);
    819         IntHandler = NULL;
    820         Py_XDECREF(DefaultHandler);
    821         DefaultHandler = NULL;
    822         Py_XDECREF(IgnoreHandler);
    823         IgnoreHandler = NULL;
     866    int i;
     867    PyObject *func;
     868
     869    PyOS_setsig(SIGINT, old_siginthandler);
     870    old_siginthandler = SIG_DFL;
     871
     872    for (i = 1; i < NSIG; i++) {
     873        func = Handlers[i].func;
     874        Handlers[i].tripped = 0;
     875        Handlers[i].func = NULL;
     876        if (i != SIGINT && func != NULL && func != Py_None &&
     877            func != DefaultHandler && func != IgnoreHandler)
     878            PyOS_setsig(i, SIG_DFL);
     879        Py_XDECREF(func);
     880    }
     881
     882    Py_XDECREF(IntHandler);
     883    IntHandler = NULL;
     884    Py_XDECREF(DefaultHandler);
     885    DefaultHandler = NULL;
     886    Py_XDECREF(IgnoreHandler);
     887    IgnoreHandler = NULL;
    824888}
    825889
     
    829893PyErr_CheckSignals(void)
    830894{
    831         int i;
    832         PyObject *f;
    833 
    834         if (!is_tripped)
    835                 return 0;
     895    int i;
     896    PyObject *f;
     897
     898    if (!is_tripped)
     899        return 0;
    836900
    837901#ifdef WITH_THREAD
    838         if (PyThread_get_thread_ident() != main_thread)
    839                 return 0;
    840 #endif
    841 
    842         /*
    843          * The is_stripped variable is meant to speed up the calls to
    844         * PyErr_CheckSignals (both directly or via pending calls) when no
    845         * signal has arrived. This variable is set to 1 when a signal arrives
    846         * and it is set to 0 here, when we know some signals arrived. This way
    847         * we can run the registered handlers with no signals blocked.
    848         *
    849         * NOTE: with this approach we can have a situation where is_tripped is
    850         *       1 but we have no more signals to handle (Handlers[i].tripped
    851         *       is 0 for every signal i). This won't do us any harm (except
    852         *       we're gonna spent some cycles for nothing). This happens when
    853         *       we receive a signal i after we zero is_tripped and before we
    854         *       check Handlers[i].tripped.
    855         */
    856         is_tripped = 0;
    857 
    858         if (!(f = (PyObject *)PyEval_GetFrame()))
    859                 f = Py_None;
    860 
    861         for (i = 1; i < NSIG; i++) {
    862                 if (Handlers[i].tripped) {
    863                         PyObject *result = NULL;
    864                         PyObject *arglist = Py_BuildValue("(iO)", i, f);
    865                         Handlers[i].tripped = 0;
    866 
    867                         if (arglist) {
    868                                 result = PyEval_CallObject(Handlers[i].func,
    869                                                            arglist);
    870                                 Py_DECREF(arglist);
    871                         }
    872                         if (!result)
    873                                 return -1;
    874 
    875                         Py_DECREF(result);
    876                 }
    877         }
    878 
    879         return 0;
     902    if (PyThread_get_thread_ident() != main_thread)
     903        return 0;
     904#endif
     905
     906    /*
     907     * The is_tripped variable is meant to speed up the calls to
     908    * PyErr_CheckSignals (both directly or via pending calls) when no
     909    * signal has arrived. This variable is set to 1 when a signal arrives
     910    * and it is set to 0 here, when we know some signals arrived. This way
     911    * we can run the registered handlers with no signals blocked.
     912    *
     913    * NOTE: with this approach we can have a situation where is_tripped is
     914    *       1 but we have no more signals to handle (Handlers[i].tripped
     915    *       is 0 for every signal i). This won't do us any harm (except
     916    *       we're gonna spent some cycles for nothing). This happens when
     917    *       we receive a signal i after we zero is_tripped and before we
     918    *       check Handlers[i].tripped.
     919    */
     920    is_tripped = 0;
     921
     922    if (!(f = (PyObject *)PyEval_GetFrame()))
     923        f = Py_None;
     924
     925    for (i = 1; i < NSIG; i++) {
     926        if (Handlers[i].tripped) {
     927            PyObject *result = NULL;
     928            PyObject *arglist = Py_BuildValue("(iO)", i, f);
     929            Handlers[i].tripped = 0;
     930
     931            if (arglist) {
     932                result = PyEval_CallObject(Handlers[i].func,
     933                                           arglist);
     934                Py_DECREF(arglist);
     935            }
     936            if (!result)
     937                return -1;
     938
     939            Py_DECREF(result);
     940        }
     941    }
     942
     943    return 0;
    880944}
    881945
     
    887951PyErr_SetInterrupt(void)
    888952{
    889         is_tripped = 1;
    890         Handlers[SIGINT].tripped = 1;
    891         Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
     953    trip_signal(SIGINT);
    892954}
    893955
     
    895957PyOS_InitInterrupts(void)
    896958{
    897         initsignal();
    898         _PyImport_FixupExtension("signal", "signal");
     959    initsignal();
     960    _PyImport_FixupExtension("signal", "signal");
    899961}
    900962
     
    902964PyOS_FiniInterrupts(void)
    903965{
    904         finisignal();
     966    finisignal();
    905967}
    906968
     
    908970PyOS_InterruptOccurred(void)
    909971{
    910         if (Handlers[SIGINT].tripped) {
     972    if (Handlers[SIGINT].tripped) {
    911973#ifdef WITH_THREAD
    912                 if (PyThread_get_thread_ident() != main_thread)
    913                         return 0;
    914 #endif
    915                 Handlers[SIGINT].tripped = 0;
    916                 return 1;
    917         }
    918         return 0;
     974        if (PyThread_get_thread_ident() != main_thread)
     975            return 0;
     976#endif
     977        Handlers[SIGINT].tripped = 0;
     978        return 1;
     979    }
     980    return 0;
     981}
     982
     983static void
     984_clear_pending_signals(void)
     985{
     986    int i;
     987    if (!is_tripped)
     988        return;
     989    is_tripped = 0;
     990    for (i = 1; i < NSIG; ++i) {
     991        Handlers[i].tripped = 0;
     992    }
    919993}
    920994
     
    922996PyOS_AfterFork(void)
    923997{
     998    /* Clear the signal flags after forking so that they aren't handled
     999     * in both processes if they came in just before the fork() but before
     1000     * the interpreter had an opportunity to call the handlers.  issue9535. */
     1001    _clear_pending_signals();
    9241002#ifdef WITH_THREAD
    925         PyEval_ReInitThreads();
    926         main_thread = PyThread_get_thread_ident();
    927         main_pid = getpid();
    928         _PyImport_ReInitLock();
    929         PyThread_ReInitTLS();
    930 #endif
    931 }
     1003    /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
     1004     * can be called safely. */
     1005    PyThread_ReInitTLS();
     1006    PyEval_ReInitThreads();
     1007    main_thread = PyThread_get_thread_ident();
     1008    main_pid = getpid();
     1009    _PyImport_ReInitLock();
     1010#endif
     1011}
Note: See TracChangeset for help on using the changeset viewer.