blob: e62be1fef03b845f098d6c2ba26fb246d5d68610 [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
90#include "thread.h"
91static long main_thread;
92static pid_t main_pid;
93#endif
94
Barry Warsaw92971171997-01-03 00:14:25 +000095static struct {
96 int tripped;
97 PyObject *func;
98} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099
Barry Warsaw92971171997-01-03 00:14:25 +0000100static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000101
Barry Warsaw92971171997-01-03 00:14:25 +0000102static PyObject *DefaultHandler;
103static PyObject *IgnoreHandler;
104static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000105
Barry Warsaw92971171997-01-03 00:14:25 +0000106
107
Guido van Rossume4485b01994-09-07 14:32:49 +0000108static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000109signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +0000110 PyObject *self;
111 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000112{
Guido van Rossume4485b01994-09-07 14:32:49 +0000113 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000114 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115}
116
Barry Warsaw92971171997-01-03 00:14:25 +0000117
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000118static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000119signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000120 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000121{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000122#ifdef WITH_THREAD
123 /* See NOTES section above */
124 if (getpid() == main_pid) {
125#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000126 is_tripped++;
127 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000128 Py_AddPendingCall(
129 (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000130#ifdef WITH_THREAD
131 }
132#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000133#ifdef SIGCHLD
134 if (sig_num == SIGCHLD) {
135 /* To avoid infinite recursion, this signal remains
136 reset until explicit re-instated.
137 Don't clear the 'func' field as it is our pointer
138 to the Python handler... */
139 return;
140 }
141#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000142 (void *)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000143}
Guido van Rossume4485b01994-09-07 14:32:49 +0000144
Guido van Rossum06d511d1995-03-10 15:13:48 +0000145
Barry Warsaw92971171997-01-03 00:14:25 +0000146
Guido van Rossum06d511d1995-03-10 15:13:48 +0000147#ifndef DONT_HAVE_SIG_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000148static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000149signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000150 PyObject *self; /* Not used */
151 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152{
153 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000154 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000155 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000156 /* alarm() returns the number of seconds remaining */
157 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000158}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000159#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160
Guido van Rossum06d511d1995-03-10 15:13:48 +0000161#ifndef DONT_HAVE_SIG_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000162static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000163signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000164 PyObject *self; /* Not used */
165 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000166{
Guido van Rossume4485b01994-09-07 14:32:49 +0000167 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000168 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000169
Guido van Rossuma597dde1995-01-10 20:56:29 +0000170 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000171 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000172 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000173 /* make sure that any exceptions that got raised are propagated
174 * back into Python
175 */
176 if (PyErr_CheckSignals())
177 return NULL;
178
Guido van Rossume4485b01994-09-07 14:32:49 +0000179 Py_INCREF(Py_None);
180 return Py_None;
181}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000182#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000183
Barry Warsaw92971171997-01-03 00:14:25 +0000184
Guido van Rossume4485b01994-09-07 14:32:49 +0000185static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000186signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000187 PyObject *self; /* Not used */
188 PyObject *args;
189{
190 PyObject *obj;
191 int sig_num;
192 PyObject *old_handler;
193 RETSIGTYPE (*func)();
194 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000195 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000196#ifdef WITH_THREAD
197 if (get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000198 PyErr_SetString(PyExc_ValueError,
199 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000200 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000201 }
202#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000203 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000204 PyErr_SetString(PyExc_ValueError,
205 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000206 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000207 }
Barry Warsaw92971171997-01-03 00:14:25 +0000208 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000209 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000210 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000211 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000212 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000213 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000214"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000215 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000216 }
217 else
Barry Warsaw92971171997-01-03 00:14:25 +0000218 func = signal_handler;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000219 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000220 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000221 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000222 }
Barry Warsaw92971171997-01-03 00:14:25 +0000223 old_handler = Handlers[sig_num].func;
224 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000225 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000226 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000227 return old_handler;
228}
229
Barry Warsaw92971171997-01-03 00:14:25 +0000230
Guido van Rossume4485b01994-09-07 14:32:49 +0000231static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000232signal_get_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000233 PyObject *self; /* Not used */
234 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000235{
236 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000237 PyObject *old_handler;
238 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000239 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000240 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000241 PyErr_SetString(PyExc_ValueError,
242 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000243 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000244 }
Barry Warsaw92971171997-01-03 00:14:25 +0000245 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000246 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000247 return old_handler;
248}
249
250
Barry Warsaw92971171997-01-03 00:14:25 +0000251
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000252/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000253static PyMethodDef signal_methods[] = {
Guido van Rossum06d511d1995-03-10 15:13:48 +0000254#ifndef DONT_HAVE_SIG_ALARM
Barry Warsaw92971171997-01-03 00:14:25 +0000255 {"alarm", signal_alarm},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000256#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000257 {"signal", signal_signal},
258 {"getsignal", signal_get_signal},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000259#ifndef DONT_HAVE_SIG_PAUSE
Barry Warsaw92971171997-01-03 00:14:25 +0000260 {"pause", signal_pause},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000261#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000262 {"default_int_handler", signal_default_int_handler},
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000263 {NULL, NULL} /* sentinel */
264};
265
Barry Warsaw92971171997-01-03 00:14:25 +0000266
267
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000268void
269initsignal()
270{
Guido van Rossume4485b01994-09-07 14:32:49 +0000271 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000272 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000273
274#ifdef WITH_THREAD
275 main_thread = get_thread_ident();
276 main_pid = getpid();
277#endif
278
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000279 /* Create the module and add the functions */
Barry Warsaw92971171997-01-03 00:14:25 +0000280 m = Py_InitModule("signal", signal_methods);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000281
282 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000283 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000284
Barry Warsaw92971171997-01-03 00:14:25 +0000285 x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
286 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
287 goto finally;
288 Py_DECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000289
Barry Warsaw92971171997-01-03 00:14:25 +0000290 x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
291 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
292 goto finally;
293
294 x = PyInt_FromLong((long)NSIG);
295 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
296 goto finally;
297
298 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
299 if (!x)
300 goto finally;
301
302 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000303 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000304 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000305#ifdef HAVE_SIGACTION
306 struct sigaction act;
307 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000308 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000309#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000310 t = signal(i, SIG_IGN);
311 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000312#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000313 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000314 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000315 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000316 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000317 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000318 else
Barry Warsaw92971171997-01-03 00:14:25 +0000319 Handlers[i].func = Py_None; /* None of our business */
320 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000321 }
Barry Warsaw92971171997-01-03 00:14:25 +0000322 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000323 /* Install default int handler */
Barry Warsaw92971171997-01-03 00:14:25 +0000324 Py_DECREF(Handlers[SIGINT].func);
325 Handlers[SIGINT].func = IntHandler;
326 Py_INCREF(IntHandler);
327 signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000328 }
329
330#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000331 x = PyInt_FromLong(SIGHUP);
332 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000333 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000334#endif
335#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000336 x = PyInt_FromLong(SIGINT);
337 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000338 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000339#endif
340#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000341 x = PyInt_FromLong(SIGQUIT);
342 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000343 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000344#endif
345#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000346 x = PyInt_FromLong(SIGILL);
347 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000348 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000349#endif
350#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000351 x = PyInt_FromLong(SIGTRAP);
352 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000353 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000354#endif
355#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000356 x = PyInt_FromLong(SIGIOT);
357 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000358 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000359#endif
360#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000361 x = PyInt_FromLong(SIGABRT);
362 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000363 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364#endif
365#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000366 x = PyInt_FromLong(SIGEMT);
367 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000368 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000369#endif
370#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000371 x = PyInt_FromLong(SIGFPE);
372 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000373 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000374#endif
375#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000376 x = PyInt_FromLong(SIGKILL);
377 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000378 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000379#endif
380#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000381 x = PyInt_FromLong(SIGBUS);
382 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000383 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000384#endif
385#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000386 x = PyInt_FromLong(SIGSEGV);
387 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000388 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000389#endif
390#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000391 x = PyInt_FromLong(SIGSYS);
392 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000393 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000394#endif
395#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000396 x = PyInt_FromLong(SIGPIPE);
397 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000398 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000399#endif
400#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000401 x = PyInt_FromLong(SIGALRM);
402 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000403 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000404#endif
405#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000406 x = PyInt_FromLong(SIGTERM);
407 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000408 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000409#endif
410#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000411 x = PyInt_FromLong(SIGUSR1);
412 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000413 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000414#endif
415#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000416 x = PyInt_FromLong(SIGUSR2);
417 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000418 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000419#endif
420#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000421 x = PyInt_FromLong(SIGCLD);
422 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000423 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000424#endif
425#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000426 x = PyInt_FromLong(SIGCHLD);
427 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000428 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000429#endif
430#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000431 x = PyInt_FromLong(SIGPWR);
432 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000433 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000434#endif
435#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000436 x = PyInt_FromLong(SIGIO);
437 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000438 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000439#endif
440#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000441 x = PyInt_FromLong(SIGURG);
442 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000443 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000444#endif
445#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000446 x = PyInt_FromLong(SIGWINCH);
447 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000448 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000449#endif
450#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000451 x = PyInt_FromLong(SIGPOLL);
452 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000453 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000454#endif
455#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000456 x = PyInt_FromLong(SIGSTOP);
457 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000458 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000459#endif
460#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000461 x = PyInt_FromLong(SIGTSTP);
462 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000463 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000464#endif
465#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000466 x = PyInt_FromLong(SIGCONT);
467 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000468 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000469#endif
470#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000471 x = PyInt_FromLong(SIGTTIN);
472 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000473 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000474#endif
475#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000476 x = PyInt_FromLong(SIGTTOU);
477 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000478 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000479#endif
480#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000481 x = PyInt_FromLong(SIGVTALRM);
482 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000483 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000484#endif
485#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000486 x = PyInt_FromLong(SIGPROF);
487 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000488 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000489#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000490#ifdef SIGXCPU
491 x = PyInt_FromLong(SIGXCPU);
492 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000493 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000494#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000495#ifdef SIGXFSZ
496 x = PyInt_FromLong(SIGXFSZ);
497 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000498 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000499#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000500 if (!PyErr_Occurred())
501 return;
502
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000504 finally:
505 Py_FatalError("can't initialize module signal");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000506}
507
Barry Warsaw92971171997-01-03 00:14:25 +0000508
509
510/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511int
Guido van Rossumec25b911995-01-22 00:46:57 +0000512PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513{
514 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000515 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000516
517 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000518 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000519#ifdef WITH_THREAD
520 if (get_thread_ident() != main_thread)
521 return 0;
522#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000523 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000524 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000525
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000526 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000527 if (Handlers[i].tripped) {
528 PyObject *result = NULL;
529 PyObject *arglist = Py_BuildValue("(iO)", i, f);
530 Handlers[i].tripped = 0;
531
532 if (arglist) {
533 result = PyEval_CallObject(Handlers[i].func,
534 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000535 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000536 }
Barry Warsaw92971171997-01-03 00:14:25 +0000537 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000538 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000539
540 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000541 }
542 }
Barry Warsaw92971171997-01-03 00:14:25 +0000543 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000544 return 0;
545}
546
Barry Warsaw92971171997-01-03 00:14:25 +0000547
548/* Replacements for intrcheck.c functionality
549 * Declared in pyerrors.h
550 */
551void
552PyErr_SetInterrupt()
553{
554 is_tripped++;
555 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000556 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000557}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000558
559void
Barry Warsaw92971171997-01-03 00:14:25 +0000560PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000561{
562 initsignal();
563}
564
565int
Barry Warsaw92971171997-01-03 00:14:25 +0000566PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000567{
Barry Warsaw92971171997-01-03 00:14:25 +0000568 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000569#ifdef WITH_THREAD
570 if (get_thread_ident() != main_thread)
571 return 0;
572#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000573 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000574 return 1;
575 }
576 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000577}