blob: 7ca697afc7f9e80af9a36d43bdb9783b7cfb925b [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum398d9fe1994-05-11 08:59:13 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum398d9fe1994-05-11 08:59:13 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum398d9fe1994-05-11 08:59:13 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum398d9fe1994-05-11 08:59:13 +000029
30******************************************************************/
31
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000032/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000033
Guido van Rossum602099a1994-09-14 13:32:22 +000034#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +000035#include "intrcheck.h"
36
Guido van Rossuma376cc51996-12-05 23:43:35 +000037#ifdef HAVE_UNISTD_H
38#include <unistd.h>
39#endif
40
Guido van Rossum398d9fe1994-05-11 08:59:13 +000041#include <signal.h>
42
Guido van Rossumbb4ba121994-06-23 11:25:45 +000043#ifndef SIG_ERR
44#define SIG_ERR ((RETSIGTYPE (*)())-1)
45#endif
46
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000047#ifndef NSIG
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000048#ifdef _SIGMAX
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000049#define NSIG (_SIGMAX + 1) /* For QNX */
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000050#else
51#define NSIG (SIGMAX + 1) /* for djgpp */
52#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000053#endif
54
55
Barry Warsaw92971171997-01-03 00:14:25 +000056
Guido van Rossumbb4ba121994-06-23 11:25:45 +000057/*
58 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
59
60 When threads are supported, we want the following semantics:
61
62 - only the main thread can set a signal handler
63 - any thread can get a signal handler
64 - signals are only delivered to the main thread
65
66 I.e. we don't support "synchronous signals" like SIGFPE (catching
67 this doesn't make much sense in Python anyway) nor do we support
68 signals as a means of inter-thread communication, since not all
69 thread implementations support that (at least our thread library
70 doesn't).
71
72 We still have the problem that in some implementations signals
73 generated by the keyboard (e.g. SIGINT) are delivered to all
74 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
75 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000076 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000077 a working implementation that works in all three cases -- the
78 handler ignores signals if getpid() isn't the same as in the main
79 thread. XXX This is a hack.
80
81*/
82
83#ifdef WITH_THREAD
84#include "thread.h"
85static long main_thread;
86static pid_t main_pid;
87#endif
88
Barry Warsaw92971171997-01-03 00:14:25 +000089static struct {
90 int tripped;
91 PyObject *func;
92} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000093
Barry Warsaw92971171997-01-03 00:14:25 +000094static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000095
Barry Warsaw92971171997-01-03 00:14:25 +000096static PyObject *DefaultHandler;
97static PyObject *IgnoreHandler;
98static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099
Barry Warsaw92971171997-01-03 00:14:25 +0000100
101
Guido van Rossume4485b01994-09-07 14:32:49 +0000102static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000103signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +0000104 PyObject *self;
105 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000106{
Guido van Rossume4485b01994-09-07 14:32:49 +0000107 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000108 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000109}
110
Barry Warsaw92971171997-01-03 00:14:25 +0000111
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000112static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000113signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000114 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000116#ifdef WITH_THREAD
117 /* See NOTES section above */
118 if (getpid() == main_pid) {
119#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000120 is_tripped++;
121 Handlers[sig_num].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000122 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000123#ifdef WITH_THREAD
124 }
125#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000126#ifdef SIGCHLD
127 if (sig_num == SIGCHLD) {
128 /* To avoid infinite recursion, this signal remains
129 reset until explicit re-instated.
130 Don't clear the 'func' field as it is our pointer
131 to the Python handler... */
132 return;
133 }
134#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000135 (void *)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000136}
Guido van Rossume4485b01994-09-07 14:32:49 +0000137
Guido van Rossum06d511d1995-03-10 15:13:48 +0000138
Barry Warsaw92971171997-01-03 00:14:25 +0000139
Guido van Rossum06d511d1995-03-10 15:13:48 +0000140#ifndef DONT_HAVE_SIG_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000141static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000142signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000143 PyObject *self; /* Not used */
144 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145{
146 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000147 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000148 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000149 /* alarm() returns the number of seconds remaining */
150 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000151}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000152#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153
Guido van Rossum06d511d1995-03-10 15:13:48 +0000154#ifndef DONT_HAVE_SIG_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000155static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000156signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000157 PyObject *self; /* Not used */
158 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000159{
Guido van Rossume4485b01994-09-07 14:32:49 +0000160 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000161 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000162
Guido van Rossuma597dde1995-01-10 20:56:29 +0000163 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000164 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000165 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000166 /* make sure that any exceptions that got raised are propagated
167 * back into Python
168 */
169 if (PyErr_CheckSignals())
170 return NULL;
171
Guido van Rossume4485b01994-09-07 14:32:49 +0000172 Py_INCREF(Py_None);
173 return Py_None;
174}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000175#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000176
Barry Warsaw92971171997-01-03 00:14:25 +0000177
Guido van Rossume4485b01994-09-07 14:32:49 +0000178static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000179signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000180 PyObject *self; /* Not used */
181 PyObject *args;
182{
183 PyObject *obj;
184 int sig_num;
185 PyObject *old_handler;
186 RETSIGTYPE (*func)();
187 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000188 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000189#ifdef WITH_THREAD
190 if (get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000191 PyErr_SetString(PyExc_ValueError,
192 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000193 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000194 }
195#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000196 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000197 PyErr_SetString(PyExc_ValueError,
198 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000199 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000200 }
Barry Warsaw92971171997-01-03 00:14:25 +0000201 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000202 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000203 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000205 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000206 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000207"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000208 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000209 }
210 else
Barry Warsaw92971171997-01-03 00:14:25 +0000211 func = signal_handler;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000212 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000213 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000214 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000215 }
Barry Warsaw92971171997-01-03 00:14:25 +0000216 old_handler = Handlers[sig_num].func;
217 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000218 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000219 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000220 return old_handler;
221}
222
Barry Warsaw92971171997-01-03 00:14:25 +0000223
Guido van Rossume4485b01994-09-07 14:32:49 +0000224static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000225signal_get_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000226 PyObject *self; /* Not used */
227 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000228{
229 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000230 PyObject *old_handler;
231 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000232 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000233 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000234 PyErr_SetString(PyExc_ValueError,
235 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000236 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000237 }
Barry Warsaw92971171997-01-03 00:14:25 +0000238 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000239 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000240 return old_handler;
241}
242
243
Barry Warsaw92971171997-01-03 00:14:25 +0000244
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000245/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000246static PyMethodDef signal_methods[] = {
Guido van Rossum06d511d1995-03-10 15:13:48 +0000247#ifndef DONT_HAVE_SIG_ALARM
Barry Warsaw92971171997-01-03 00:14:25 +0000248 {"alarm", signal_alarm},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000249#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000250 {"signal", signal_signal},
251 {"getsignal", signal_get_signal},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000252#ifndef DONT_HAVE_SIG_PAUSE
Barry Warsaw92971171997-01-03 00:14:25 +0000253 {"pause", signal_pause},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000254#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000255 {"default_int_handler", signal_default_int_handler},
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000256 {NULL, NULL} /* sentinel */
257};
258
Barry Warsaw92971171997-01-03 00:14:25 +0000259
260
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000261void
262initsignal()
263{
Guido van Rossume4485b01994-09-07 14:32:49 +0000264 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000265 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000266
267#ifdef WITH_THREAD
268 main_thread = get_thread_ident();
269 main_pid = getpid();
270#endif
271
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000272 /* Create the module and add the functions */
Barry Warsaw92971171997-01-03 00:14:25 +0000273 m = Py_InitModule("signal", signal_methods);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000274
275 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000276 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000277
Barry Warsaw92971171997-01-03 00:14:25 +0000278 x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
279 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
280 goto finally;
281 Py_DECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000282
Barry Warsaw92971171997-01-03 00:14:25 +0000283 x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
284 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
285 goto finally;
286
287 x = PyInt_FromLong((long)NSIG);
288 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
289 goto finally;
290
291 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
292 if (!x)
293 goto finally;
294
295 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000296 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000297 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000298#ifdef HAVE_SIGACTION
299 struct sigaction act;
300 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000301 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000302#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000303 t = signal(i, SIG_IGN);
304 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000305#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000306 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000307 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000308 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000309 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000310 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000311 else
Barry Warsaw92971171997-01-03 00:14:25 +0000312 Handlers[i].func = Py_None; /* None of our business */
313 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000314 }
Barry Warsaw92971171997-01-03 00:14:25 +0000315 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000316 /* Install default int handler */
Barry Warsaw92971171997-01-03 00:14:25 +0000317 Py_DECREF(Handlers[SIGINT].func);
318 Handlers[SIGINT].func = IntHandler;
319 Py_INCREF(IntHandler);
320 signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000321 }
322
323#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000324 x = PyInt_FromLong(SIGHUP);
325 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000326 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000327#endif
328#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000329 x = PyInt_FromLong(SIGINT);
330 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000331 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332#endif
333#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000334 x = PyInt_FromLong(SIGQUIT);
335 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000336 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000337#endif
338#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000339 x = PyInt_FromLong(SIGILL);
340 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000341 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000342#endif
343#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000344 x = PyInt_FromLong(SIGTRAP);
345 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000346 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000347#endif
348#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000349 x = PyInt_FromLong(SIGIOT);
350 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000351 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000352#endif
353#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000354 x = PyInt_FromLong(SIGABRT);
355 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000356 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000357#endif
358#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000359 x = PyInt_FromLong(SIGEMT);
360 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000361 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000362#endif
363#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000364 x = PyInt_FromLong(SIGFPE);
365 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000366 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000367#endif
368#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000369 x = PyInt_FromLong(SIGKILL);
370 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000371 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372#endif
373#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000374 x = PyInt_FromLong(SIGBUS);
375 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000376 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000377#endif
378#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000379 x = PyInt_FromLong(SIGSEGV);
380 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000381 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000382#endif
383#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000384 x = PyInt_FromLong(SIGSYS);
385 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000386 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000387#endif
388#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000389 x = PyInt_FromLong(SIGPIPE);
390 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000391 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392#endif
393#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000394 x = PyInt_FromLong(SIGALRM);
395 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000396 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000397#endif
398#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000399 x = PyInt_FromLong(SIGTERM);
400 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000401 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000402#endif
403#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000404 x = PyInt_FromLong(SIGUSR1);
405 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000406 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000407#endif
408#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000409 x = PyInt_FromLong(SIGUSR2);
410 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000411 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000412#endif
413#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000414 x = PyInt_FromLong(SIGCLD);
415 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000416 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000417#endif
418#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000419 x = PyInt_FromLong(SIGCHLD);
420 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000421 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000422#endif
423#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000424 x = PyInt_FromLong(SIGPWR);
425 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000426 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000427#endif
428#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000429 x = PyInt_FromLong(SIGIO);
430 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000431 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000432#endif
433#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000434 x = PyInt_FromLong(SIGURG);
435 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000436 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000437#endif
438#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000439 x = PyInt_FromLong(SIGWINCH);
440 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000441 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000442#endif
443#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000444 x = PyInt_FromLong(SIGPOLL);
445 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000446 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000447#endif
448#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000449 x = PyInt_FromLong(SIGSTOP);
450 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000451 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000452#endif
453#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000454 x = PyInt_FromLong(SIGTSTP);
455 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000456 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000457#endif
458#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000459 x = PyInt_FromLong(SIGCONT);
460 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000461 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000462#endif
463#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000464 x = PyInt_FromLong(SIGTTIN);
465 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000466 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000467#endif
468#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000469 x = PyInt_FromLong(SIGTTOU);
470 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000471 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000472#endif
473#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000474 x = PyInt_FromLong(SIGVTALRM);
475 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000476 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477#endif
478#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000479 x = PyInt_FromLong(SIGPROF);
480 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000481 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000482#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000483#ifdef SIGXCPU
484 x = PyInt_FromLong(SIGXCPU);
485 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000486 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000487#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000488#ifdef SIGXFSZ
489 x = PyInt_FromLong(SIGXFSZ);
490 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000491 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000492#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000493 if (!PyErr_Occurred())
494 return;
495
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000496 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000497 finally:
498 Py_FatalError("can't initialize module signal");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000499}
500
Barry Warsaw92971171997-01-03 00:14:25 +0000501
502
503/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000504int
Guido van Rossumec25b911995-01-22 00:46:57 +0000505PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000506{
507 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000508 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000509
510 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000511 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000512#ifdef WITH_THREAD
513 if (get_thread_ident() != main_thread)
514 return 0;
515#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000516 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000517 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000518
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000519 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000520 if (Handlers[i].tripped) {
521 PyObject *result = NULL;
522 PyObject *arglist = Py_BuildValue("(iO)", i, f);
523 Handlers[i].tripped = 0;
524
525 if (arglist) {
526 result = PyEval_CallObject(Handlers[i].func,
527 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000528 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000529 }
Barry Warsaw92971171997-01-03 00:14:25 +0000530 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000531 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000532
533 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000534 }
535 }
Barry Warsaw92971171997-01-03 00:14:25 +0000536 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537 return 0;
538}
539
Barry Warsaw92971171997-01-03 00:14:25 +0000540
541/* Replacements for intrcheck.c functionality
542 * Declared in pyerrors.h
543 */
544void
545PyErr_SetInterrupt()
546{
547 is_tripped++;
548 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000549 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000550}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000551
552void
Barry Warsaw92971171997-01-03 00:14:25 +0000553PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000554{
555 initsignal();
556}
557
558int
Barry Warsaw92971171997-01-03 00:14:25 +0000559PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000560{
Barry Warsaw92971171997-01-03 00:14:25 +0000561 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000562#ifdef WITH_THREAD
563 if (get_thread_ident() != main_thread)
564 return 0;
565#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000566 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000567 return 1;
568 }
569 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000570}