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 |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 10 | #include <process.h> |
| 11 | #endif |
| 12 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 13 | #include <signal.h> |
| 14 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 15 | #include <sys/stat.h> |
| 16 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 17 | #ifndef SIG_ERR |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 18 | #define SIG_ERR ((PyOS_sighandler_t)(-1)) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 19 | #endif |
| 20 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 21 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 22 | #define NSIG 12 |
| 23 | #include <process.h> |
| 24 | #endif |
| 25 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 26 | #ifndef NSIG |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 27 | # if defined(_NSIG) |
| 28 | # define NSIG _NSIG /* For BSD/SysV */ |
| 29 | # elif defined(_SIGMAX) |
| 30 | # define NSIG (_SIGMAX + 1) /* For QNX */ |
| 31 | # elif defined(SIGMAX) |
| 32 | # define NSIG (SIGMAX + 1) /* For djgpp */ |
| 33 | # else |
| 34 | # define NSIG 64 /* Use a reasonable default value */ |
| 35 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 39 | /* |
| 40 | NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS |
| 41 | |
| 42 | When threads are supported, we want the following semantics: |
| 43 | |
| 44 | - only the main thread can set a signal handler |
| 45 | - any thread can get a signal handler |
| 46 | - signals are only delivered to the main thread |
| 47 | |
| 48 | I.e. we don't support "synchronous signals" like SIGFPE (catching |
| 49 | this doesn't make much sense in Python anyway) nor do we support |
| 50 | signals as a means of inter-thread communication, since not all |
| 51 | thread implementations support that (at least our thread library |
| 52 | doesn't). |
| 53 | |
| 54 | We still have the problem that in some implementations signals |
| 55 | generated by the keyboard (e.g. SIGINT) are delivered to all |
| 56 | threads (e.g. SGI), while in others (e.g. Solaris) such signals are |
| 57 | delivered to one random thread (an intermediate possibility would |
Guido van Rossum | a3c04b0 | 1995-01-12 11:29:01 +0000 | [diff] [blame] | 58 | 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] | 59 | a working implementation that works in all three cases -- the |
| 60 | handler ignores signals if getpid() isn't the same as in the main |
| 61 | thread. XXX This is a hack. |
| 62 | |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 63 | GNU pth is a user-space threading library, and as such, all threads |
| 64 | run within the same process. In this case, if the currently running |
| 65 | 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] | 66 | */ |
| 67 | |
| 68 | #ifdef WITH_THREAD |
Guido van Rossum | 295b8e5 | 1997-06-06 21:16:41 +0000 | [diff] [blame] | 69 | #include <sys/types.h> /* For pid_t */ |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 70 | #include "pythread.h" |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 71 | static long main_thread; |
| 72 | static pid_t main_pid; |
| 73 | #endif |
| 74 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 75 | static struct { |
| 76 | int tripped; |
| 77 | PyObject *func; |
| 78 | } Handlers[NSIG]; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 79 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 80 | static sig_atomic_t wakeup_fd = -1; |
| 81 | |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 82 | /* Speed up sigcheck() when none tripped */ |
| 83 | static volatile sig_atomic_t is_tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 84 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 85 | static PyObject *DefaultHandler; |
| 86 | static PyObject *IgnoreHandler; |
| 87 | static PyObject *IntHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 88 | |
Martin v. Löwis | f58de1b | 2001-03-06 12:13:56 +0000 | [diff] [blame] | 89 | /* On Solaris 8, gcc will produce a warning that the function |
| 90 | declaration is not a prototype. This is caused by the definition of |
| 91 | SIG_DFL as (void (*)())0; the correct declaration would have been |
| 92 | (void (*)(int))0. */ |
| 93 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 94 | static PyOS_sighandler_t old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 97 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 98 | signal_default_int_handler(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 99 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 100 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 101 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 104 | PyDoc_STRVAR(default_int_handler_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 105 | "default_int_handler(...)\n\ |
| 106 | \n\ |
Michael W. Hudson | 24ec211 | 2004-06-17 15:55:53 +0000 | [diff] [blame] | 107 | The default handler for SIGINT installed by Python.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 108 | It raises KeyboardInterrupt."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 109 | |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 110 | |
| 111 | static int |
| 112 | checksignals_witharg(void * unused) |
| 113 | { |
| 114 | return PyErr_CheckSignals(); |
| 115 | } |
| 116 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 117 | static void |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 118 | signal_handler(int sig_num) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 120 | #ifdef WITH_THREAD |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 121 | #ifdef WITH_PTH |
| 122 | if (PyThread_get_thread_ident() != main_thread) { |
| 123 | pth_raise(*(pth_t *) main_thread, sig_num); |
| 124 | return; |
| 125 | } |
| 126 | #endif |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 127 | /* See NOTES section above */ |
| 128 | if (getpid() == main_pid) { |
| 129 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 130 | Handlers[sig_num].tripped = 1; |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 131 | /* Set is_tripped after setting .tripped, as it gets |
| 132 | cleared in PyErr_CheckSignals() before .tripped. */ |
| 133 | is_tripped = 1; |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 134 | Py_AddPendingCall(checksignals_witharg, NULL); |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 135 | if (wakeup_fd != -1) |
| 136 | write(wakeup_fd, "\0", 1); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 137 | #ifdef WITH_THREAD |
| 138 | } |
| 139 | #endif |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 140 | #ifdef SIGCHLD |
| 141 | if (sig_num == SIGCHLD) { |
| 142 | /* To avoid infinite recursion, this signal remains |
| 143 | reset until explicit re-instated. |
| 144 | Don't clear the 'func' field as it is our pointer |
| 145 | to the Python handler... */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 146 | return; |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 147 | } |
| 148 | #endif |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 149 | PyOS_setsig(sig_num, signal_handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 150 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 153 | #ifdef HAVE_ALARM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 154 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 155 | signal_alarm(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 156 | { |
| 157 | int t; |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 158 | if (!PyArg_ParseTuple(args, "i:alarm", &t)) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 159 | return NULL; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 160 | /* alarm() returns the number of seconds remaining */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 161 | return PyLong_FromLong((long)alarm(t)); |
Guido van Rossum | aa0f4c7 | 1994-08-23 13:49:37 +0000 | [diff] [blame] | 162 | } |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 163 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 164 | PyDoc_STRVAR(alarm_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 165 | "alarm(seconds)\n\ |
| 166 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 167 | Arrange for SIGALRM to arrive after the given number of seconds."); |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 168 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 170 | #ifdef HAVE_PAUSE |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 171 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 172 | signal_pause(PyObject *self) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 173 | { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 174 | Py_BEGIN_ALLOW_THREADS |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 175 | (void)pause(); |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 176 | Py_END_ALLOW_THREADS |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 177 | /* make sure that any exceptions that got raised are propagated |
| 178 | * back into Python |
| 179 | */ |
| 180 | if (PyErr_CheckSignals()) |
| 181 | return NULL; |
| 182 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 183 | Py_INCREF(Py_None); |
| 184 | return Py_None; |
| 185 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 186 | PyDoc_STRVAR(pause_doc, |
Barry Warsaw | 1ee36ff | 1998-07-21 22:41:18 +0000 | [diff] [blame] | 187 | "pause()\n\ |
| 188 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 189 | Wait until a signal arrives."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 191 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 194 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 195 | signal_signal(PyObject *self, PyObject *args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 196 | { |
| 197 | PyObject *obj; |
| 198 | int sig_num; |
| 199 | PyObject *old_handler; |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 200 | void (*func)(int); |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 201 | if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj)) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 202 | return NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 203 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 204 | if (PyThread_get_thread_ident() != main_thread) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 205 | PyErr_SetString(PyExc_ValueError, |
| 206 | "signal only works in main thread"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 207 | return NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 208 | } |
| 209 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 210 | if (sig_num < 1 || sig_num >= NSIG) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 211 | PyErr_SetString(PyExc_ValueError, |
| 212 | "signal number out of range"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 213 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 214 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 215 | if (obj == IgnoreHandler) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 216 | func = SIG_IGN; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 217 | else if (obj == DefaultHandler) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 218 | func = SIG_DFL; |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 219 | else if (!PyCallable_Check(obj)) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 220 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 221 | "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 222 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 223 | } |
| 224 | else |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 225 | func = signal_handler; |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 226 | if (PyOS_setsig(sig_num, func) == SIG_ERR) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 227 | PyErr_SetFromErrno(PyExc_RuntimeError); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 228 | return NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 229 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 230 | old_handler = Handlers[sig_num].func; |
| 231 | Handlers[sig_num].tripped = 0; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 232 | Py_INCREF(obj); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 233 | Handlers[sig_num].func = obj; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 234 | return old_handler; |
| 235 | } |
| 236 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 237 | PyDoc_STRVAR(signal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 238 | "signal(sig, action) -> action\n\ |
| 239 | \n\ |
| 240 | Set the action for the given signal. The action can be SIG_DFL,\n\ |
| 241 | SIG_IGN, or a callable Python object. The previous action is\n\ |
| 242 | returned. See getsignal() for possible return values.\n\ |
| 243 | \n\ |
| 244 | *** IMPORTANT NOTICE ***\n\ |
| 245 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 246 | 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] | 247 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 249 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 250 | signal_getsignal(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 251 | { |
| 252 | int sig_num; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 253 | PyObject *old_handler; |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 254 | if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num)) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 255 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 256 | if (sig_num < 1 || sig_num >= NSIG) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 257 | PyErr_SetString(PyExc_ValueError, |
| 258 | "signal number out of range"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 259 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 260 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 261 | old_handler = Handlers[sig_num].func; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 262 | Py_INCREF(old_handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 263 | return old_handler; |
| 264 | } |
| 265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 266 | PyDoc_STRVAR(getsignal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 267 | "getsignal(sig) -> action\n\ |
| 268 | \n\ |
| 269 | Return the current action for the given signal. The return value can be:\n\ |
| 270 | SIG_IGN -- if the signal is being ignored\n\ |
| 271 | SIG_DFL -- if the default action for the signal is in effect\n\ |
| 272 | None -- if an unknown handler is in effect\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 273 | anything else -- the callable Python object used as a handler"); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 275 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 276 | static PyObject * |
| 277 | signal_set_wakeup_fd(PyObject *self, PyObject *args) |
| 278 | { |
| 279 | struct stat buf; |
| 280 | int fd, old_fd; |
| 281 | if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd)) |
| 282 | return NULL; |
| 283 | #ifdef WITH_THREAD |
| 284 | if (PyThread_get_thread_ident() != main_thread) { |
| 285 | PyErr_SetString(PyExc_ValueError, |
| 286 | "set_wakeup_fd only works in main thread"); |
| 287 | return NULL; |
| 288 | } |
| 289 | #endif |
| 290 | if (fd != -1 && fstat(fd, &buf) != 0) { |
| 291 | PyErr_SetString(PyExc_ValueError, "invalid fd"); |
| 292 | return NULL; |
| 293 | } |
| 294 | old_fd = wakeup_fd; |
| 295 | wakeup_fd = fd; |
| 296 | return PyLong_FromLong(old_fd); |
| 297 | } |
| 298 | |
| 299 | PyDoc_STRVAR(set_wakeup_fd_doc, |
| 300 | "set_wakeup_fd(fd) -> fd\n\ |
| 301 | \n\ |
| 302 | Sets the fd to be written to (with '\\0') when a signal\n\ |
| 303 | comes in. A library can use this to wakeup select or poll.\n\ |
| 304 | The previous fd is returned.\n\ |
| 305 | \n\ |
| 306 | The fd must be non-blocking."); |
| 307 | |
| 308 | /* C API for the same, without all the error checking */ |
| 309 | int |
| 310 | PySignal_SetWakeupFd(int fd) |
| 311 | { |
| 312 | int old_fd = wakeup_fd; |
| 313 | if (fd < 0) |
| 314 | fd = -1; |
| 315 | wakeup_fd = fd; |
| 316 | return old_fd; |
| 317 | } |
| 318 | |
| 319 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 320 | /* List of functions defined in the module */ |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 321 | static PyMethodDef signal_methods[] = { |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 322 | #ifdef HAVE_ALARM |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 323 | {"alarm", signal_alarm, METH_VARARGS, alarm_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 324 | #endif |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 325 | {"signal", signal_signal, METH_VARARGS, signal_doc}, |
| 326 | {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc}, |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 327 | {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 328 | #ifdef HAVE_PAUSE |
Neil Schemenauer | 78662cf | 2002-03-28 21:04:14 +0000 | [diff] [blame] | 329 | {"pause", (PyCFunction)signal_pause, |
| 330 | METH_NOARGS,pause_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 331 | #endif |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 332 | {"default_int_handler", signal_default_int_handler, |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 333 | METH_VARARGS, default_int_handler_doc}, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 334 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 337 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 338 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 339 | "This module provides mechanisms to use signal handlers in Python.\n\ |
| 340 | \n\ |
| 341 | Functions:\n\ |
| 342 | \n\ |
| 343 | alarm() -- cause SIGALRM after a specified time [Unix only]\n\ |
| 344 | signal() -- set the action for a given signal\n\ |
| 345 | getsignal() -- get the signal action for a given signal\n\ |
| 346 | pause() -- wait until a signal arrives [Unix only]\n\ |
| 347 | default_int_handler() -- default SIGINT handler\n\ |
| 348 | \n\ |
| 349 | Constants:\n\ |
| 350 | \n\ |
| 351 | SIG_DFL -- used to refer to the system default handler\n\ |
| 352 | SIG_IGN -- used to ignore the signal\n\ |
| 353 | NSIG -- number of defined signals\n\ |
| 354 | \n\ |
| 355 | SIGINT, SIGTERM, etc. -- signal numbers\n\ |
| 356 | \n\ |
| 357 | *** IMPORTANT NOTICE ***\n\ |
| 358 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 359 | 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] | 360 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 361 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 362 | initsignal(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 363 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 364 | PyObject *m, *d, *x; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 365 | int i; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 366 | |
| 367 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 368 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 369 | main_pid = getpid(); |
| 370 | #endif |
| 371 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 372 | /* Create the module and add the functions */ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 373 | m = Py_InitModule3("signal", signal_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 374 | if (m == NULL) |
| 375 | return; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 376 | |
| 377 | /* Add some symbolic constants to the module */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 378 | d = PyModule_GetDict(m); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | 276fa43 | 2000-06-30 23:04:18 +0000 | [diff] [blame] | 380 | x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 381 | if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) |
| 382 | goto finally; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | 276fa43 | 2000-06-30 23:04:18 +0000 | [diff] [blame] | 384 | x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 385 | if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) |
| 386 | goto finally; |
| 387 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 388 | x = PyLong_FromLong((long)NSIG); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 389 | if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) |
| 390 | goto finally; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 391 | Py_DECREF(x); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 392 | |
| 393 | x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); |
| 394 | if (!x) |
| 395 | goto finally; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 396 | Py_INCREF(IntHandler); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 397 | |
| 398 | Handlers[0].tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 399 | for (i = 1; i < NSIG; i++) { |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 400 | void (*t)(int); |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 401 | t = PyOS_getsig(i); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 402 | Handlers[i].tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 403 | if (t == SIG_DFL) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 404 | Handlers[i].func = DefaultHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 405 | else if (t == SIG_IGN) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 406 | Handlers[i].func = IgnoreHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 407 | else |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 408 | Handlers[i].func = Py_None; /* None of our business */ |
| 409 | Py_INCREF(Handlers[i].func); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 410 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 411 | if (Handlers[SIGINT].func == DefaultHandler) { |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 412 | /* Install default int handler */ |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 413 | Py_INCREF(IntHandler); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 414 | Py_DECREF(Handlers[SIGINT].func); |
| 415 | Handlers[SIGINT].func = IntHandler; |
Neal Norwitz | 4178515 | 2002-06-13 21:42:51 +0000 | [diff] [blame] | 416 | old_siginthandler = PyOS_setsig(SIGINT, signal_handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | #ifdef SIGHUP |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 420 | x = PyLong_FromLong(SIGHUP); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 421 | PyDict_SetItemString(d, "SIGHUP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 422 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 423 | #endif |
| 424 | #ifdef SIGINT |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 425 | x = PyLong_FromLong(SIGINT); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 426 | PyDict_SetItemString(d, "SIGINT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 427 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 428 | #endif |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 429 | #ifdef SIGBREAK |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 430 | x = PyLong_FromLong(SIGBREAK); |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 431 | PyDict_SetItemString(d, "SIGBREAK", x); |
| 432 | Py_XDECREF(x); |
| 433 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 434 | #ifdef SIGQUIT |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 435 | x = PyLong_FromLong(SIGQUIT); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 436 | PyDict_SetItemString(d, "SIGQUIT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 437 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 438 | #endif |
| 439 | #ifdef SIGILL |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 440 | x = PyLong_FromLong(SIGILL); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 441 | PyDict_SetItemString(d, "SIGILL", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 442 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 443 | #endif |
| 444 | #ifdef SIGTRAP |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 445 | x = PyLong_FromLong(SIGTRAP); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 446 | PyDict_SetItemString(d, "SIGTRAP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 447 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 448 | #endif |
| 449 | #ifdef SIGIOT |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 450 | x = PyLong_FromLong(SIGIOT); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 451 | PyDict_SetItemString(d, "SIGIOT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 452 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 453 | #endif |
| 454 | #ifdef SIGABRT |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 455 | x = PyLong_FromLong(SIGABRT); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 456 | PyDict_SetItemString(d, "SIGABRT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 457 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 458 | #endif |
| 459 | #ifdef SIGEMT |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 460 | x = PyLong_FromLong(SIGEMT); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 461 | PyDict_SetItemString(d, "SIGEMT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 462 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 463 | #endif |
| 464 | #ifdef SIGFPE |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 465 | x = PyLong_FromLong(SIGFPE); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 466 | PyDict_SetItemString(d, "SIGFPE", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 467 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 468 | #endif |
| 469 | #ifdef SIGKILL |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 470 | x = PyLong_FromLong(SIGKILL); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 471 | PyDict_SetItemString(d, "SIGKILL", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 472 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 473 | #endif |
| 474 | #ifdef SIGBUS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 475 | x = PyLong_FromLong(SIGBUS); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 476 | PyDict_SetItemString(d, "SIGBUS", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 477 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 478 | #endif |
| 479 | #ifdef SIGSEGV |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 480 | x = PyLong_FromLong(SIGSEGV); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 481 | PyDict_SetItemString(d, "SIGSEGV", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 482 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 483 | #endif |
| 484 | #ifdef SIGSYS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 485 | x = PyLong_FromLong(SIGSYS); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 486 | PyDict_SetItemString(d, "SIGSYS", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 487 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 488 | #endif |
| 489 | #ifdef SIGPIPE |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 490 | x = PyLong_FromLong(SIGPIPE); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 491 | PyDict_SetItemString(d, "SIGPIPE", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 492 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 493 | #endif |
| 494 | #ifdef SIGALRM |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 495 | x = PyLong_FromLong(SIGALRM); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 496 | PyDict_SetItemString(d, "SIGALRM", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 497 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 498 | #endif |
| 499 | #ifdef SIGTERM |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 500 | x = PyLong_FromLong(SIGTERM); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 501 | PyDict_SetItemString(d, "SIGTERM", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 502 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 503 | #endif |
| 504 | #ifdef SIGUSR1 |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 505 | x = PyLong_FromLong(SIGUSR1); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 506 | PyDict_SetItemString(d, "SIGUSR1", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 507 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 508 | #endif |
| 509 | #ifdef SIGUSR2 |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 510 | x = PyLong_FromLong(SIGUSR2); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 511 | PyDict_SetItemString(d, "SIGUSR2", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 512 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 513 | #endif |
| 514 | #ifdef SIGCLD |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 515 | x = PyLong_FromLong(SIGCLD); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 516 | PyDict_SetItemString(d, "SIGCLD", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 517 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 518 | #endif |
| 519 | #ifdef SIGCHLD |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 520 | x = PyLong_FromLong(SIGCHLD); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 521 | PyDict_SetItemString(d, "SIGCHLD", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 522 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 523 | #endif |
| 524 | #ifdef SIGPWR |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 525 | x = PyLong_FromLong(SIGPWR); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 526 | PyDict_SetItemString(d, "SIGPWR", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 527 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 528 | #endif |
| 529 | #ifdef SIGIO |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 530 | x = PyLong_FromLong(SIGIO); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 531 | PyDict_SetItemString(d, "SIGIO", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 532 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 533 | #endif |
| 534 | #ifdef SIGURG |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 535 | x = PyLong_FromLong(SIGURG); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 536 | PyDict_SetItemString(d, "SIGURG", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 537 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 538 | #endif |
| 539 | #ifdef SIGWINCH |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 540 | x = PyLong_FromLong(SIGWINCH); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 541 | PyDict_SetItemString(d, "SIGWINCH", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 542 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 543 | #endif |
| 544 | #ifdef SIGPOLL |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 545 | x = PyLong_FromLong(SIGPOLL); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 546 | PyDict_SetItemString(d, "SIGPOLL", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 547 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 548 | #endif |
| 549 | #ifdef SIGSTOP |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 550 | x = PyLong_FromLong(SIGSTOP); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 551 | PyDict_SetItemString(d, "SIGSTOP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 552 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 553 | #endif |
| 554 | #ifdef SIGTSTP |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 555 | x = PyLong_FromLong(SIGTSTP); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 556 | PyDict_SetItemString(d, "SIGTSTP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 557 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 558 | #endif |
| 559 | #ifdef SIGCONT |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 560 | x = PyLong_FromLong(SIGCONT); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 561 | PyDict_SetItemString(d, "SIGCONT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 562 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 563 | #endif |
| 564 | #ifdef SIGTTIN |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 565 | x = PyLong_FromLong(SIGTTIN); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 566 | PyDict_SetItemString(d, "SIGTTIN", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 567 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 568 | #endif |
| 569 | #ifdef SIGTTOU |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 570 | x = PyLong_FromLong(SIGTTOU); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 571 | PyDict_SetItemString(d, "SIGTTOU", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 572 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 573 | #endif |
| 574 | #ifdef SIGVTALRM |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 575 | x = PyLong_FromLong(SIGVTALRM); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 576 | PyDict_SetItemString(d, "SIGVTALRM", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 577 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 578 | #endif |
| 579 | #ifdef SIGPROF |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 580 | x = PyLong_FromLong(SIGPROF); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 581 | PyDict_SetItemString(d, "SIGPROF", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 582 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 583 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 584 | #ifdef SIGXCPU |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 585 | x = PyLong_FromLong(SIGXCPU); |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 586 | PyDict_SetItemString(d, "SIGXCPU", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 587 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 588 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 589 | #ifdef SIGXFSZ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 590 | x = PyLong_FromLong(SIGXFSZ); |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 591 | PyDict_SetItemString(d, "SIGXFSZ", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 592 | Py_XDECREF(x); |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 593 | #endif |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 594 | #ifdef SIGRTMIN |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 595 | x = PyLong_FromLong(SIGRTMIN); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 596 | PyDict_SetItemString(d, "SIGRTMIN", x); |
| 597 | Py_XDECREF(x); |
| 598 | #endif |
| 599 | #ifdef SIGRTMAX |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 600 | x = PyLong_FromLong(SIGRTMAX); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 601 | PyDict_SetItemString(d, "SIGRTMAX", x); |
| 602 | Py_XDECREF(x); |
| 603 | #endif |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 604 | #ifdef SIGINFO |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 605 | x = PyLong_FromLong(SIGINFO); |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 606 | PyDict_SetItemString(d, "SIGINFO", x); |
| 607 | Py_XDECREF(x); |
| 608 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 609 | if (!PyErr_Occurred()) |
| 610 | return; |
| 611 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 612 | /* Check for errors */ |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 613 | finally: |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
| 617 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 618 | finisignal(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 619 | { |
| 620 | int i; |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 621 | PyObject *func; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 622 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 623 | PyOS_setsig(SIGINT, old_siginthandler); |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 624 | old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 625 | |
| 626 | for (i = 1; i < NSIG; i++) { |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 627 | func = Handlers[i].func; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 628 | Handlers[i].tripped = 0; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 629 | Handlers[i].func = NULL; |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 630 | if (i != SIGINT && func != NULL && func != Py_None && |
| 631 | func != DefaultHandler && func != IgnoreHandler) |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 632 | PyOS_setsig(i, SIG_DFL); |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 633 | Py_XDECREF(func); |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | Py_XDECREF(IntHandler); |
| 637 | IntHandler = NULL; |
| 638 | Py_XDECREF(DefaultHandler); |
| 639 | DefaultHandler = NULL; |
| 640 | Py_XDECREF(IgnoreHandler); |
| 641 | IgnoreHandler = NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 644 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 645 | /* Declared in pyerrors.h */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 646 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 647 | PyErr_CheckSignals(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 648 | { |
| 649 | int i; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 650 | PyObject *f; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 651 | |
| 652 | if (!is_tripped) |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 653 | return 0; |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 654 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 655 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 656 | if (PyThread_get_thread_ident() != main_thread) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 657 | return 0; |
| 658 | #endif |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * The is_stripped variable is meant to speed up the calls to |
| 662 | * PyErr_CheckSignals (both directly or via pending calls) when no |
| 663 | * signal has arrived. This variable is set to 1 when a signal arrives |
| 664 | * and it is set to 0 here, when we know some signals arrived. This way |
| 665 | * we can run the registered handlers with no signals blocked. |
| 666 | * |
| 667 | * NOTE: with this approach we can have a situation where is_tripped is |
| 668 | * 1 but we have no more signals to handle (Handlers[i].tripped |
| 669 | * is 0 for every signal i). This won't do us any harm (except |
| 670 | * we're gonna spent some cycles for nothing). This happens when |
| 671 | * we receive a signal i after we zero is_tripped and before we |
| 672 | * check Handlers[i].tripped. |
| 673 | */ |
| 674 | is_tripped = 0; |
| 675 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 676 | if (!(f = (PyObject *)PyEval_GetFrame())) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 677 | f = Py_None; |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 678 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 679 | for (i = 1; i < NSIG; i++) { |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 680 | if (Handlers[i].tripped) { |
| 681 | PyObject *result = NULL; |
| 682 | PyObject *arglist = Py_BuildValue("(iO)", i, f); |
| 683 | Handlers[i].tripped = 0; |
| 684 | |
| 685 | if (arglist) { |
| 686 | result = PyEval_CallObject(Handlers[i].func, |
| 687 | arglist); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 688 | Py_DECREF(arglist); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 689 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 690 | if (!result) |
Guido van Rossum | fcdd0e4 | 1997-01-21 06:13:09 +0000 | [diff] [blame] | 691 | return -1; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 692 | |
| 693 | Py_DECREF(result); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 696 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 700 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 701 | /* Replacements for intrcheck.c functionality |
| 702 | * Declared in pyerrors.h |
| 703 | */ |
| 704 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 705 | PyErr_SetInterrupt(void) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 706 | { |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 707 | is_tripped = 1; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 708 | Handlers[SIGINT].tripped = 1; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 709 | Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 710 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 711 | |
| 712 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 713 | PyOS_InitInterrupts(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 714 | { |
| 715 | initsignal(); |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 716 | _PyImport_FixupExtension("signal", "signal"); |
| 717 | } |
| 718 | |
| 719 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 720 | PyOS_FiniInterrupts(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 721 | { |
| 722 | finisignal(); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 726 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 727 | { |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 728 | if (Handlers[SIGINT].tripped) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 729 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 730 | if (PyThread_get_thread_ident() != main_thread) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 731 | return 0; |
| 732 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 733 | Handlers[SIGINT].tripped = 0; |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 734 | return 1; |
| 735 | } |
| 736 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 737 | } |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 738 | |
| 739 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 740 | PyOS_AfterFork(void) |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 741 | { |
| 742 | #ifdef WITH_THREAD |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 743 | PyEval_ReInitThreads(); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 744 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 745 | main_pid = getpid(); |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 746 | _PyImport_ReInitLock(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 747 | #endif |
| 748 | } |