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