blob: 175f6eb558fb1a9241ca3719626b90387cddf34c [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 Rossum8e9ebfd1997-11-22 21:53:48 +000053#if defined(PYOS_OS2)
54#define NSIG 12
55#include <process.h>
56#endif
57
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000058#ifndef NSIG
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000059#ifdef _SIGMAX
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000060#define NSIG (_SIGMAX + 1) /* For QNX */
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000061#else
62#define NSIG (SIGMAX + 1) /* for djgpp */
63#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000064#endif
65
66
Barry Warsaw92971171997-01-03 00:14:25 +000067
Guido van Rossumbb4ba121994-06-23 11:25:45 +000068/*
69 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
70
71 When threads are supported, we want the following semantics:
72
73 - only the main thread can set a signal handler
74 - any thread can get a signal handler
75 - signals are only delivered to the main thread
76
77 I.e. we don't support "synchronous signals" like SIGFPE (catching
78 this doesn't make much sense in Python anyway) nor do we support
79 signals as a means of inter-thread communication, since not all
80 thread implementations support that (at least our thread library
81 doesn't).
82
83 We still have the problem that in some implementations signals
84 generated by the keyboard (e.g. SIGINT) are delivered to all
85 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
86 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000087 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000088 a working implementation that works in all three cases -- the
89 handler ignores signals if getpid() isn't the same as in the main
90 thread. XXX This is a hack.
91
92*/
93
94#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000095#include <sys/types.h> /* For pid_t */
Guido van Rossumbb4ba121994-06-23 11:25:45 +000096#include "thread.h"
97static long main_thread;
98static pid_t main_pid;
99#endif
100
Barry Warsaw92971171997-01-03 00:14:25 +0000101static struct {
102 int tripped;
103 PyObject *func;
104} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000105
Barry Warsaw92971171997-01-03 00:14:25 +0000106static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000107
Barry Warsaw92971171997-01-03 00:14:25 +0000108static PyObject *DefaultHandler;
109static PyObject *IgnoreHandler;
110static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000111
Guido van Rossum08c16611997-08-02 03:01:42 +0000112static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
113
Barry Warsaw92971171997-01-03 00:14:25 +0000114
115
Guido van Rossume4485b01994-09-07 14:32:49 +0000116static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000117signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +0000118 PyObject *self;
119 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000120{
Guido van Rossume4485b01994-09-07 14:32:49 +0000121 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000122 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000123}
124
Barry Warsaw92971171997-01-03 00:14:25 +0000125
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000126static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000127signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000128 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000129{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000130#ifdef WITH_THREAD
131 /* See NOTES section above */
132 if (getpid() == main_pid) {
133#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000134 is_tripped++;
135 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000136 Py_AddPendingCall(
137 (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000138#ifdef WITH_THREAD
139 }
140#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000141#ifdef SIGCHLD
142 if (sig_num == SIGCHLD) {
143 /* To avoid infinite recursion, this signal remains
144 reset until explicit re-instated.
145 Don't clear the 'func' field as it is our pointer
146 to the Python handler... */
147 return;
148 }
149#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000150 (void *)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000151}
Guido van Rossume4485b01994-09-07 14:32:49 +0000152
Guido van Rossum06d511d1995-03-10 15:13:48 +0000153
Barry Warsaw92971171997-01-03 00:14:25 +0000154
Guido van Rossum1171ee61997-08-22 20:42:00 +0000155#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000156static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000157signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000158 PyObject *self; /* Not used */
159 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160{
161 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000162 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000163 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000164 /* alarm() returns the number of seconds remaining */
165 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000166}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000167#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168
Guido van Rossum1171ee61997-08-22 20:42:00 +0000169#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000170static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000171signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000172 PyObject *self; /* Not used */
173 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000174{
Guido van Rossume4485b01994-09-07 14:32:49 +0000175 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000176 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000177
Guido van Rossuma597dde1995-01-10 20:56:29 +0000178 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000179 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000180 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000181 /* make sure that any exceptions that got raised are propagated
182 * back into Python
183 */
184 if (PyErr_CheckSignals())
185 return NULL;
186
Guido van Rossume4485b01994-09-07 14:32:49 +0000187 Py_INCREF(Py_None);
188 return Py_None;
189}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000190#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000191
Barry Warsaw92971171997-01-03 00:14:25 +0000192
Guido van Rossume4485b01994-09-07 14:32:49 +0000193static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000194signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000195 PyObject *self; /* Not used */
196 PyObject *args;
197{
198 PyObject *obj;
199 int sig_num;
200 PyObject *old_handler;
201 RETSIGTYPE (*func)();
202 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000203 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000204#ifdef WITH_THREAD
205 if (get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000206 PyErr_SetString(PyExc_ValueError,
207 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000208 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000209 }
210#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000211 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000212 PyErr_SetString(PyExc_ValueError,
213 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000214 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215 }
Barry Warsaw92971171997-01-03 00:14:25 +0000216 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000218 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000219 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000220 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000221 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000222"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000223 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000224 }
225 else
Barry Warsaw92971171997-01-03 00:14:25 +0000226 func = signal_handler;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000227 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000228 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000229 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000230 }
Barry Warsaw92971171997-01-03 00:14:25 +0000231 old_handler = Handlers[sig_num].func;
232 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000233 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000234 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000235 return old_handler;
236}
237
Barry Warsaw92971171997-01-03 00:14:25 +0000238
Guido van Rossume4485b01994-09-07 14:32:49 +0000239static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000240signal_get_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000241 PyObject *self; /* Not used */
242 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000243{
244 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000245 PyObject *old_handler;
246 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000247 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000248 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000249 PyErr_SetString(PyExc_ValueError,
250 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000251 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000252 }
Barry Warsaw92971171997-01-03 00:14:25 +0000253 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000254 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000255 return old_handler;
256}
257
258
Barry Warsaw92971171997-01-03 00:14:25 +0000259
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000261static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000262#ifdef HAVE_ALARM
Barry Warsaw92971171997-01-03 00:14:25 +0000263 {"alarm", signal_alarm},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000264#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000265 {"signal", signal_signal},
266 {"getsignal", signal_get_signal},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000267#ifdef HAVE_PAUSE
Barry Warsaw92971171997-01-03 00:14:25 +0000268 {"pause", signal_pause},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000269#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000270 {"default_int_handler", signal_default_int_handler},
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000271 {NULL, NULL} /* sentinel */
272};
273
Barry Warsaw92971171997-01-03 00:14:25 +0000274
275
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000276void
277initsignal()
278{
Guido van Rossume4485b01994-09-07 14:32:49 +0000279 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000280 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000281
282#ifdef WITH_THREAD
283 main_thread = get_thread_ident();
284 main_pid = getpid();
285#endif
286
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000287 /* Create the module and add the functions */
Barry Warsaw92971171997-01-03 00:14:25 +0000288 m = Py_InitModule("signal", signal_methods);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000289
290 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000291 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000292
Barry Warsaw92971171997-01-03 00:14:25 +0000293 x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
294 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
295 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000296
Barry Warsaw92971171997-01-03 00:14:25 +0000297 x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
298 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
299 goto finally;
300
301 x = PyInt_FromLong((long)NSIG);
302 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
303 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000304 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000305
306 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
307 if (!x)
308 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000309 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000310
311 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000312 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000313 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000314#ifdef HAVE_SIGACTION
315 struct sigaction act;
316 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000317 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000318#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000319 t = signal(i, SIG_IGN);
320 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000321#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000322 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000323 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000324 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000326 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000327 else
Barry Warsaw92971171997-01-03 00:14:25 +0000328 Handlers[i].func = Py_None; /* None of our business */
329 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000330 }
Barry Warsaw92971171997-01-03 00:14:25 +0000331 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000333 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000334 Py_DECREF(Handlers[SIGINT].func);
335 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000336 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000337 }
338
339#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000340 x = PyInt_FromLong(SIGHUP);
341 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000342 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000343#endif
344#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000345 x = PyInt_FromLong(SIGINT);
346 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000347 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348#endif
349#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000350 x = PyInt_FromLong(SIGQUIT);
351 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000352 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000353#endif
354#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000355 x = PyInt_FromLong(SIGILL);
356 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000357 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000358#endif
359#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000360 x = PyInt_FromLong(SIGTRAP);
361 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000362 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363#endif
364#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000365 x = PyInt_FromLong(SIGIOT);
366 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000367 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368#endif
369#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000370 x = PyInt_FromLong(SIGABRT);
371 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000372 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000373#endif
374#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000375 x = PyInt_FromLong(SIGEMT);
376 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000377 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000378#endif
379#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000380 x = PyInt_FromLong(SIGFPE);
381 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000382 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000383#endif
384#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000385 x = PyInt_FromLong(SIGKILL);
386 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000387 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000388#endif
389#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000390 x = PyInt_FromLong(SIGBUS);
391 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000392 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000393#endif
394#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000395 x = PyInt_FromLong(SIGSEGV);
396 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000397 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000398#endif
399#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000400 x = PyInt_FromLong(SIGSYS);
401 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000402 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000403#endif
404#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000405 x = PyInt_FromLong(SIGPIPE);
406 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000407 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000408#endif
409#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000410 x = PyInt_FromLong(SIGALRM);
411 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000412 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000413#endif
414#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000415 x = PyInt_FromLong(SIGTERM);
416 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000417 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000418#endif
419#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000420 x = PyInt_FromLong(SIGUSR1);
421 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000422 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000423#endif
424#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000425 x = PyInt_FromLong(SIGUSR2);
426 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000427 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000428#endif
429#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000430 x = PyInt_FromLong(SIGCLD);
431 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000432 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000433#endif
434#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000435 x = PyInt_FromLong(SIGCHLD);
436 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000437 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000438#endif
439#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000440 x = PyInt_FromLong(SIGPWR);
441 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000442 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443#endif
444#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000445 x = PyInt_FromLong(SIGIO);
446 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000447 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000448#endif
449#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000450 x = PyInt_FromLong(SIGURG);
451 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000452 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000453#endif
454#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000455 x = PyInt_FromLong(SIGWINCH);
456 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000457 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458#endif
459#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000460 x = PyInt_FromLong(SIGPOLL);
461 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000462 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000463#endif
464#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000465 x = PyInt_FromLong(SIGSTOP);
466 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000467 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000468#endif
469#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000470 x = PyInt_FromLong(SIGTSTP);
471 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000472 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000473#endif
474#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000475 x = PyInt_FromLong(SIGCONT);
476 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000477 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478#endif
479#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000480 x = PyInt_FromLong(SIGTTIN);
481 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000482 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483#endif
484#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000485 x = PyInt_FromLong(SIGTTOU);
486 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000487 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000488#endif
489#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000490 x = PyInt_FromLong(SIGVTALRM);
491 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000492 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493#endif
494#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000495 x = PyInt_FromLong(SIGPROF);
496 PyDict_SetItemString(d, "SIGPROF", 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 SIGXCPU
500 x = PyInt_FromLong(SIGXCPU);
501 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000502 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000504#ifdef SIGXFSZ
505 x = PyInt_FromLong(SIGXFSZ);
506 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000507 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000508#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000509 if (!PyErr_Occurred())
510 return;
511
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000512 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000513 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000514 return;
515}
516
517static void
518finisignal()
519{
520 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000521 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000522
523 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000524 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000525
526 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000527 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000528 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000529 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000530 if (i != SIGINT && func != NULL && func != Py_None &&
531 func != DefaultHandler && func != IgnoreHandler)
532 signal(i, SIG_DFL);
533 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000534 }
535
536 Py_XDECREF(IntHandler);
537 IntHandler = NULL;
538 Py_XDECREF(DefaultHandler);
539 DefaultHandler = NULL;
540 Py_XDECREF(IgnoreHandler);
541 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000542}
543
Barry Warsaw92971171997-01-03 00:14:25 +0000544
545
546/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547int
Guido van Rossumec25b911995-01-22 00:46:57 +0000548PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000549{
550 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000551 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000552
553 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000554 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000555#ifdef WITH_THREAD
556 if (get_thread_ident() != main_thread)
557 return 0;
558#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000559 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000560 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000561
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000562 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000563 if (Handlers[i].tripped) {
564 PyObject *result = NULL;
565 PyObject *arglist = Py_BuildValue("(iO)", i, f);
566 Handlers[i].tripped = 0;
567
568 if (arglist) {
569 result = PyEval_CallObject(Handlers[i].func,
570 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000571 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000572 }
Barry Warsaw92971171997-01-03 00:14:25 +0000573 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000574 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000575
576 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000577 }
578 }
Barry Warsaw92971171997-01-03 00:14:25 +0000579 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000580 return 0;
581}
582
Barry Warsaw92971171997-01-03 00:14:25 +0000583
584/* Replacements for intrcheck.c functionality
585 * Declared in pyerrors.h
586 */
587void
588PyErr_SetInterrupt()
589{
590 is_tripped++;
591 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000592 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000593}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000594
595void
Barry Warsaw92971171997-01-03 00:14:25 +0000596PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000597{
598 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000599 _PyImport_FixupExtension("signal", "signal");
600}
601
602void
603PyOS_FiniInterrupts()
604{
605 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000606}
607
608int
Barry Warsaw92971171997-01-03 00:14:25 +0000609PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000610{
Barry Warsaw92971171997-01-03 00:14:25 +0000611 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000612#ifdef WITH_THREAD
613 if (get_thread_ident() != main_thread)
614 return 0;
615#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000616 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000617 return 1;
618 }
619 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000620}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000621
622void
623PyOS_AfterFork()
624{
625#ifdef WITH_THREAD
626 main_thread = get_thread_ident();
627 main_pid = getpid();
628#endif
629}