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