blob: e8154925278b4b6c9217b253d199a0c535df087c [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 Rossum644a12b1997-04-09 19:24:53 +000034/* XXX Signals should be recorded per thread, now we have thread state. */
35
Guido van Rossum602099a1994-09-14 13:32:22 +000036#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +000037#include "intrcheck.h"
38
Guido van Rossum644a12b1997-04-09 19:24:53 +000039#ifdef MS_WIN32
40#include <process.h>
41#endif
42
Guido van Rossuma376cc51996-12-05 23:43:35 +000043#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
46
Guido van Rossum398d9fe1994-05-11 08:59:13 +000047#include <signal.h>
48
Guido van Rossumbb4ba121994-06-23 11:25:45 +000049#ifndef SIG_ERR
50#define SIG_ERR ((RETSIGTYPE (*)())-1)
51#endif
52
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000053#ifndef NSIG
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000054#ifdef _SIGMAX
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000055#define NSIG (_SIGMAX + 1) /* For QNX */
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000056#else
57#define NSIG (SIGMAX + 1) /* for djgpp */
58#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000059#endif
60
61
Barry Warsaw92971171997-01-03 00:14:25 +000062
Guido van Rossumbb4ba121994-06-23 11:25:45 +000063/*
64 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
65
66 When threads are supported, we want the following semantics:
67
68 - only the main thread can set a signal handler
69 - any thread can get a signal handler
70 - signals are only delivered to the main thread
71
72 I.e. we don't support "synchronous signals" like SIGFPE (catching
73 this doesn't make much sense in Python anyway) nor do we support
74 signals as a means of inter-thread communication, since not all
75 thread implementations support that (at least our thread library
76 doesn't).
77
78 We still have the problem that in some implementations signals
79 generated by the keyboard (e.g. SIGINT) are delivered to all
80 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
81 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000082 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000083 a working implementation that works in all three cases -- the
84 handler ignores signals if getpid() isn't the same as in the main
85 thread. XXX This is a hack.
86
87*/
88
89#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000090#include <sys/types.h> /* For pid_t */
Guido van Rossumbb4ba121994-06-23 11:25:45 +000091#include "thread.h"
92static long main_thread;
93static pid_t main_pid;
94#endif
95
Barry Warsaw92971171997-01-03 00:14:25 +000096static struct {
97 int tripped;
98 PyObject *func;
99} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000100
Barry Warsaw92971171997-01-03 00:14:25 +0000101static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000102
Barry Warsaw92971171997-01-03 00:14:25 +0000103static PyObject *DefaultHandler;
104static PyObject *IgnoreHandler;
105static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000106
Guido van Rossum08c16611997-08-02 03:01:42 +0000107static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
108
Barry Warsaw92971171997-01-03 00:14:25 +0000109
110
Guido van Rossume4485b01994-09-07 14:32:49 +0000111static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000112signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +0000113 PyObject *self;
114 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115{
Guido van Rossume4485b01994-09-07 14:32:49 +0000116 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000117 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000118}
119
Barry Warsaw92971171997-01-03 00:14:25 +0000120
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000121static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000122signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000123 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000124{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000125#ifdef WITH_THREAD
126 /* See NOTES section above */
127 if (getpid() == main_pid) {
128#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000129 is_tripped++;
130 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000131 Py_AddPendingCall(
132 (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000133#ifdef WITH_THREAD
134 }
135#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000136#ifdef SIGCHLD
137 if (sig_num == SIGCHLD) {
138 /* To avoid infinite recursion, this signal remains
139 reset until explicit re-instated.
140 Don't clear the 'func' field as it is our pointer
141 to the Python handler... */
142 return;
143 }
144#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000145 (void *)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000146}
Guido van Rossume4485b01994-09-07 14:32:49 +0000147
Guido van Rossum06d511d1995-03-10 15:13:48 +0000148
Barry Warsaw92971171997-01-03 00:14:25 +0000149
Guido van Rossum1171ee61997-08-22 20:42:00 +0000150#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000151static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000152signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000153 PyObject *self; /* Not used */
154 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155{
156 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000157 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000158 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000159 /* alarm() returns the number of seconds remaining */
160 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000161}
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 *
Barry Warsaw92971171997-01-03 00:14:25 +0000166signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000167 PyObject *self; /* Not used */
168 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000169{
Guido van Rossume4485b01994-09-07 14:32:49 +0000170 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000171 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000172
Guido van Rossuma597dde1995-01-10 20:56:29 +0000173 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000174 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000175 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000176 /* make sure that any exceptions that got raised are propagated
177 * back into Python
178 */
179 if (PyErr_CheckSignals())
180 return NULL;
181
Guido van Rossume4485b01994-09-07 14:32:49 +0000182 Py_INCREF(Py_None);
183 return Py_None;
184}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000185#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000186
Barry Warsaw92971171997-01-03 00:14:25 +0000187
Guido van Rossume4485b01994-09-07 14:32:49 +0000188static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000189signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000190 PyObject *self; /* Not used */
191 PyObject *args;
192{
193 PyObject *obj;
194 int sig_num;
195 PyObject *old_handler;
196 RETSIGTYPE (*func)();
197 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000198 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000199#ifdef WITH_THREAD
200 if (get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000201 PyErr_SetString(PyExc_ValueError,
202 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000203 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000204 }
205#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000206 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000207 PyErr_SetString(PyExc_ValueError,
208 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000209 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 }
Barry Warsaw92971171997-01-03 00:14:25 +0000211 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000212 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000213 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000214 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000215 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000216 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000218 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000219 }
220 else
Barry Warsaw92971171997-01-03 00:14:25 +0000221 func = signal_handler;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000222 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000223 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000224 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000225 }
Barry Warsaw92971171997-01-03 00:14:25 +0000226 old_handler = Handlers[sig_num].func;
227 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000228 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000229 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000230 return old_handler;
231}
232
Barry Warsaw92971171997-01-03 00:14:25 +0000233
Guido van Rossume4485b01994-09-07 14:32:49 +0000234static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000235signal_get_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000236 PyObject *self; /* Not used */
237 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000238{
239 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000240 PyObject *old_handler;
241 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000242 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000243 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000244 PyErr_SetString(PyExc_ValueError,
245 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000246 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000247 }
Barry Warsaw92971171997-01-03 00:14:25 +0000248 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000249 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000250 return old_handler;
251}
252
253
Barry Warsaw92971171997-01-03 00:14:25 +0000254
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000255/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000256static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000257#ifdef HAVE_ALARM
Barry Warsaw92971171997-01-03 00:14:25 +0000258 {"alarm", signal_alarm},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000259#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000260 {"signal", signal_signal},
261 {"getsignal", signal_get_signal},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000262#ifdef HAVE_PAUSE
Barry Warsaw92971171997-01-03 00:14:25 +0000263 {"pause", signal_pause},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000264#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000265 {"default_int_handler", signal_default_int_handler},
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000266 {NULL, NULL} /* sentinel */
267};
268
Barry Warsaw92971171997-01-03 00:14:25 +0000269
270
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000271void
272initsignal()
273{
Guido van Rossume4485b01994-09-07 14:32:49 +0000274 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000275 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000276
277#ifdef WITH_THREAD
278 main_thread = get_thread_ident();
279 main_pid = getpid();
280#endif
281
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000282 /* Create the module and add the functions */
Barry Warsaw92971171997-01-03 00:14:25 +0000283 m = Py_InitModule("signal", signal_methods);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000284
285 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000286 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000287
Barry Warsaw92971171997-01-03 00:14:25 +0000288 x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
289 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
290 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000291
Barry Warsaw92971171997-01-03 00:14:25 +0000292 x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
293 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
294 goto finally;
295
296 x = PyInt_FromLong((long)NSIG);
297 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
298 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000299 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000300
301 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
302 if (!x)
303 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000304 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000305
306 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000307 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000308 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000309#ifdef HAVE_SIGACTION
310 struct sigaction act;
311 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000312 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000313#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000314 t = signal(i, SIG_IGN);
315 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000316#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000317 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000318 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000319 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000320 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000321 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322 else
Barry Warsaw92971171997-01-03 00:14:25 +0000323 Handlers[i].func = Py_None; /* None of our business */
324 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325 }
Barry Warsaw92971171997-01-03 00:14:25 +0000326 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000327 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000328 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000329 Py_DECREF(Handlers[SIGINT].func);
330 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000331 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332 }
333
334#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000335 x = PyInt_FromLong(SIGHUP);
336 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000337 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000338#endif
339#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000340 x = PyInt_FromLong(SIGINT);
341 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000342 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000343#endif
344#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000345 x = PyInt_FromLong(SIGQUIT);
346 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000347 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348#endif
349#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000350 x = PyInt_FromLong(SIGILL);
351 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000352 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000353#endif
354#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000355 x = PyInt_FromLong(SIGTRAP);
356 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000357 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000358#endif
359#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000360 x = PyInt_FromLong(SIGIOT);
361 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000362 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363#endif
364#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000365 x = PyInt_FromLong(SIGABRT);
366 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000367 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368#endif
369#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000370 x = PyInt_FromLong(SIGEMT);
371 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000372 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000373#endif
374#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000375 x = PyInt_FromLong(SIGFPE);
376 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000377 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000378#endif
379#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000380 x = PyInt_FromLong(SIGKILL);
381 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000382 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000383#endif
384#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000385 x = PyInt_FromLong(SIGBUS);
386 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000387 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000388#endif
389#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000390 x = PyInt_FromLong(SIGSEGV);
391 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000392 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000393#endif
394#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000395 x = PyInt_FromLong(SIGSYS);
396 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000397 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000398#endif
399#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000400 x = PyInt_FromLong(SIGPIPE);
401 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000402 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000403#endif
404#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000405 x = PyInt_FromLong(SIGALRM);
406 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000407 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000408#endif
409#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000410 x = PyInt_FromLong(SIGTERM);
411 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000412 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000413#endif
414#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000415 x = PyInt_FromLong(SIGUSR1);
416 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000417 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000418#endif
419#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000420 x = PyInt_FromLong(SIGUSR2);
421 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000422 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000423#endif
424#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000425 x = PyInt_FromLong(SIGCLD);
426 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000427 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000428#endif
429#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000430 x = PyInt_FromLong(SIGCHLD);
431 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000432 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000433#endif
434#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000435 x = PyInt_FromLong(SIGPWR);
436 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000437 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000438#endif
439#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000440 x = PyInt_FromLong(SIGIO);
441 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000442 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443#endif
444#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000445 x = PyInt_FromLong(SIGURG);
446 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000447 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000448#endif
449#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000450 x = PyInt_FromLong(SIGWINCH);
451 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000452 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000453#endif
454#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000455 x = PyInt_FromLong(SIGPOLL);
456 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000457 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458#endif
459#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000460 x = PyInt_FromLong(SIGSTOP);
461 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000462 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000463#endif
464#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000465 x = PyInt_FromLong(SIGTSTP);
466 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000467 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000468#endif
469#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000470 x = PyInt_FromLong(SIGCONT);
471 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000472 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000473#endif
474#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000475 x = PyInt_FromLong(SIGTTIN);
476 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000477 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478#endif
479#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000480 x = PyInt_FromLong(SIGTTOU);
481 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000482 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483#endif
484#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000485 x = PyInt_FromLong(SIGVTALRM);
486 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000487 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000488#endif
489#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000490 x = PyInt_FromLong(SIGPROF);
491 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000492 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000494#ifdef SIGXCPU
495 x = PyInt_FromLong(SIGXCPU);
496 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000497 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000499#ifdef SIGXFSZ
500 x = PyInt_FromLong(SIGXFSZ);
501 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000502 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000503#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000504 if (!PyErr_Occurred())
505 return;
506
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000507 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000508 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000509 return;
510}
511
512static void
513finisignal()
514{
515 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000516 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000517
518 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000519 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000520
521 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000522 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000523 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000524 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000525 if (i != SIGINT && func != NULL && func != Py_None &&
526 func != DefaultHandler && func != IgnoreHandler)
527 signal(i, SIG_DFL);
528 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000529 }
530
531 Py_XDECREF(IntHandler);
532 IntHandler = NULL;
533 Py_XDECREF(DefaultHandler);
534 DefaultHandler = NULL;
535 Py_XDECREF(IgnoreHandler);
536 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537}
538
Barry Warsaw92971171997-01-03 00:14:25 +0000539
540
541/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000542int
Guido van Rossumec25b911995-01-22 00:46:57 +0000543PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000544{
545 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000546 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000547
548 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000549 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000550#ifdef WITH_THREAD
551 if (get_thread_ident() != main_thread)
552 return 0;
553#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000554 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000555 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000556
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000557 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000558 if (Handlers[i].tripped) {
559 PyObject *result = NULL;
560 PyObject *arglist = Py_BuildValue("(iO)", i, f);
561 Handlers[i].tripped = 0;
562
563 if (arglist) {
564 result = PyEval_CallObject(Handlers[i].func,
565 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000566 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000567 }
Barry Warsaw92971171997-01-03 00:14:25 +0000568 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000569 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000570
571 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000572 }
573 }
Barry Warsaw92971171997-01-03 00:14:25 +0000574 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000575 return 0;
576}
577
Barry Warsaw92971171997-01-03 00:14:25 +0000578
579/* Replacements for intrcheck.c functionality
580 * Declared in pyerrors.h
581 */
582void
583PyErr_SetInterrupt()
584{
585 is_tripped++;
586 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000587 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000588}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000589
590void
Barry Warsaw92971171997-01-03 00:14:25 +0000591PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592{
593 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000594 _PyImport_FixupExtension("signal", "signal");
595}
596
597void
598PyOS_FiniInterrupts()
599{
600 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000601}
602
603int
Barry Warsaw92971171997-01-03 00:14:25 +0000604PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000605{
Barry Warsaw92971171997-01-03 00:14:25 +0000606 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000607#ifdef WITH_THREAD
608 if (get_thread_ident() != main_thread)
609 return 0;
610#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000611 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000612 return 1;
613 }
614 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000615}