Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2 | /* Signal module -- many thanks to Lance Ellinghaus */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 4 | /* XXX Signals should be recorded per thread, now we have thread state. */ |
| 5 | |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 6 | #include "Python.h" |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 7 | #include "intrcheck.h" |
| 8 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9 | #ifdef MS_WINDOWS |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 10 | #include <Windows.h> |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 11 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 12 | #include <process.h> |
| 13 | #endif |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 14 | #endif |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 15 | |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 16 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 17 | #include <signal.h> |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 18 | #endif |
| 19 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 21 | #endif |
Martin v. Löwis | b74d084 | 2008-03-24 13:54:23 +0000 | [diff] [blame] | 22 | #ifdef HAVE_SYS_TIME_H |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 23 | #include <sys/time.h> |
Martin v. Löwis | b74d084 | 2008-03-24 13:54:23 +0000 | [diff] [blame] | 24 | #endif |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 26 | #ifndef SIG_ERR |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 27 | #define SIG_ERR ((PyOS_sighandler_t)(-1)) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 30 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 31 | #define NSIG 12 |
| 32 | #include <process.h> |
| 33 | #endif |
| 34 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 35 | #ifndef NSIG |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 36 | # if defined(_NSIG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 37 | # define NSIG _NSIG /* For BSD/SysV */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 38 | # elif defined(_SIGMAX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 39 | # define NSIG (_SIGMAX + 1) /* For QNX */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 40 | # elif defined(SIGMAX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 41 | # define NSIG (SIGMAX + 1) /* For djgpp */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 42 | # else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 43 | # define NSIG 64 /* Use a reasonable default value */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 44 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 48 | /* |
| 49 | NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS |
| 50 | |
| 51 | When threads are supported, we want the following semantics: |
| 52 | |
| 53 | - only the main thread can set a signal handler |
| 54 | - any thread can get a signal handler |
| 55 | - signals are only delivered to the main thread |
| 56 | |
| 57 | I.e. we don't support "synchronous signals" like SIGFPE (catching |
| 58 | this doesn't make much sense in Python anyway) nor do we support |
| 59 | signals as a means of inter-thread communication, since not all |
| 60 | thread implementations support that (at least our thread library |
| 61 | doesn't). |
| 62 | |
| 63 | We still have the problem that in some implementations signals |
| 64 | generated by the keyboard (e.g. SIGINT) are delivered to all |
| 65 | threads (e.g. SGI), while in others (e.g. Solaris) such signals are |
| 66 | delivered to one random thread (an intermediate possibility would |
Guido van Rossum | a3c04b0 | 1995-01-12 11:29:01 +0000 | [diff] [blame] | 67 | be to deliver it to the main thread -- POSIX?). For now, we have |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 68 | a working implementation that works in all three cases -- the |
| 69 | handler ignores signals if getpid() isn't the same as in the main |
| 70 | thread. XXX This is a hack. |
| 71 | |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 72 | GNU pth is a user-space threading library, and as such, all threads |
| 73 | run within the same process. In this case, if the currently running |
| 74 | thread is not the main_thread, send the signal to the main_thread. |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 75 | */ |
| 76 | |
| 77 | #ifdef WITH_THREAD |
Guido van Rossum | 295b8e5 | 1997-06-06 21:16:41 +0000 | [diff] [blame] | 78 | #include <sys/types.h> /* For pid_t */ |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 79 | #include "pythread.h" |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 80 | static long main_thread; |
| 81 | static pid_t main_pid; |
| 82 | #endif |
| 83 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 84 | static struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 85 | int tripped; |
| 86 | PyObject *func; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 87 | } Handlers[NSIG]; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 89 | static sig_atomic_t wakeup_fd = -1; |
| 90 | |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 91 | /* Speed up sigcheck() when none tripped */ |
| 92 | static volatile sig_atomic_t is_tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 93 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 94 | static PyObject *DefaultHandler; |
| 95 | static PyObject *IgnoreHandler; |
| 96 | static PyObject *IntHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 97 | |
Martin v. Löwis | f58de1b | 2001-03-06 12:13:56 +0000 | [diff] [blame] | 98 | /* On Solaris 8, gcc will produce a warning that the function |
| 99 | declaration is not a prototype. This is caused by the definition of |
| 100 | SIG_DFL as (void (*)())0; the correct declaration would have been |
| 101 | (void (*)(int))0. */ |
| 102 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 103 | static PyOS_sighandler_t old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 104 | |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 105 | #ifdef HAVE_GETITIMER |
| 106 | static PyObject *ItimerError; |
| 107 | |
| 108 | /* auxiliary functions for setitimer/getitimer */ |
| 109 | static void |
| 110 | timeval_from_double(double d, struct timeval *tv) |
| 111 | { |
| 112 | tv->tv_sec = floor(d); |
| 113 | tv->tv_usec = fmod(d, 1.0) * 1000000.0; |
| 114 | } |
| 115 | |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 116 | Py_LOCAL_INLINE(double) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 117 | double_from_timeval(struct timeval *tv) |
| 118 | { |
| 119 | return tv->tv_sec + (double)(tv->tv_usec / 1000000.0); |
| 120 | } |
| 121 | |
| 122 | static PyObject * |
| 123 | itimer_retval(struct itimerval *iv) |
| 124 | { |
| 125 | PyObject *r, *v; |
| 126 | |
| 127 | r = PyTuple_New(2); |
| 128 | if (r == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 129 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 130 | |
| 131 | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 132 | Py_DECREF(r); |
| 133 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | PyTuple_SET_ITEM(r, 0, v); |
| 137 | |
| 138 | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 139 | Py_DECREF(r); |
| 140 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | PyTuple_SET_ITEM(r, 1, v); |
| 144 | |
| 145 | return r; |
| 146 | } |
| 147 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 149 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 150 | signal_default_int_handler(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 151 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 152 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 153 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 156 | PyDoc_STRVAR(default_int_handler_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 157 | "default_int_handler(...)\n\ |
| 158 | \n\ |
Michael W. Hudson | 24ec211 | 2004-06-17 15:55:53 +0000 | [diff] [blame] | 159 | The default handler for SIGINT installed by Python.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 160 | It raises KeyboardInterrupt."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 161 | |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 162 | |
| 163 | static int |
| 164 | checksignals_witharg(void * unused) |
| 165 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 166 | return PyErr_CheckSignals(); |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 169 | static void |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 170 | signal_handler(int sig_num) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 171 | { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 172 | #ifdef WITH_THREAD |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 173 | #ifdef WITH_PTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 174 | if (PyThread_get_thread_ident() != main_thread) { |
| 175 | pth_raise(*(pth_t *) main_thread, sig_num); |
| 176 | return; |
| 177 | } |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 178 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 179 | /* See NOTES section above */ |
| 180 | if (getpid() == main_pid) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 181 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 182 | Handlers[sig_num].tripped = 1; |
| 183 | /* Set is_tripped after setting .tripped, as it gets |
| 184 | cleared in PyErr_CheckSignals() before .tripped. */ |
| 185 | is_tripped = 1; |
| 186 | Py_AddPendingCall(checksignals_witharg, NULL); |
| 187 | if (wakeup_fd != -1) |
| 188 | write(wakeup_fd, "\0", 1); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 189 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 190 | } |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 191 | #endif |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 192 | #ifdef SIGCHLD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 193 | if (sig_num == SIGCHLD) { |
| 194 | /* To avoid infinite recursion, this signal remains |
| 195 | reset until explicit re-instated. |
| 196 | Don't clear the 'func' field as it is our pointer |
| 197 | to the Python handler... */ |
| 198 | return; |
| 199 | } |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 200 | #endif |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 201 | #ifndef HAVE_SIGACTION |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 202 | /* If the handler was not set up with sigaction, reinstall it. See |
| 203 | * Python/pythonrun.c for the implementation of PyOS_setsig which |
| 204 | * makes this true. See also issue8354. */ |
| 205 | PyOS_setsig(sig_num, signal_handler); |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 206 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 207 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 210 | #ifdef HAVE_ALARM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 211 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 212 | signal_alarm(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 214 | int t; |
| 215 | if (!PyArg_ParseTuple(args, "i:alarm", &t)) |
| 216 | return NULL; |
| 217 | /* alarm() returns the number of seconds remaining */ |
| 218 | return PyInt_FromLong((long)alarm(t)); |
Guido van Rossum | aa0f4c7 | 1994-08-23 13:49:37 +0000 | [diff] [blame] | 219 | } |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 221 | PyDoc_STRVAR(alarm_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 222 | "alarm(seconds)\n\ |
| 223 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 224 | Arrange for SIGALRM to arrive after the given number of seconds."); |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 225 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 227 | #ifdef HAVE_PAUSE |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 228 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 229 | signal_pause(PyObject *self) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 230 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | Py_BEGIN_ALLOW_THREADS |
| 232 | (void)pause(); |
| 233 | Py_END_ALLOW_THREADS |
| 234 | /* make sure that any exceptions that got raised are propagated |
| 235 | * back into Python |
| 236 | */ |
| 237 | if (PyErr_CheckSignals()) |
| 238 | return NULL; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 240 | Py_INCREF(Py_None); |
| 241 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 242 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 243 | PyDoc_STRVAR(pause_doc, |
Barry Warsaw | 1ee36ff | 1998-07-21 22:41:18 +0000 | [diff] [blame] | 244 | "pause()\n\ |
| 245 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 246 | Wait until a signal arrives."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 248 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 251 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 252 | signal_signal(PyObject *self, PyObject *args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 253 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 254 | PyObject *obj; |
| 255 | int sig_num; |
| 256 | PyObject *old_handler; |
| 257 | void (*func)(int); |
| 258 | if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj)) |
| 259 | return NULL; |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame] | 260 | #ifdef MS_WINDOWS |
| 261 | /* Validate that sig_num is one of the allowable signals */ |
Brian Curtin | 8426333 | 2010-09-06 16:17:50 +0000 | [diff] [blame] | 262 | switch (sig_num) { |
| 263 | case SIGABRT: break; |
| 264 | case SIGFPE: break; |
| 265 | case SIGILL: break; |
| 266 | case SIGINT: break; |
| 267 | case SIGSEGV: break; |
| 268 | case SIGTERM: break; |
| 269 | default: |
| 270 | PyErr_SetString(PyExc_ValueError, "invalid signal value"); |
| 271 | return NULL; |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame] | 272 | } |
| 273 | #endif |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 274 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 275 | if (PyThread_get_thread_ident() != main_thread) { |
| 276 | PyErr_SetString(PyExc_ValueError, |
| 277 | "signal only works in main thread"); |
| 278 | return NULL; |
| 279 | } |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 280 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 281 | if (sig_num < 1 || sig_num >= NSIG) { |
| 282 | PyErr_SetString(PyExc_ValueError, |
| 283 | "signal number out of range"); |
| 284 | return NULL; |
| 285 | } |
| 286 | if (obj == IgnoreHandler) |
| 287 | func = SIG_IGN; |
| 288 | else if (obj == DefaultHandler) |
| 289 | func = SIG_DFL; |
| 290 | else if (!PyCallable_Check(obj)) { |
| 291 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 292 | "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
| 295 | else |
| 296 | func = signal_handler; |
| 297 | if (PyOS_setsig(sig_num, func) == SIG_ERR) { |
| 298 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 299 | return NULL; |
| 300 | } |
| 301 | old_handler = Handlers[sig_num].func; |
| 302 | Handlers[sig_num].tripped = 0; |
| 303 | Py_INCREF(obj); |
| 304 | Handlers[sig_num].func = obj; |
| 305 | return old_handler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 308 | PyDoc_STRVAR(signal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 309 | "signal(sig, action) -> action\n\ |
| 310 | \n\ |
| 311 | Set the action for the given signal. The action can be SIG_DFL,\n\ |
| 312 | SIG_IGN, or a callable Python object. The previous action is\n\ |
| 313 | returned. See getsignal() for possible return values.\n\ |
| 314 | \n\ |
| 315 | *** IMPORTANT NOTICE ***\n\ |
| 316 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 317 | the first is the signal number, the second is the interrupted stack frame."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 320 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 321 | signal_getsignal(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 322 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 323 | int sig_num; |
| 324 | PyObject *old_handler; |
| 325 | if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num)) |
| 326 | return NULL; |
| 327 | if (sig_num < 1 || sig_num >= NSIG) { |
| 328 | PyErr_SetString(PyExc_ValueError, |
| 329 | "signal number out of range"); |
| 330 | return NULL; |
| 331 | } |
| 332 | old_handler = Handlers[sig_num].func; |
| 333 | Py_INCREF(old_handler); |
| 334 | return old_handler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 337 | PyDoc_STRVAR(getsignal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 338 | "getsignal(sig) -> action\n\ |
| 339 | \n\ |
| 340 | Return the current action for the given signal. The return value can be:\n\ |
| 341 | SIG_IGN -- if the signal is being ignored\n\ |
| 342 | SIG_DFL -- if the default action for the signal is in effect\n\ |
| 343 | None -- if an unknown handler is in effect\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 344 | anything else -- the callable Python object used as a handler"); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 345 | |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 346 | #ifdef HAVE_SIGINTERRUPT |
| 347 | PyDoc_STRVAR(siginterrupt_doc, |
| 348 | "siginterrupt(sig, flag) -> None\n\ |
| 349 | change system call restart behaviour: if flag is False, system calls\n\ |
| 350 | will be restarted when interrupted by signal sig, else system calls\n\ |
| 351 | will be interrupted."); |
| 352 | |
| 353 | static PyObject * |
| 354 | signal_siginterrupt(PyObject *self, PyObject *args) |
| 355 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 356 | int sig_num; |
| 357 | int flag; |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 359 | if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag)) |
| 360 | return NULL; |
| 361 | if (sig_num < 1 || sig_num >= NSIG) { |
| 362 | PyErr_SetString(PyExc_ValueError, |
| 363 | "signal number out of range"); |
| 364 | return NULL; |
| 365 | } |
| 366 | if (siginterrupt(sig_num, flag)<0) { |
| 367 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 368 | return NULL; |
| 369 | } |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 370 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 371 | Py_INCREF(Py_None); |
| 372 | return Py_None; |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | #endif |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 377 | static PyObject * |
| 378 | signal_set_wakeup_fd(PyObject *self, PyObject *args) |
| 379 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 380 | struct stat buf; |
| 381 | int fd, old_fd; |
| 382 | if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd)) |
| 383 | return NULL; |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 384 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 385 | if (PyThread_get_thread_ident() != main_thread) { |
| 386 | PyErr_SetString(PyExc_ValueError, |
| 387 | "set_wakeup_fd only works in main thread"); |
| 388 | return NULL; |
| 389 | } |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 390 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 391 | if (fd != -1 && fstat(fd, &buf) != 0) { |
| 392 | PyErr_SetString(PyExc_ValueError, "invalid fd"); |
| 393 | return NULL; |
| 394 | } |
| 395 | old_fd = wakeup_fd; |
| 396 | wakeup_fd = fd; |
| 397 | return PyLong_FromLong(old_fd); |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | PyDoc_STRVAR(set_wakeup_fd_doc, |
| 401 | "set_wakeup_fd(fd) -> fd\n\ |
| 402 | \n\ |
| 403 | Sets the fd to be written to (with '\\0') when a signal\n\ |
| 404 | comes in. A library can use this to wakeup select or poll.\n\ |
| 405 | The previous fd is returned.\n\ |
| 406 | \n\ |
| 407 | The fd must be non-blocking."); |
| 408 | |
| 409 | /* C API for the same, without all the error checking */ |
| 410 | int |
| 411 | PySignal_SetWakeupFd(int fd) |
| 412 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 413 | int old_fd = wakeup_fd; |
| 414 | if (fd < 0) |
| 415 | fd = -1; |
| 416 | wakeup_fd = fd; |
| 417 | return old_fd; |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 421 | #ifdef HAVE_SETITIMER |
| 422 | static PyObject * |
| 423 | signal_setitimer(PyObject *self, PyObject *args) |
| 424 | { |
| 425 | double first; |
| 426 | double interval = 0; |
| 427 | int which; |
| 428 | struct itimerval new, old; |
| 429 | |
| 430 | if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 431 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 432 | |
| 433 | timeval_from_double(first, &new.it_value); |
| 434 | timeval_from_double(interval, &new.it_interval); |
| 435 | /* Let OS check "which" value */ |
| 436 | if (setitimer(which, &new, &old) != 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 437 | PyErr_SetFromErrno(ItimerError); |
| 438 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | return itimer_retval(&old); |
| 442 | } |
| 443 | |
| 444 | PyDoc_STRVAR(setitimer_doc, |
| 445 | "setitimer(which, seconds[, interval])\n\ |
| 446 | \n\ |
| 447 | Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\ |
| 448 | or ITIMER_PROF) to fire after value seconds and after\n\ |
| 449 | that every interval seconds.\n\ |
| 450 | The itimer can be cleared by setting seconds to zero.\n\ |
| 451 | \n\ |
| 452 | Returns old values as a tuple: (delay, interval)."); |
| 453 | #endif |
| 454 | |
| 455 | |
| 456 | #ifdef HAVE_GETITIMER |
| 457 | static PyObject * |
| 458 | signal_getitimer(PyObject *self, PyObject *args) |
| 459 | { |
| 460 | int which; |
| 461 | struct itimerval old; |
| 462 | |
| 463 | if (!PyArg_ParseTuple(args, "i:getitimer", &which)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 464 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 465 | |
| 466 | if (getitimer(which, &old) != 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 467 | PyErr_SetFromErrno(ItimerError); |
| 468 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | return itimer_retval(&old); |
| 472 | } |
| 473 | |
| 474 | PyDoc_STRVAR(getitimer_doc, |
| 475 | "getitimer(which)\n\ |
| 476 | \n\ |
| 477 | Returns current value of given itimer."); |
| 478 | #endif |
| 479 | |
| 480 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 481 | /* List of functions defined in the module */ |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 482 | static PyMethodDef signal_methods[] = { |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 483 | #ifdef HAVE_ALARM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 484 | {"alarm", signal_alarm, METH_VARARGS, alarm_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 485 | #endif |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 486 | #ifdef HAVE_SETITIMER |
| 487 | {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc}, |
| 488 | #endif |
| 489 | #ifdef HAVE_GETITIMER |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 490 | {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc}, |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 491 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 492 | {"signal", signal_signal, METH_VARARGS, signal_doc}, |
| 493 | {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc}, |
| 494 | {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc}, |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 495 | #ifdef HAVE_SIGINTERRUPT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 496 | {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc}, |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 497 | #endif |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 498 | #ifdef HAVE_PAUSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 499 | {"pause", (PyCFunction)signal_pause, |
| 500 | METH_NOARGS,pause_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 501 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 502 | {"default_int_handler", signal_default_int_handler, |
| 503 | METH_VARARGS, default_int_handler_doc}, |
| 504 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 505 | }; |
| 506 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 507 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 508 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 509 | "This module provides mechanisms to use signal handlers in Python.\n\ |
| 510 | \n\ |
| 511 | Functions:\n\ |
| 512 | \n\ |
| 513 | alarm() -- cause SIGALRM after a specified time [Unix only]\n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 514 | setitimer() -- cause a signal (described below) after a specified\n\ |
| 515 | float time and the timer may restart then [Unix only]\n\ |
| 516 | getitimer() -- get current value of timer [Unix only]\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 517 | signal() -- set the action for a given signal\n\ |
| 518 | getsignal() -- get the signal action for a given signal\n\ |
| 519 | pause() -- wait until a signal arrives [Unix only]\n\ |
| 520 | default_int_handler() -- default SIGINT handler\n\ |
| 521 | \n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 522 | signal constants:\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 523 | SIG_DFL -- used to refer to the system default handler\n\ |
| 524 | SIG_IGN -- used to ignore the signal\n\ |
| 525 | NSIG -- number of defined signals\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 526 | SIGINT, SIGTERM, etc. -- signal numbers\n\ |
| 527 | \n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 528 | itimer constants:\n\ |
| 529 | ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\ |
| 530 | expiration\n\ |
| 531 | ITIMER_VIRTUAL -- decrements only when the process is executing,\n\ |
| 532 | and delivers SIGVTALRM upon expiration\n\ |
| 533 | ITIMER_PROF -- decrements both when the process is executing and\n\ |
| 534 | when the system is executing on behalf of the process.\n\ |
| 535 | Coupled with ITIMER_VIRTUAL, this timer is usually\n\ |
| 536 | used to profile the time spent by the application\n\ |
| 537 | in user and kernel space. SIGPROF is delivered upon\n\ |
| 538 | expiration.\n\ |
| 539 | \n\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 540 | *** IMPORTANT NOTICE ***\n\ |
| 541 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 542 | the first is the signal number, the second is the interrupted stack frame."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 543 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 544 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 545 | initsignal(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 546 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 547 | PyObject *m, *d, *x; |
| 548 | int i; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 549 | |
| 550 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 551 | main_thread = PyThread_get_thread_ident(); |
| 552 | main_pid = getpid(); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 553 | #endif |
| 554 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 555 | /* Create the module and add the functions */ |
| 556 | m = Py_InitModule3("signal", signal_methods, module_doc); |
| 557 | if (m == NULL) |
| 558 | return; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 560 | /* Add some symbolic constants to the module */ |
| 561 | d = PyModule_GetDict(m); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 563 | x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); |
| 564 | if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) |
| 565 | goto finally; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 567 | x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); |
| 568 | if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) |
| 569 | goto finally; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 571 | x = PyInt_FromLong((long)NSIG); |
| 572 | if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) |
| 573 | goto finally; |
| 574 | Py_DECREF(x); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 576 | x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); |
| 577 | if (!x) |
| 578 | goto finally; |
| 579 | Py_INCREF(IntHandler); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 581 | Handlers[0].tripped = 0; |
| 582 | for (i = 1; i < NSIG; i++) { |
| 583 | void (*t)(int); |
| 584 | t = PyOS_getsig(i); |
| 585 | Handlers[i].tripped = 0; |
| 586 | if (t == SIG_DFL) |
| 587 | Handlers[i].func = DefaultHandler; |
| 588 | else if (t == SIG_IGN) |
| 589 | Handlers[i].func = IgnoreHandler; |
| 590 | else |
| 591 | Handlers[i].func = Py_None; /* None of our business */ |
| 592 | Py_INCREF(Handlers[i].func); |
| 593 | } |
| 594 | if (Handlers[SIGINT].func == DefaultHandler) { |
| 595 | /* Install default int handler */ |
| 596 | Py_INCREF(IntHandler); |
| 597 | Py_DECREF(Handlers[SIGINT].func); |
| 598 | Handlers[SIGINT].func = IntHandler; |
| 599 | old_siginthandler = PyOS_setsig(SIGINT, signal_handler); |
| 600 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 601 | |
| 602 | #ifdef SIGHUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 603 | x = PyInt_FromLong(SIGHUP); |
| 604 | PyDict_SetItemString(d, "SIGHUP", x); |
| 605 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 606 | #endif |
| 607 | #ifdef SIGINT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 608 | x = PyInt_FromLong(SIGINT); |
| 609 | PyDict_SetItemString(d, "SIGINT", x); |
| 610 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 611 | #endif |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 612 | #ifdef SIGBREAK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 613 | x = PyInt_FromLong(SIGBREAK); |
| 614 | PyDict_SetItemString(d, "SIGBREAK", x); |
| 615 | Py_XDECREF(x); |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 616 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 617 | #ifdef SIGQUIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 618 | x = PyInt_FromLong(SIGQUIT); |
| 619 | PyDict_SetItemString(d, "SIGQUIT", x); |
| 620 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 621 | #endif |
| 622 | #ifdef SIGILL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 623 | x = PyInt_FromLong(SIGILL); |
| 624 | PyDict_SetItemString(d, "SIGILL", x); |
| 625 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 626 | #endif |
| 627 | #ifdef SIGTRAP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 628 | x = PyInt_FromLong(SIGTRAP); |
| 629 | PyDict_SetItemString(d, "SIGTRAP", x); |
| 630 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 631 | #endif |
| 632 | #ifdef SIGIOT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 633 | x = PyInt_FromLong(SIGIOT); |
| 634 | PyDict_SetItemString(d, "SIGIOT", x); |
| 635 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 636 | #endif |
| 637 | #ifdef SIGABRT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 638 | x = PyInt_FromLong(SIGABRT); |
| 639 | PyDict_SetItemString(d, "SIGABRT", x); |
| 640 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 641 | #endif |
| 642 | #ifdef SIGEMT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 643 | x = PyInt_FromLong(SIGEMT); |
| 644 | PyDict_SetItemString(d, "SIGEMT", x); |
| 645 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 646 | #endif |
| 647 | #ifdef SIGFPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 648 | x = PyInt_FromLong(SIGFPE); |
| 649 | PyDict_SetItemString(d, "SIGFPE", x); |
| 650 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 651 | #endif |
| 652 | #ifdef SIGKILL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 653 | x = PyInt_FromLong(SIGKILL); |
| 654 | PyDict_SetItemString(d, "SIGKILL", x); |
| 655 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 656 | #endif |
| 657 | #ifdef SIGBUS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 658 | x = PyInt_FromLong(SIGBUS); |
| 659 | PyDict_SetItemString(d, "SIGBUS", x); |
| 660 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 661 | #endif |
| 662 | #ifdef SIGSEGV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | x = PyInt_FromLong(SIGSEGV); |
| 664 | PyDict_SetItemString(d, "SIGSEGV", x); |
| 665 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 666 | #endif |
| 667 | #ifdef SIGSYS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 668 | x = PyInt_FromLong(SIGSYS); |
| 669 | PyDict_SetItemString(d, "SIGSYS", x); |
| 670 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 671 | #endif |
| 672 | #ifdef SIGPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 673 | x = PyInt_FromLong(SIGPIPE); |
| 674 | PyDict_SetItemString(d, "SIGPIPE", x); |
| 675 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 676 | #endif |
| 677 | #ifdef SIGALRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 678 | x = PyInt_FromLong(SIGALRM); |
| 679 | PyDict_SetItemString(d, "SIGALRM", x); |
| 680 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 681 | #endif |
| 682 | #ifdef SIGTERM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 683 | x = PyInt_FromLong(SIGTERM); |
| 684 | PyDict_SetItemString(d, "SIGTERM", x); |
| 685 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 686 | #endif |
| 687 | #ifdef SIGUSR1 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 688 | x = PyInt_FromLong(SIGUSR1); |
| 689 | PyDict_SetItemString(d, "SIGUSR1", x); |
| 690 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 691 | #endif |
| 692 | #ifdef SIGUSR2 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 693 | x = PyInt_FromLong(SIGUSR2); |
| 694 | PyDict_SetItemString(d, "SIGUSR2", x); |
| 695 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 696 | #endif |
| 697 | #ifdef SIGCLD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 698 | x = PyInt_FromLong(SIGCLD); |
| 699 | PyDict_SetItemString(d, "SIGCLD", x); |
| 700 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 701 | #endif |
| 702 | #ifdef SIGCHLD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 703 | x = PyInt_FromLong(SIGCHLD); |
| 704 | PyDict_SetItemString(d, "SIGCHLD", x); |
| 705 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 706 | #endif |
| 707 | #ifdef SIGPWR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 708 | x = PyInt_FromLong(SIGPWR); |
| 709 | PyDict_SetItemString(d, "SIGPWR", x); |
| 710 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 711 | #endif |
| 712 | #ifdef SIGIO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 713 | x = PyInt_FromLong(SIGIO); |
| 714 | PyDict_SetItemString(d, "SIGIO", x); |
| 715 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 716 | #endif |
| 717 | #ifdef SIGURG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 718 | x = PyInt_FromLong(SIGURG); |
| 719 | PyDict_SetItemString(d, "SIGURG", x); |
| 720 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 721 | #endif |
| 722 | #ifdef SIGWINCH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 723 | x = PyInt_FromLong(SIGWINCH); |
| 724 | PyDict_SetItemString(d, "SIGWINCH", x); |
| 725 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 726 | #endif |
| 727 | #ifdef SIGPOLL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 728 | x = PyInt_FromLong(SIGPOLL); |
| 729 | PyDict_SetItemString(d, "SIGPOLL", x); |
| 730 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 731 | #endif |
| 732 | #ifdef SIGSTOP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 733 | x = PyInt_FromLong(SIGSTOP); |
| 734 | PyDict_SetItemString(d, "SIGSTOP", x); |
| 735 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 736 | #endif |
| 737 | #ifdef SIGTSTP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 738 | x = PyInt_FromLong(SIGTSTP); |
| 739 | PyDict_SetItemString(d, "SIGTSTP", x); |
| 740 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 741 | #endif |
| 742 | #ifdef SIGCONT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 743 | x = PyInt_FromLong(SIGCONT); |
| 744 | PyDict_SetItemString(d, "SIGCONT", x); |
| 745 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 746 | #endif |
| 747 | #ifdef SIGTTIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 748 | x = PyInt_FromLong(SIGTTIN); |
| 749 | PyDict_SetItemString(d, "SIGTTIN", x); |
| 750 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 751 | #endif |
| 752 | #ifdef SIGTTOU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 753 | x = PyInt_FromLong(SIGTTOU); |
| 754 | PyDict_SetItemString(d, "SIGTTOU", x); |
| 755 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 756 | #endif |
| 757 | #ifdef SIGVTALRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 758 | x = PyInt_FromLong(SIGVTALRM); |
| 759 | PyDict_SetItemString(d, "SIGVTALRM", x); |
| 760 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 761 | #endif |
| 762 | #ifdef SIGPROF |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 763 | x = PyInt_FromLong(SIGPROF); |
| 764 | PyDict_SetItemString(d, "SIGPROF", x); |
| 765 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 766 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 767 | #ifdef SIGXCPU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 768 | x = PyInt_FromLong(SIGXCPU); |
| 769 | PyDict_SetItemString(d, "SIGXCPU", x); |
| 770 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 771 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 772 | #ifdef SIGXFSZ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 773 | x = PyInt_FromLong(SIGXFSZ); |
| 774 | PyDict_SetItemString(d, "SIGXFSZ", x); |
| 775 | Py_XDECREF(x); |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 776 | #endif |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 777 | #ifdef SIGRTMIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 778 | x = PyInt_FromLong(SIGRTMIN); |
| 779 | PyDict_SetItemString(d, "SIGRTMIN", x); |
| 780 | Py_XDECREF(x); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 781 | #endif |
| 782 | #ifdef SIGRTMAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 783 | x = PyInt_FromLong(SIGRTMAX); |
| 784 | PyDict_SetItemString(d, "SIGRTMAX", x); |
| 785 | Py_XDECREF(x); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 786 | #endif |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 787 | #ifdef SIGINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 788 | x = PyInt_FromLong(SIGINFO); |
| 789 | PyDict_SetItemString(d, "SIGINFO", x); |
| 790 | Py_XDECREF(x); |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 791 | #endif |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 792 | |
| 793 | #ifdef ITIMER_REAL |
| 794 | x = PyLong_FromLong(ITIMER_REAL); |
| 795 | PyDict_SetItemString(d, "ITIMER_REAL", x); |
| 796 | Py_DECREF(x); |
| 797 | #endif |
| 798 | #ifdef ITIMER_VIRTUAL |
| 799 | x = PyLong_FromLong(ITIMER_VIRTUAL); |
| 800 | PyDict_SetItemString(d, "ITIMER_VIRTUAL", x); |
| 801 | Py_DECREF(x); |
| 802 | #endif |
| 803 | #ifdef ITIMER_PROF |
| 804 | x = PyLong_FromLong(ITIMER_PROF); |
| 805 | PyDict_SetItemString(d, "ITIMER_PROF", x); |
| 806 | Py_DECREF(x); |
| 807 | #endif |
| 808 | |
| 809 | #if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 810 | ItimerError = PyErr_NewException("signal.ItimerError", |
| 811 | PyExc_IOError, NULL); |
Neal Norwitz | 18aa388 | 2008-08-24 05:04:52 +0000 | [diff] [blame] | 812 | if (ItimerError != NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 813 | PyDict_SetItemString(d, "ItimerError", ItimerError); |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 814 | #endif |
| 815 | |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 816 | #ifdef CTRL_C_EVENT |
| 817 | x = PyInt_FromLong(CTRL_C_EVENT); |
| 818 | PyDict_SetItemString(d, "CTRL_C_EVENT", x); |
| 819 | Py_DECREF(x); |
| 820 | #endif |
| 821 | |
| 822 | #ifdef CTRL_BREAK_EVENT |
| 823 | x = PyInt_FromLong(CTRL_BREAK_EVENT); |
| 824 | PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x); |
| 825 | Py_DECREF(x); |
| 826 | #endif |
| 827 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 828 | if (!PyErr_Occurred()) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 829 | return; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 830 | |
| 831 | /* Check for errors */ |
| 832 | finally: |
| 833 | return; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 837 | finisignal(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 838 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 839 | int i; |
| 840 | PyObject *func; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 842 | PyOS_setsig(SIGINT, old_siginthandler); |
| 843 | old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 845 | for (i = 1; i < NSIG; i++) { |
| 846 | func = Handlers[i].func; |
| 847 | Handlers[i].tripped = 0; |
| 848 | Handlers[i].func = NULL; |
| 849 | if (i != SIGINT && func != NULL && func != Py_None && |
| 850 | func != DefaultHandler && func != IgnoreHandler) |
| 851 | PyOS_setsig(i, SIG_DFL); |
| 852 | Py_XDECREF(func); |
| 853 | } |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 855 | Py_XDECREF(IntHandler); |
| 856 | IntHandler = NULL; |
| 857 | Py_XDECREF(DefaultHandler); |
| 858 | DefaultHandler = NULL; |
| 859 | Py_XDECREF(IgnoreHandler); |
| 860 | IgnoreHandler = NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 863 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 864 | /* Declared in pyerrors.h */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 865 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 866 | PyErr_CheckSignals(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 867 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 868 | int i; |
| 869 | PyObject *f; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 870 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 871 | if (!is_tripped) |
| 872 | return 0; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 873 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 874 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 875 | if (PyThread_get_thread_ident() != main_thread) |
| 876 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 877 | #endif |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 879 | /* |
| 880 | * The is_tripped variable is meant to speed up the calls to |
| 881 | * PyErr_CheckSignals (both directly or via pending calls) when no |
| 882 | * signal has arrived. This variable is set to 1 when a signal arrives |
| 883 | * and it is set to 0 here, when we know some signals arrived. This way |
| 884 | * we can run the registered handlers with no signals blocked. |
| 885 | * |
| 886 | * NOTE: with this approach we can have a situation where is_tripped is |
| 887 | * 1 but we have no more signals to handle (Handlers[i].tripped |
| 888 | * is 0 for every signal i). This won't do us any harm (except |
| 889 | * we're gonna spent some cycles for nothing). This happens when |
| 890 | * we receive a signal i after we zero is_tripped and before we |
| 891 | * check Handlers[i].tripped. |
| 892 | */ |
| 893 | is_tripped = 0; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 895 | if (!(f = (PyObject *)PyEval_GetFrame())) |
| 896 | f = Py_None; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 898 | for (i = 1; i < NSIG; i++) { |
| 899 | if (Handlers[i].tripped) { |
| 900 | PyObject *result = NULL; |
| 901 | PyObject *arglist = Py_BuildValue("(iO)", i, f); |
| 902 | Handlers[i].tripped = 0; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 904 | if (arglist) { |
| 905 | result = PyEval_CallObject(Handlers[i].func, |
| 906 | arglist); |
| 907 | Py_DECREF(arglist); |
| 908 | } |
| 909 | if (!result) |
| 910 | return -1; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 912 | Py_DECREF(result); |
| 913 | } |
| 914 | } |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 915 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 916 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 919 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 920 | /* Replacements for intrcheck.c functionality |
| 921 | * Declared in pyerrors.h |
| 922 | */ |
| 923 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 924 | PyErr_SetInterrupt(void) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 925 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 926 | is_tripped = 1; |
| 927 | Handlers[SIGINT].tripped = 1; |
| 928 | Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 929 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 930 | |
| 931 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 932 | PyOS_InitInterrupts(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 933 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 934 | initsignal(); |
| 935 | _PyImport_FixupExtension("signal", "signal"); |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 939 | PyOS_FiniInterrupts(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 940 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 941 | finisignal(); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 945 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 946 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 947 | if (Handlers[SIGINT].tripped) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 948 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 949 | if (PyThread_get_thread_ident() != main_thread) |
| 950 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 951 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 952 | Handlers[SIGINT].tripped = 0; |
| 953 | return 1; |
| 954 | } |
| 955 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 956 | } |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 957 | |
| 958 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 959 | PyOS_AfterFork(void) |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 960 | { |
| 961 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 962 | PyEval_ReInitThreads(); |
| 963 | main_thread = PyThread_get_thread_ident(); |
| 964 | main_pid = getpid(); |
| 965 | _PyImport_ReInitLock(); |
| 966 | PyThread_ReInitTLS(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 967 | #endif |
| 968 | } |