blob: 89cab8973c4c99f2fe3411ae11d0a1ccdc378cd5 [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
Barry Warsaw92971171997-01-03 00:14:25 +0000107
108
Guido van Rossume4485b01994-09-07 14:32:49 +0000109static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000110signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +0000111 PyObject *self;
112 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000113{
Guido van Rossume4485b01994-09-07 14:32:49 +0000114 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000115 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000116}
117
Barry Warsaw92971171997-01-03 00:14:25 +0000118
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000119static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000120signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000121 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000122{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000123#ifdef WITH_THREAD
124 /* See NOTES section above */
125 if (getpid() == main_pid) {
126#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000127 is_tripped++;
128 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000129 Py_AddPendingCall(
130 (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000131#ifdef WITH_THREAD
132 }
133#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000134#ifdef SIGCHLD
135 if (sig_num == SIGCHLD) {
136 /* To avoid infinite recursion, this signal remains
137 reset until explicit re-instated.
138 Don't clear the 'func' field as it is our pointer
139 to the Python handler... */
140 return;
141 }
142#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000143 (void *)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000144}
Guido van Rossume4485b01994-09-07 14:32:49 +0000145
Guido van Rossum06d511d1995-03-10 15:13:48 +0000146
Barry Warsaw92971171997-01-03 00:14:25 +0000147
Guido van Rossum06d511d1995-03-10 15:13:48 +0000148#ifndef DONT_HAVE_SIG_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000149static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000150signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000151 PyObject *self; /* Not used */
152 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153{
154 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000155 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000156 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000157 /* alarm() returns the number of seconds remaining */
158 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000159}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000160#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161
Guido van Rossum06d511d1995-03-10 15:13:48 +0000162#ifndef DONT_HAVE_SIG_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000163static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000164signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000165 PyObject *self; /* Not used */
166 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000167{
Guido van Rossume4485b01994-09-07 14:32:49 +0000168 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000169 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000170
Guido van Rossuma597dde1995-01-10 20:56:29 +0000171 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000172 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000173 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000174 /* make sure that any exceptions that got raised are propagated
175 * back into Python
176 */
177 if (PyErr_CheckSignals())
178 return NULL;
179
Guido van Rossume4485b01994-09-07 14:32:49 +0000180 Py_INCREF(Py_None);
181 return Py_None;
182}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000183#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000184
Barry Warsaw92971171997-01-03 00:14:25 +0000185
Guido van Rossume4485b01994-09-07 14:32:49 +0000186static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000187signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000188 PyObject *self; /* Not used */
189 PyObject *args;
190{
191 PyObject *obj;
192 int sig_num;
193 PyObject *old_handler;
194 RETSIGTYPE (*func)();
195 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000196 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000197#ifdef WITH_THREAD
198 if (get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000199 PyErr_SetString(PyExc_ValueError,
200 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000201 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000202 }
203#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000205 PyErr_SetString(PyExc_ValueError,
206 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000207 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000208 }
Barry Warsaw92971171997-01-03 00:14:25 +0000209 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000211 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000212 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000213 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000214 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000216 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217 }
218 else
Barry Warsaw92971171997-01-03 00:14:25 +0000219 func = signal_handler;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000220 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000221 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000222 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000223 }
Barry Warsaw92971171997-01-03 00:14:25 +0000224 old_handler = Handlers[sig_num].func;
225 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000226 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000227 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000228 return old_handler;
229}
230
Barry Warsaw92971171997-01-03 00:14:25 +0000231
Guido van Rossume4485b01994-09-07 14:32:49 +0000232static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000233signal_get_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000234 PyObject *self; /* Not used */
235 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000236{
237 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000238 PyObject *old_handler;
239 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000240 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000241 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000242 PyErr_SetString(PyExc_ValueError,
243 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000244 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000245 }
Barry Warsaw92971171997-01-03 00:14:25 +0000246 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000247 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000248 return old_handler;
249}
250
251
Barry Warsaw92971171997-01-03 00:14:25 +0000252
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000253/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000254static PyMethodDef signal_methods[] = {
Guido van Rossum06d511d1995-03-10 15:13:48 +0000255#ifndef DONT_HAVE_SIG_ALARM
Barry Warsaw92971171997-01-03 00:14:25 +0000256 {"alarm", signal_alarm},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000257#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000258 {"signal", signal_signal},
259 {"getsignal", signal_get_signal},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000260#ifndef DONT_HAVE_SIG_PAUSE
Barry Warsaw92971171997-01-03 00:14:25 +0000261 {"pause", signal_pause},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000262#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000263 {"default_int_handler", signal_default_int_handler},
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000264 {NULL, NULL} /* sentinel */
265};
266
Barry Warsaw92971171997-01-03 00:14:25 +0000267
268
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000269void
270initsignal()
271{
Guido van Rossume4485b01994-09-07 14:32:49 +0000272 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000273 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000274
275#ifdef WITH_THREAD
276 main_thread = get_thread_ident();
277 main_pid = getpid();
278#endif
279
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000280 /* Create the module and add the functions */
Barry Warsaw92971171997-01-03 00:14:25 +0000281 m = Py_InitModule("signal", signal_methods);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000282
283 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000284 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000285
Barry Warsaw92971171997-01-03 00:14:25 +0000286 x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
287 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
288 goto finally;
289 Py_DECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000290
Barry Warsaw92971171997-01-03 00:14:25 +0000291 x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
292 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
293 goto finally;
294
295 x = PyInt_FromLong((long)NSIG);
296 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
297 goto finally;
298
299 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
300 if (!x)
301 goto finally;
302
303 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000304 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000305 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000306#ifdef HAVE_SIGACTION
307 struct sigaction act;
308 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000309 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000310#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000311 t = signal(i, SIG_IGN);
312 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000313#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000314 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000315 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000316 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000317 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000318 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000319 else
Barry Warsaw92971171997-01-03 00:14:25 +0000320 Handlers[i].func = Py_None; /* None of our business */
321 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322 }
Barry Warsaw92971171997-01-03 00:14:25 +0000323 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000324 /* Install default int handler */
Barry Warsaw92971171997-01-03 00:14:25 +0000325 Py_DECREF(Handlers[SIGINT].func);
326 Handlers[SIGINT].func = IntHandler;
327 Py_INCREF(IntHandler);
328 signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000329 }
330
331#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000332 x = PyInt_FromLong(SIGHUP);
333 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000334 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000335#endif
336#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000337 x = PyInt_FromLong(SIGINT);
338 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000339 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340#endif
341#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000342 x = PyInt_FromLong(SIGQUIT);
343 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000344 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000345#endif
346#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000347 x = PyInt_FromLong(SIGILL);
348 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000349 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000350#endif
351#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000352 x = PyInt_FromLong(SIGTRAP);
353 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000354 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000355#endif
356#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000357 x = PyInt_FromLong(SIGIOT);
358 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000359 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000360#endif
361#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000362 x = PyInt_FromLong(SIGABRT);
363 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000364 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000365#endif
366#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000367 x = PyInt_FromLong(SIGEMT);
368 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000369 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000370#endif
371#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000372 x = PyInt_FromLong(SIGFPE);
373 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000374 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000375#endif
376#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000377 x = PyInt_FromLong(SIGKILL);
378 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000379 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000380#endif
381#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000382 x = PyInt_FromLong(SIGBUS);
383 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000384 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000385#endif
386#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000387 x = PyInt_FromLong(SIGSEGV);
388 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000389 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000390#endif
391#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000392 x = PyInt_FromLong(SIGSYS);
393 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000394 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000395#endif
396#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000397 x = PyInt_FromLong(SIGPIPE);
398 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000399 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000400#endif
401#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000402 x = PyInt_FromLong(SIGALRM);
403 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000404 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000405#endif
406#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000407 x = PyInt_FromLong(SIGTERM);
408 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000409 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000410#endif
411#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000412 x = PyInt_FromLong(SIGUSR1);
413 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000414 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000415#endif
416#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000417 x = PyInt_FromLong(SIGUSR2);
418 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000419 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000420#endif
421#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000422 x = PyInt_FromLong(SIGCLD);
423 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000424 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000425#endif
426#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000427 x = PyInt_FromLong(SIGCHLD);
428 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000429 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000430#endif
431#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000432 x = PyInt_FromLong(SIGPWR);
433 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000434 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000435#endif
436#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000437 x = PyInt_FromLong(SIGIO);
438 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000439 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000440#endif
441#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000442 x = PyInt_FromLong(SIGURG);
443 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000444 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000445#endif
446#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000447 x = PyInt_FromLong(SIGWINCH);
448 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000449 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000450#endif
451#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000452 x = PyInt_FromLong(SIGPOLL);
453 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000454 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000455#endif
456#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000457 x = PyInt_FromLong(SIGSTOP);
458 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000459 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000460#endif
461#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000462 x = PyInt_FromLong(SIGTSTP);
463 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000464 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000465#endif
466#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000467 x = PyInt_FromLong(SIGCONT);
468 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000469 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000470#endif
471#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000472 x = PyInt_FromLong(SIGTTIN);
473 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000474 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000475#endif
476#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000477 x = PyInt_FromLong(SIGTTOU);
478 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000479 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000480#endif
481#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000482 x = PyInt_FromLong(SIGVTALRM);
483 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000484 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000485#endif
486#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000487 x = PyInt_FromLong(SIGPROF);
488 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000489 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000490#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000491#ifdef SIGXCPU
492 x = PyInt_FromLong(SIGXCPU);
493 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000494 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000495#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000496#ifdef SIGXFSZ
497 x = PyInt_FromLong(SIGXFSZ);
498 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000499 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000500#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000501 if (!PyErr_Occurred())
502 return;
503
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000504 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000505 finally:
506 Py_FatalError("can't initialize module signal");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000507}
508
Barry Warsaw92971171997-01-03 00:14:25 +0000509
510
511/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000512int
Guido van Rossumec25b911995-01-22 00:46:57 +0000513PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000514{
515 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000516 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000517
518 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000519 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000520#ifdef WITH_THREAD
521 if (get_thread_ident() != main_thread)
522 return 0;
523#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000524 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000525 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000526
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000527 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000528 if (Handlers[i].tripped) {
529 PyObject *result = NULL;
530 PyObject *arglist = Py_BuildValue("(iO)", i, f);
531 Handlers[i].tripped = 0;
532
533 if (arglist) {
534 result = PyEval_CallObject(Handlers[i].func,
535 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000536 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537 }
Barry Warsaw92971171997-01-03 00:14:25 +0000538 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000539 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000540
541 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000542 }
543 }
Barry Warsaw92971171997-01-03 00:14:25 +0000544 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000545 return 0;
546}
547
Barry Warsaw92971171997-01-03 00:14:25 +0000548
549/* Replacements for intrcheck.c functionality
550 * Declared in pyerrors.h
551 */
552void
553PyErr_SetInterrupt()
554{
555 is_tripped++;
556 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000557 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000558}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000559
560void
Barry Warsaw92971171997-01-03 00:14:25 +0000561PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000562{
563 initsignal();
564}
565
566int
Barry Warsaw92971171997-01-03 00:14:25 +0000567PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000568{
Barry Warsaw92971171997-01-03 00:14:25 +0000569 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000570#ifdef WITH_THREAD
571 if (get_thread_ident() != main_thread)
572 return 0;
573#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000574 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000575 return 1;
576 }
577 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000578}