blob: 0f9574fe29b4f857b542c97c8ce2392fe11b6890 [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 Rossumbb4ba121994-06-23 11:25:45 +000015#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000016#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000017#endif
18
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000019#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000020#define NSIG 12
21#include <process.h>
22#endif
23
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000024#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000025# if defined(_NSIG)
26# define NSIG _NSIG /* For BSD/SysV */
27# elif defined(_SIGMAX)
28# define NSIG (_SIGMAX + 1) /* For QNX */
29# elif defined(SIGMAX)
30# define NSIG (SIGMAX + 1) /* For djgpp */
31# else
32# define NSIG 64 /* Use a reasonable default value */
33# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000034#endif
35
36
Guido van Rossumbb4ba121994-06-23 11:25:45 +000037/*
38 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
39
40 When threads are supported, we want the following semantics:
41
42 - only the main thread can set a signal handler
43 - any thread can get a signal handler
44 - signals are only delivered to the main thread
45
46 I.e. we don't support "synchronous signals" like SIGFPE (catching
47 this doesn't make much sense in Python anyway) nor do we support
48 signals as a means of inter-thread communication, since not all
49 thread implementations support that (at least our thread library
50 doesn't).
51
52 We still have the problem that in some implementations signals
53 generated by the keyboard (e.g. SIGINT) are delivered to all
54 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
55 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000056 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000057 a working implementation that works in all three cases -- the
58 handler ignores signals if getpid() isn't the same as in the main
59 thread. XXX This is a hack.
60
Guido van Rossum9e8181b2000-09-19 00:46:46 +000061 GNU pth is a user-space threading library, and as such, all threads
62 run within the same process. In this case, if the currently running
63 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000064*/
65
66#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000067#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000068#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000069static long main_thread;
70static pid_t main_pid;
71#endif
72
Barry Warsaw92971171997-01-03 00:14:25 +000073static struct {
74 int tripped;
75 PyObject *func;
76} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000077
Barry Warsaw92971171997-01-03 00:14:25 +000078static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000079
Barry Warsaw92971171997-01-03 00:14:25 +000080static PyObject *DefaultHandler;
81static PyObject *IgnoreHandler;
82static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000083
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000084/* On Solaris 8, gcc will produce a warning that the function
85 declaration is not a prototype. This is caused by the definition of
86 SIG_DFL as (void (*)())0; the correct declaration would have been
87 (void (*)(int))0. */
88
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000089static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000090
Barry Warsaw92971171997-01-03 00:14:25 +000091
Guido van Rossume4485b01994-09-07 14:32:49 +000092static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000093signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094{
Guido van Rossume4485b01994-09-07 14:32:49 +000095 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +000096 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097}
98
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000100"default_int_handler(...)\n\
101\n\
102The default handler for SIGINT instated by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000103It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000104
Thomas Wouters0796b002000-07-22 23:49:30 +0000105
106static int
107checksignals_witharg(void * unused)
108{
109 return PyErr_CheckSignals();
110}
111
Tim Peters4f1b2082000-07-23 21:18:09 +0000112static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000113signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000114{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000115#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000116#ifdef WITH_PTH
117 if (PyThread_get_thread_ident() != main_thread) {
118 pth_raise(*(pth_t *) main_thread, sig_num);
119 return;
120 }
121#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000122 /* See NOTES section above */
123 if (getpid() == main_pid) {
124#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000125 is_tripped++;
126 Handlers[sig_num].tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000127 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000128#ifdef WITH_THREAD
129 }
130#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000131#ifdef SIGCHLD
132 if (sig_num == SIGCHLD) {
133 /* To avoid infinite recursion, this signal remains
134 reset until explicit re-instated.
135 Don't clear the 'func' field as it is our pointer
136 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000137 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000138 }
139#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000140#ifdef HAVE_SIGINTERRUPT
141 siginterrupt(sig_num, 1);
142#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000143 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000144}
Guido van Rossume4485b01994-09-07 14:32:49 +0000145
Guido van Rossum06d511d1995-03-10 15:13:48 +0000146
Guido van Rossum1171ee61997-08-22 20:42:00 +0000147#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000148static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000149signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150{
151 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000152 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000153 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000154 /* alarm() returns the number of seconds remaining */
Fred Drakedff3a372001-07-19 21:29:49 +0000155 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000156}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000158PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000159"alarm(seconds)\n\
160\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000161Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000162#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163
Guido van Rossum1171ee61997-08-22 20:42:00 +0000164#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000165static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000166signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000167{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000168 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000169 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000170 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000171 /* make sure that any exceptions that got raised are propagated
172 * back into Python
173 */
174 if (PyErr_CheckSignals())
175 return NULL;
176
Guido van Rossume4485b01994-09-07 14:32:49 +0000177 Py_INCREF(Py_None);
178 return Py_None;
179}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000180PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000181"pause()\n\
182\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000183Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000184
Guido van Rossum06d511d1995-03-10 15:13:48 +0000185#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000186
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000187
Guido van Rossume4485b01994-09-07 14:32:49 +0000188static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000189signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000190{
191 PyObject *obj;
192 int sig_num;
193 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000194 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000195 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000196 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000197#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000198 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000199 PyErr_SetString(PyExc_ValueError,
200 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000201 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000202 }
203#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000205 PyErr_SetString(PyExc_ValueError,
206 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000207 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000208 }
Barry Warsaw92971171997-01-03 00:14:25 +0000209 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000211 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000212 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000213 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000214 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000216 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217 }
218 else
Barry Warsaw92971171997-01-03 00:14:25 +0000219 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000220#ifdef HAVE_SIGINTERRUPT
221 siginterrupt(sig_num, 1);
222#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000223 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000224 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000225 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000226 }
Barry Warsaw92971171997-01-03 00:14:25 +0000227 old_handler = Handlers[sig_num].func;
228 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000229 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000230 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000231 return old_handler;
232}
233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000234PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000235"signal(sig, action) -> action\n\
236\n\
237Set the action for the given signal. The action can be SIG_DFL,\n\
238SIG_IGN, or a callable Python object. The previous action is\n\
239returned. See getsignal() for possible return values.\n\
240\n\
241*** IMPORTANT NOTICE ***\n\
242A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000243the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000244
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000245
Guido van Rossume4485b01994-09-07 14:32:49 +0000246static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000247signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000248{
249 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000250 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000251 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000252 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000253 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000254 PyErr_SetString(PyExc_ValueError,
255 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000256 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000257 }
Barry Warsaw92971171997-01-03 00:14:25 +0000258 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000259 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260 return old_handler;
261}
262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000263PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000264"getsignal(sig) -> action\n\
265\n\
266Return the current action for the given signal. The return value can be:\n\
267SIG_IGN -- if the signal is being ignored\n\
268SIG_DFL -- if the default action for the signal is in effect\n\
269None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000270anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000271
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000272#ifdef HAVE_SIGPROCMASK /* we assume that having SIGPROCMASK is enough
273 to guarantee full POSIX signal handling */
274/* returns 0 for success, <0 for failure (with exception set) */
275static int
276_signal_list_to_sigset(PyObject* seq, sigset_t* set, char* mesg)
277{
278 int i, len, val;
279
280 seq = PySequence_Fast(seq, mesg);
281 if (!seq)
282 return -1;
283
284 len = PySequence_Fast_GET_SIZE(seq);
285
286 sigemptyset(set);
287
288 for (i = 0; i < len; i++) {
289 val = PyInt_AsLong(PySequence_Fast_GET_ITEM(seq, i));
290 if (val == -1 && PyErr_Occurred()) {
291 Py_DECREF(seq);
292 return -1;
293 }
294 if (sigaddset(set, val) < 0) {
295 Py_DECREF(seq);
296 PyErr_SetFromErrno(PyExc_ValueError);
297 return -1;
298 }
299 }
300
301 Py_DECREF(seq);
302 return 0;
303}
304
305static PyObject*
306_signal_sigset_to_list(sigset_t* set)
307{
308 PyObject* ret;
309 PyObject* ob;
310 int i;
311
312 ret = PyList_New(0);
313 if (!ret)
314 return NULL;
315
316 for (i = 1; i < NSIG; i++) {
317 if (sigismember(set, i)) {
318 ob = PyInt_FromLong(i);
319 if (!ob) {
320 Py_DECREF(ret);
321 return NULL;
322 }
323 PyList_Append(ret, ob);
324 Py_DECREF(ob);
325 }
326 }
327
328 return ret;
329}
330
331static PyObject*
332signal_sigprocmask(PyObject* self, PyObject* args)
333{
334 int how;
335 sigset_t newset, oldset;
336 PyObject* seq;
337
338 if (!PyArg_ParseTuple(args, "iO", &how, &seq))
339 return NULL;
340
341 if (_signal_list_to_sigset(seq, &newset,
342 "sigprocmask requires a sequence") < 0)
343 return NULL;
344
345 if (sigprocmask(how, &newset, &oldset) < 0) {
346 return PyErr_SetFromErrno(PyExc_ValueError);
347 }
348
349 if (PyErr_CheckSignals())
350 return NULL;
351
352 return _signal_sigset_to_list(&oldset);
353}
354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000355PyDoc_STRVAR(sigprocmask_doc,
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000356"sigprocmask(how, sigset) -> sigset\n\
357\n\
358Change the list of currently blocked signals. The parameter how should be\n\
359one of SIG_BLOCK, SIG_UNBLOCK or SIG_SETMASK and sigset should be a\n\
360sequence of signal numbers. The behaviour of the call depends on the value\n\
361of how:\n\
362\n\
363 SIG_BLOCK\n\
364 The set of blocked signals is the union of the current set and the\n\
365 sigset argument.\n\
366 SIG_UNBLOCK\n\
367 The signals in sigset are removed from the current set of blocked\n\
368 signals. It is legal to attempt to unblock a signal which is not\n\
369 blocked.\n\
370 SIG_SETMASK\n\
371 The set of blocked signals is set to the argument set.\n\
372\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000373A list contating the numbers of the previously blocked signals is returned.");
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000374
375static PyObject*
376signal_sigpending(PyObject* self)
377{
378 sigset_t set;
379
380 if (sigpending(&set) < 0) {
381 return PyErr_SetFromErrno(PyExc_ValueError);
382 }
383
384 return _signal_sigset_to_list(&set);
385}
386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(sigpending_doc,
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000388"sigpending() -> sigset\n\
389\n\
390Return the set of pending signals, i.e. a list containing the numbers of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391those signals that have been raised while blocked.");
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000392
393static PyObject*
394signal_sigsuspend(PyObject* self, PyObject* arg)
395{
396 sigset_t set;
397
398 if (_signal_list_to_sigset(arg, &set,
399 "sigsuspend requires a sequence") < 0)
400 return NULL;
401
402 Py_BEGIN_ALLOW_THREADS
403 sigsuspend(&set);
404 Py_END_ALLOW_THREADS
405
406 if (PyErr_CheckSignals())
407 return NULL;
408
409 Py_INCREF(Py_None);
410 return Py_None;
411}
412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000413PyDoc_STRVAR(sigsuspend_doc,
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000414"sigsuspend(sigset) -> None\n\
415\n\
416Temporarily replace the signal mask with sigset (which should be a sequence\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417of signal numbers) and suspend the process until a signal is received.");
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000418#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000419
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000420/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000421static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000422#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000423 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000424#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000425 {"signal", signal_signal, METH_VARARGS, signal_doc},
426 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000427#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000428 {"pause", (PyCFunction)signal_pause,
429 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000430#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000431 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000432 METH_VARARGS, default_int_handler_doc},
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000433#ifdef HAVE_SIGPROCMASK
434 {"sigprocmask", (PyCFunction)signal_sigprocmask,
435 METH_VARARGS, sigprocmask_doc},
436 {"sigpending", (PyCFunction)signal_sigpending,
437 METH_NOARGS, sigpending_doc},
438 {"sigsuspend", (PyCFunction)signal_sigsuspend,
439 METH_O, sigsuspend_doc},
440#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000441 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000442};
443
Barry Warsaw92971171997-01-03 00:14:25 +0000444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000446"This module provides mechanisms to use signal handlers in Python.\n\
447\n\
448Functions:\n\
449\n\
450alarm() -- cause SIGALRM after a specified time [Unix only]\n\
451signal() -- set the action for a given signal\n\
452getsignal() -- get the signal action for a given signal\n\
453pause() -- wait until a signal arrives [Unix only]\n\
454default_int_handler() -- default SIGINT handler\n\
455\n\
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000456sigpending() |\n\
457sigprocmask() |-- posix signal mask handling [Unix only]\n\
458sigsuspend() |\n\
459\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000460Constants:\n\
461\n\
462SIG_DFL -- used to refer to the system default handler\n\
463SIG_IGN -- used to ignore the signal\n\
464NSIG -- number of defined signals\n\
465\n\
466SIGINT, SIGTERM, etc. -- signal numbers\n\
467\n\
468*** IMPORTANT NOTICE ***\n\
469A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000470the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000471
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000472PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000473initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000474{
Guido van Rossume4485b01994-09-07 14:32:49 +0000475 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000476 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000477
478#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000479 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000480 main_pid = getpid();
481#endif
482
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000484 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000485
486 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000487 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000488
Guido van Rossum276fa432000-06-30 23:04:18 +0000489 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000490 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
491 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000492
Guido van Rossum276fa432000-06-30 23:04:18 +0000493 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000494 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
495 goto finally;
496
497 x = PyInt_FromLong((long)NSIG);
498 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
499 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000500 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000501
502 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
503 if (!x)
504 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000505 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000506
507 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000508 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000509 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000510 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000511 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000512 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000513 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000514 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000515 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000516 else
Barry Warsaw92971171997-01-03 00:14:25 +0000517 Handlers[i].func = Py_None; /* None of our business */
518 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000519 }
Barry Warsaw92971171997-01-03 00:14:25 +0000520 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000522 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000523 Py_DECREF(Handlers[SIGINT].func);
524 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000525 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000526 }
527
528#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000529 x = PyInt_FromLong(SIGHUP);
530 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000531 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532#endif
533#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000534 x = PyInt_FromLong(SIGINT);
535 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000536 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000538#ifdef SIGBREAK
539 x = PyInt_FromLong(SIGBREAK);
540 PyDict_SetItemString(d, "SIGBREAK", x);
541 Py_XDECREF(x);
542#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000543#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000544 x = PyInt_FromLong(SIGQUIT);
545 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000546 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547#endif
548#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000549 x = PyInt_FromLong(SIGILL);
550 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000551 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000552#endif
553#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000554 x = PyInt_FromLong(SIGTRAP);
555 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000556 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000557#endif
558#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000559 x = PyInt_FromLong(SIGIOT);
560 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000561 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000562#endif
563#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000564 x = PyInt_FromLong(SIGABRT);
565 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000566 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000567#endif
568#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000569 x = PyInt_FromLong(SIGEMT);
570 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000571 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000572#endif
573#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000574 x = PyInt_FromLong(SIGFPE);
575 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000576 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000577#endif
578#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000579 x = PyInt_FromLong(SIGKILL);
580 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000581 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000582#endif
583#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000584 x = PyInt_FromLong(SIGBUS);
585 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000586 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000587#endif
588#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000589 x = PyInt_FromLong(SIGSEGV);
590 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000591 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592#endif
593#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000594 x = PyInt_FromLong(SIGSYS);
595 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000596 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000597#endif
598#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000599 x = PyInt_FromLong(SIGPIPE);
600 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000601 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000602#endif
603#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000604 x = PyInt_FromLong(SIGALRM);
605 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000606 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000607#endif
608#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000609 x = PyInt_FromLong(SIGTERM);
610 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000611 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000612#endif
613#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000614 x = PyInt_FromLong(SIGUSR1);
615 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000616 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000617#endif
618#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000619 x = PyInt_FromLong(SIGUSR2);
620 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000621 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000622#endif
623#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000624 x = PyInt_FromLong(SIGCLD);
625 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000626 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000627#endif
628#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000629 x = PyInt_FromLong(SIGCHLD);
630 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000631 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000632#endif
633#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000634 x = PyInt_FromLong(SIGPWR);
635 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000636 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000637#endif
638#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000639 x = PyInt_FromLong(SIGIO);
640 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000641 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000642#endif
643#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000644 x = PyInt_FromLong(SIGURG);
645 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000646 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000647#endif
648#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000649 x = PyInt_FromLong(SIGWINCH);
650 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000651 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000652#endif
653#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000654 x = PyInt_FromLong(SIGPOLL);
655 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000656 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000657#endif
658#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000659 x = PyInt_FromLong(SIGSTOP);
660 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000661 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000662#endif
663#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000664 x = PyInt_FromLong(SIGTSTP);
665 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000666 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000667#endif
668#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000669 x = PyInt_FromLong(SIGCONT);
670 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000671 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000672#endif
673#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000674 x = PyInt_FromLong(SIGTTIN);
675 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000676 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000677#endif
678#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000679 x = PyInt_FromLong(SIGTTOU);
680 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000681 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000682#endif
683#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000684 x = PyInt_FromLong(SIGVTALRM);
685 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000686 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000687#endif
688#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000689 x = PyInt_FromLong(SIGPROF);
690 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000691 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000692#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000693#ifdef SIGXCPU
694 x = PyInt_FromLong(SIGXCPU);
695 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000696 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000697#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000698#ifdef SIGXFSZ
699 x = PyInt_FromLong(SIGXFSZ);
700 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000701 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000702#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000703#ifdef SIGINFO
704 x = PyInt_FromLong(SIGINFO);
705 PyDict_SetItemString(d, "SIGINFO", x);
706 Py_XDECREF(x);
707#endif
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000708#ifdef HAVE_SIGPROCMASK
709 x = PyInt_FromLong(SIG_BLOCK);
710 PyDict_SetItemString(d, "SIG_BLOCK", x);
711 Py_XDECREF(x);
712 x = PyInt_FromLong(SIG_UNBLOCK);
713 PyDict_SetItemString(d, "SIG_UNBLOCK", x);
714 Py_XDECREF(x);
715 x = PyInt_FromLong(SIG_SETMASK);
716 PyDict_SetItemString(d, "SIG_SETMASK", x);
717 Py_XDECREF(x);
718#endif
719
Barry Warsaw92971171997-01-03 00:14:25 +0000720 if (!PyErr_Occurred())
721 return;
722
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000723 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000724 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000725 return;
726}
727
728static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000729finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000730{
731 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000732 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000733
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000734 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000735 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000736
737 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000738 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000739 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000740 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000741 if (i != SIGINT && func != NULL && func != Py_None &&
742 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000743 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000744 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000745 }
746
747 Py_XDECREF(IntHandler);
748 IntHandler = NULL;
749 Py_XDECREF(DefaultHandler);
750 DefaultHandler = NULL;
751 Py_XDECREF(IgnoreHandler);
752 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000753}
754
Barry Warsaw92971171997-01-03 00:14:25 +0000755
Barry Warsaw92971171997-01-03 00:14:25 +0000756/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000757int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000758PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000759{
760 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000761 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000762
763 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000764 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000765#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000766 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000767 return 0;
768#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000769 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000770 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000771
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000772 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000773 if (Handlers[i].tripped) {
774 PyObject *result = NULL;
775 PyObject *arglist = Py_BuildValue("(iO)", i, f);
776 Handlers[i].tripped = 0;
777
778 if (arglist) {
779 result = PyEval_CallObject(Handlers[i].func,
780 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000781 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000782 }
Barry Warsaw92971171997-01-03 00:14:25 +0000783 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000784 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000785
786 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000787 }
788 }
Barry Warsaw92971171997-01-03 00:14:25 +0000789 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000790 return 0;
791}
792
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000793
Barry Warsaw92971171997-01-03 00:14:25 +0000794/* Replacements for intrcheck.c functionality
795 * Declared in pyerrors.h
796 */
797void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000798PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000799{
800 is_tripped++;
801 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000802 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000803}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000804
805void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000806PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000807{
808 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000809 _PyImport_FixupExtension("signal", "signal");
810}
811
812void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000813PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000814{
815 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000816}
817
818int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000819PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000820{
Barry Warsaw92971171997-01-03 00:14:25 +0000821 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000822#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000823 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000824 return 0;
825#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000826 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000827 return 1;
828 }
829 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000830}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000831
832void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000833PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000834{
835#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000836 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000837 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000838 main_pid = getpid();
839#endif
840}