blob: 8acec219eab30e1042d53ad15928109e6077a5f3 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +00007#include "intrcheck.h"
8
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009#ifdef MS_WINDOWS
Guido van Rossum644a12b1997-04-09 19:24:53 +000010#include <process.h>
11#endif
12
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013#include <signal.h>
14
Guido van Rossum02de8972007-12-19 19:41:06 +000015#include <sys/stat.h>
16
Guido van Rossumbb4ba121994-06-23 11:25:45 +000017#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000018#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000019#endif
20
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000021#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000022#define NSIG 12
23#include <process.h>
24#endif
25
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000026#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000027# if defined(_NSIG)
28# define NSIG _NSIG /* For BSD/SysV */
29# elif defined(_SIGMAX)
30# define NSIG (_SIGMAX + 1) /* For QNX */
31# elif defined(SIGMAX)
32# define NSIG (SIGMAX + 1) /* For djgpp */
33# else
34# define NSIG 64 /* Use a reasonable default value */
35# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000036#endif
37
38
Guido van Rossumbb4ba121994-06-23 11:25:45 +000039/*
40 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
41
42 When threads are supported, we want the following semantics:
43
44 - only the main thread can set a signal handler
45 - any thread can get a signal handler
46 - signals are only delivered to the main thread
47
48 I.e. we don't support "synchronous signals" like SIGFPE (catching
49 this doesn't make much sense in Python anyway) nor do we support
50 signals as a means of inter-thread communication, since not all
51 thread implementations support that (at least our thread library
52 doesn't).
53
54 We still have the problem that in some implementations signals
55 generated by the keyboard (e.g. SIGINT) are delivered to all
56 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
57 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000058 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000059 a working implementation that works in all three cases -- the
60 handler ignores signals if getpid() isn't the same as in the main
61 thread. XXX This is a hack.
62
Guido van Rossum9e8181b2000-09-19 00:46:46 +000063 GNU pth is a user-space threading library, and as such, all threads
64 run within the same process. In this case, if the currently running
65 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000066*/
67
68#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000069#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000070#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071static long main_thread;
72static pid_t main_pid;
73#endif
74
Barry Warsaw92971171997-01-03 00:14:25 +000075static struct {
76 int tripped;
77 PyObject *func;
78} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000079
Guido van Rossum02de8972007-12-19 19:41:06 +000080static sig_atomic_t wakeup_fd = -1;
81
Guido van Rossum137c49c2007-12-10 23:00:12 +000082/* Speed up sigcheck() when none tripped */
83static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000084
Barry Warsaw92971171997-01-03 00:14:25 +000085static PyObject *DefaultHandler;
86static PyObject *IgnoreHandler;
87static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000088
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000089/* On Solaris 8, gcc will produce a warning that the function
90 declaration is not a prototype. This is caused by the definition of
91 SIG_DFL as (void (*)())0; the correct declaration would have been
92 (void (*)(int))0. */
93
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000094static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000095
Barry Warsaw92971171997-01-03 00:14:25 +000096
Guido van Rossume4485b01994-09-07 14:32:49 +000097static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000098signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099{
Guido van Rossume4485b01994-09-07 14:32:49 +0000100 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000101 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000102}
103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000105"default_int_handler(...)\n\
106\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000107The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000108It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000109
Thomas Wouters0796b002000-07-22 23:49:30 +0000110
111static int
112checksignals_witharg(void * unused)
113{
114 return PyErr_CheckSignals();
115}
116
Tim Peters4f1b2082000-07-23 21:18:09 +0000117static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000118signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000119{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000120#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000121#ifdef WITH_PTH
122 if (PyThread_get_thread_ident() != main_thread) {
123 pth_raise(*(pth_t *) main_thread, sig_num);
124 return;
125 }
126#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000127 /* See NOTES section above */
128 if (getpid() == main_pid) {
129#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000130 Handlers[sig_num].tripped = 1;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000131 /* Set is_tripped after setting .tripped, as it gets
132 cleared in PyErr_CheckSignals() before .tripped. */
133 is_tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000134 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum02de8972007-12-19 19:41:06 +0000135 if (wakeup_fd != -1)
136 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000137#ifdef WITH_THREAD
138 }
139#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000140#ifdef SIGCHLD
141 if (sig_num == SIGCHLD) {
142 /* To avoid infinite recursion, this signal remains
143 reset until explicit re-instated.
144 Don't clear the 'func' field as it is our pointer
145 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000146 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000147 }
148#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000149 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000150}
Guido van Rossume4485b01994-09-07 14:32:49 +0000151
Guido van Rossum06d511d1995-03-10 15:13:48 +0000152
Guido van Rossum1171ee61997-08-22 20:42:00 +0000153#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000154static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000155signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156{
157 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000158 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000159 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000160 /* alarm() returns the number of seconds remaining */
Fred Drakedff3a372001-07-19 21:29:49 +0000161 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000162}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000165"alarm(seconds)\n\
166\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000168#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169
Guido van Rossum1171ee61997-08-22 20:42:00 +0000170#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000171static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000172signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000173{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000174 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000175 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000176 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000177 /* make sure that any exceptions that got raised are propagated
178 * back into Python
179 */
180 if (PyErr_CheckSignals())
181 return NULL;
182
Guido van Rossume4485b01994-09-07 14:32:49 +0000183 Py_INCREF(Py_None);
184 return Py_None;
185}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000186PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000187"pause()\n\
188\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000189Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000190
Guido van Rossum06d511d1995-03-10 15:13:48 +0000191#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000192
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000193
Guido van Rossume4485b01994-09-07 14:32:49 +0000194static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000195signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000196{
197 PyObject *obj;
198 int sig_num;
199 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000200 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000201 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000202 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000203#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000204 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000205 PyErr_SetString(PyExc_ValueError,
206 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000207 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000208 }
209#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000211 PyErr_SetString(PyExc_ValueError,
212 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000213 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000214 }
Barry Warsaw92971171997-01-03 00:14:25 +0000215 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000216 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000217 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000218 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000219 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000220 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000221"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000222 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000223 }
224 else
Barry Warsaw92971171997-01-03 00:14:25 +0000225 func = signal_handler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000226 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000227 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000228 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000229 }
Barry Warsaw92971171997-01-03 00:14:25 +0000230 old_handler = Handlers[sig_num].func;
231 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000232 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000233 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000234 return old_handler;
235}
236
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000238"signal(sig, action) -> action\n\
239\n\
240Set the action for the given signal. The action can be SIG_DFL,\n\
241SIG_IGN, or a callable Python object. The previous action is\n\
242returned. See getsignal() for possible return values.\n\
243\n\
244*** IMPORTANT NOTICE ***\n\
245A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000246the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000247
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000248
Guido van Rossume4485b01994-09-07 14:32:49 +0000249static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000250signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000251{
252 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000253 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000254 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000255 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000256 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000257 PyErr_SetString(PyExc_ValueError,
258 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000259 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260 }
Barry Warsaw92971171997-01-03 00:14:25 +0000261 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000262 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000263 return old_handler;
264}
265
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000266PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000267"getsignal(sig) -> action\n\
268\n\
269Return the current action for the given signal. The return value can be:\n\
270SIG_IGN -- if the signal is being ignored\n\
271SIG_DFL -- if the default action for the signal is in effect\n\
272None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000273anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000274
Facundo Batista7e251e82008-02-23 15:07:35 +0000275#ifdef HAVE_SIGINTERRUPT
276PyDoc_STRVAR(siginterrupt_doc,
277"siginterrupt(sig, flag) -> None\n\
278change system call restart behaviour: if flag is False, system calls\n\
279will be restarted when interrupted by signal sig, else system calls\n\
280will be interrupted.");
281
282static PyObject *
283signal_siginterrupt(PyObject *self, PyObject *args)
284{
285 int sig_num;
286 int flag;
287
288 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
289 return NULL;
290 if (sig_num < 1 || sig_num >= NSIG) {
291 PyErr_SetString(PyExc_ValueError,
292 "signal number out of range");
293 return NULL;
294 }
295 if (siginterrupt(sig_num, flag)<0) {
296 PyErr_SetFromErrno(PyExc_RuntimeError);
297 return NULL;
298 }
299
300 Py_INCREF(Py_None);
301 return Py_None;
302}
303
304#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000305
Guido van Rossum02de8972007-12-19 19:41:06 +0000306static PyObject *
307signal_set_wakeup_fd(PyObject *self, PyObject *args)
308{
309 struct stat buf;
310 int fd, old_fd;
311 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
312 return NULL;
313#ifdef WITH_THREAD
314 if (PyThread_get_thread_ident() != main_thread) {
315 PyErr_SetString(PyExc_ValueError,
316 "set_wakeup_fd only works in main thread");
317 return NULL;
318 }
319#endif
320 if (fd != -1 && fstat(fd, &buf) != 0) {
321 PyErr_SetString(PyExc_ValueError, "invalid fd");
322 return NULL;
323 }
324 old_fd = wakeup_fd;
325 wakeup_fd = fd;
326 return PyLong_FromLong(old_fd);
327}
328
329PyDoc_STRVAR(set_wakeup_fd_doc,
330"set_wakeup_fd(fd) -> fd\n\
331\n\
332Sets the fd to be written to (with '\\0') when a signal\n\
333comes in. A library can use this to wakeup select or poll.\n\
334The previous fd is returned.\n\
335\n\
336The fd must be non-blocking.");
337
338/* C API for the same, without all the error checking */
339int
340PySignal_SetWakeupFd(int fd)
341{
342 int old_fd = wakeup_fd;
343 if (fd < 0)
344 fd = -1;
345 wakeup_fd = fd;
346 return old_fd;
347}
348
349
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000350/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000351static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000352#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000353 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000354#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000355 {"signal", signal_signal, METH_VARARGS, signal_doc},
356 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Guido van Rossum02de8972007-12-19 19:41:06 +0000357 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000358#ifdef HAVE_SIGINTERRUPT
359 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
360#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000361#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000362 {"pause", (PyCFunction)signal_pause,
363 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000364#endif
Guido van Rossum02de8972007-12-19 19:41:06 +0000365 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000366 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000367 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368};
369
Barry Warsaw92971171997-01-03 00:14:25 +0000370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000371PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000372"This module provides mechanisms to use signal handlers in Python.\n\
373\n\
374Functions:\n\
375\n\
376alarm() -- cause SIGALRM after a specified time [Unix only]\n\
377signal() -- set the action for a given signal\n\
378getsignal() -- get the signal action for a given signal\n\
379pause() -- wait until a signal arrives [Unix only]\n\
380default_int_handler() -- default SIGINT handler\n\
381\n\
382Constants:\n\
383\n\
384SIG_DFL -- used to refer to the system default handler\n\
385SIG_IGN -- used to ignore the signal\n\
386NSIG -- number of defined signals\n\
387\n\
388SIGINT, SIGTERM, etc. -- signal numbers\n\
389\n\
390*** IMPORTANT NOTICE ***\n\
391A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000393
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000394PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000395initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000396{
Guido van Rossume4485b01994-09-07 14:32:49 +0000397 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000398 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000399
400#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000401 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000402 main_pid = getpid();
403#endif
404
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000405 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000406 m = Py_InitModule3("signal", signal_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000407 if (m == NULL)
408 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000409
410 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000411 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000412
Guido van Rossum276fa432000-06-30 23:04:18 +0000413 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000414 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
415 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000416
Guido van Rossum276fa432000-06-30 23:04:18 +0000417 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000418 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
419 goto finally;
420
421 x = PyInt_FromLong((long)NSIG);
422 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
423 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000424 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000425
426 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
427 if (!x)
428 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000429 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000430
431 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000432 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000433 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000434 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000435 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000436 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000437 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000438 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000439 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000440 else
Barry Warsaw92971171997-01-03 00:14:25 +0000441 Handlers[i].func = Py_None; /* None of our business */
442 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443 }
Barry Warsaw92971171997-01-03 00:14:25 +0000444 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000445 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000446 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000447 Py_DECREF(Handlers[SIGINT].func);
448 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000449 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000450 }
451
452#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000453 x = PyInt_FromLong(SIGHUP);
454 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000455 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000456#endif
457#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000458 x = PyInt_FromLong(SIGINT);
459 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000460 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000461#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000462#ifdef SIGBREAK
463 x = PyInt_FromLong(SIGBREAK);
464 PyDict_SetItemString(d, "SIGBREAK", x);
465 Py_XDECREF(x);
466#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000467#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000468 x = PyInt_FromLong(SIGQUIT);
469 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000470 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000471#endif
472#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000473 x = PyInt_FromLong(SIGILL);
474 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000475 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000476#endif
477#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000478 x = PyInt_FromLong(SIGTRAP);
479 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000480 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000481#endif
482#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000483 x = PyInt_FromLong(SIGIOT);
484 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000485 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000486#endif
487#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000488 x = PyInt_FromLong(SIGABRT);
489 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000490 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000491#endif
492#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000493 x = PyInt_FromLong(SIGEMT);
494 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000495 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000496#endif
497#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000498 x = PyInt_FromLong(SIGFPE);
499 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000500 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000501#endif
502#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000503 x = PyInt_FromLong(SIGKILL);
504 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000505 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000506#endif
507#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000508 x = PyInt_FromLong(SIGBUS);
509 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000510 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511#endif
512#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000513 x = PyInt_FromLong(SIGSEGV);
514 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000515 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000516#endif
517#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000518 x = PyInt_FromLong(SIGSYS);
519 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000520 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521#endif
522#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000523 x = PyInt_FromLong(SIGPIPE);
524 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000525 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000526#endif
527#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000528 x = PyInt_FromLong(SIGALRM);
529 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000530 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000531#endif
532#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000533 x = PyInt_FromLong(SIGTERM);
534 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000535 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000536#endif
537#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000538 x = PyInt_FromLong(SIGUSR1);
539 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000540 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000541#endif
542#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000543 x = PyInt_FromLong(SIGUSR2);
544 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000545 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000546#endif
547#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000548 x = PyInt_FromLong(SIGCLD);
549 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000550 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000551#endif
552#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000553 x = PyInt_FromLong(SIGCHLD);
554 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000555 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000556#endif
557#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000558 x = PyInt_FromLong(SIGPWR);
559 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000560 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000561#endif
562#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000563 x = PyInt_FromLong(SIGIO);
564 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000565 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000566#endif
567#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000568 x = PyInt_FromLong(SIGURG);
569 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000570 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000571#endif
572#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000573 x = PyInt_FromLong(SIGWINCH);
574 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000575 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000576#endif
577#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000578 x = PyInt_FromLong(SIGPOLL);
579 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000580 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000581#endif
582#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000583 x = PyInt_FromLong(SIGSTOP);
584 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000585 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000586#endif
587#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000588 x = PyInt_FromLong(SIGTSTP);
589 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000590 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000591#endif
592#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000593 x = PyInt_FromLong(SIGCONT);
594 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000595 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000596#endif
597#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000598 x = PyInt_FromLong(SIGTTIN);
599 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000600 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000601#endif
602#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000603 x = PyInt_FromLong(SIGTTOU);
604 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000605 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000606#endif
607#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000608 x = PyInt_FromLong(SIGVTALRM);
609 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000610 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000611#endif
612#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000613 x = PyInt_FromLong(SIGPROF);
614 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000615 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000616#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000617#ifdef SIGXCPU
618 x = PyInt_FromLong(SIGXCPU);
619 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000620 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000621#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000622#ifdef SIGXFSZ
623 x = PyInt_FromLong(SIGXFSZ);
624 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000625 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000626#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000627#ifdef SIGRTMIN
628 x = PyInt_FromLong(SIGRTMIN);
629 PyDict_SetItemString(d, "SIGRTMIN", x);
630 Py_XDECREF(x);
631#endif
632#ifdef SIGRTMAX
633 x = PyInt_FromLong(SIGRTMAX);
634 PyDict_SetItemString(d, "SIGRTMAX", x);
635 Py_XDECREF(x);
636#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000637#ifdef SIGINFO
638 x = PyInt_FromLong(SIGINFO);
639 PyDict_SetItemString(d, "SIGINFO", x);
640 Py_XDECREF(x);
641#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000642 if (!PyErr_Occurred())
643 return;
644
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000645 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000646 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000647 return;
648}
649
650static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000651finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000652{
653 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000654 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000655
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000656 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000657 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000658
659 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000660 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000661 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000662 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000663 if (i != SIGINT && func != NULL && func != Py_None &&
664 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000665 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000666 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000667 }
668
669 Py_XDECREF(IntHandler);
670 IntHandler = NULL;
671 Py_XDECREF(DefaultHandler);
672 DefaultHandler = NULL;
673 Py_XDECREF(IgnoreHandler);
674 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000675}
676
Barry Warsaw92971171997-01-03 00:14:25 +0000677
Barry Warsaw92971171997-01-03 00:14:25 +0000678/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000679int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000680PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000681{
682 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000683 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000684
685 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000686 return 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000687
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000688#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000689 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000690 return 0;
691#endif
Guido van Rossum137c49c2007-12-10 23:00:12 +0000692
693 /*
694 * The is_stripped variable is meant to speed up the calls to
695 * PyErr_CheckSignals (both directly or via pending calls) when no
696 * signal has arrived. This variable is set to 1 when a signal arrives
697 * and it is set to 0 here, when we know some signals arrived. This way
698 * we can run the registered handlers with no signals blocked.
699 *
700 * NOTE: with this approach we can have a situation where is_tripped is
701 * 1 but we have no more signals to handle (Handlers[i].tripped
702 * is 0 for every signal i). This won't do us any harm (except
703 * we're gonna spent some cycles for nothing). This happens when
704 * we receive a signal i after we zero is_tripped and before we
705 * check Handlers[i].tripped.
706 */
707 is_tripped = 0;
708
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000709 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000710 f = Py_None;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000711
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000712 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000713 if (Handlers[i].tripped) {
714 PyObject *result = NULL;
715 PyObject *arglist = Py_BuildValue("(iO)", i, f);
716 Handlers[i].tripped = 0;
717
718 if (arglist) {
719 result = PyEval_CallObject(Handlers[i].func,
720 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000721 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000722 }
Barry Warsaw92971171997-01-03 00:14:25 +0000723 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000724 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000725
726 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000727 }
728 }
Guido van Rossum137c49c2007-12-10 23:00:12 +0000729
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000730 return 0;
731}
732
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000733
Barry Warsaw92971171997-01-03 00:14:25 +0000734/* Replacements for intrcheck.c functionality
735 * Declared in pyerrors.h
736 */
737void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000738PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000739{
Guido van Rossum137c49c2007-12-10 23:00:12 +0000740 is_tripped = 1;
Barry Warsaw92971171997-01-03 00:14:25 +0000741 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000742 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000743}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000744
745void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000746PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000747{
748 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000749 _PyImport_FixupExtension("signal", "signal");
750}
751
752void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000753PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000754{
755 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000756}
757
758int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000759PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000760{
Barry Warsaw92971171997-01-03 00:14:25 +0000761 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000762#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000763 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000764 return 0;
765#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000766 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000767 return 1;
768 }
769 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000770}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000771
772void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000773PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000774{
775#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000776 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000777 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000778 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000779 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000780#endif
781}