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