blob: fe794478e1ff4b3948048e966eb389a8e1d41129 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000025/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000026
Guido van Rossum602099a1994-09-14 13:32:22 +000027#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +000028#include "intrcheck.h"
29
30#include <signal.h>
31
Guido van Rossumbb4ba121994-06-23 11:25:45 +000032#ifndef SIG_ERR
33#define SIG_ERR ((RETSIGTYPE (*)())-1)
34#endif
35
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000036#ifndef NSIG
37#define NSIG (_SIGMAX + 1) /* For QNX */
38#endif
39
40
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041/*
42 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
43
44 When threads are supported, we want the following semantics:
45
46 - only the main thread can set a signal handler
47 - any thread can get a signal handler
48 - signals are only delivered to the main thread
49
50 I.e. we don't support "synchronous signals" like SIGFPE (catching
51 this doesn't make much sense in Python anyway) nor do we support
52 signals as a means of inter-thread communication, since not all
53 thread implementations support that (at least our thread library
54 doesn't).
55
56 We still have the problem that in some implementations signals
57 generated by the keyboard (e.g. SIGINT) are delivered to all
58 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
59 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000060 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000061 a working implementation that works in all three cases -- the
62 handler ignores signals if getpid() isn't the same as in the main
63 thread. XXX This is a hack.
64
65*/
66
67#ifdef WITH_THREAD
68#include "thread.h"
69static long main_thread;
70static pid_t main_pid;
71#endif
72
Guido van Rossume4485b01994-09-07 14:32:49 +000073struct PySignal_SignalArrayStruct {
Guido van Rossum398d9fe1994-05-11 08:59:13 +000074 int tripped;
Guido van Rossume4485b01994-09-07 14:32:49 +000075 PyObject *func;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000076};
77
Guido van Rossume4485b01994-09-07 14:32:49 +000078static struct PySignal_SignalArrayStruct PySignal_SignalHandlerArray[NSIG];
79static int PySignal_IsTripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000080
Guido van Rossume4485b01994-09-07 14:32:49 +000081static PyObject *PySignal_SignalDefaultHandler;
82static PyObject *PySignal_SignalIgnoreHandler;
83static PyObject *PySignal_DefaultIntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000084
Guido van Rossume4485b01994-09-07 14:32:49 +000085static PyObject *
86PySignal_CDefaultIntHandler(self, arg)
87 PyObject *self;
88 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000089{
Guido van Rossume4485b01994-09-07 14:32:49 +000090 PyErr_SetNone(PyExc_KeyboardInterrupt);
91 return (PyObject *)NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000092}
93
Guido van Rossum06d511d1995-03-10 15:13:48 +000094void
95PyErr_SetInterrupt()
96{
97 PySignal_IsTripped++;
98 PySignal_SignalHandlerArray[SIGINT].tripped = 1;
99}
100
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000101static RETSIGTYPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000102PySignal_Handler(sig_num)
103 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000104{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000105#ifdef WITH_THREAD
106 /* See NOTES section above */
107 if (getpid() == main_pid) {
108#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000109 PySignal_IsTripped++;
110 PySignal_SignalHandlerArray[sig_num].tripped = 1;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000111#ifdef WITH_THREAD
112 }
113#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000114#ifdef SIGCHLD
115 if (sig_num == SIGCHLD) {
116 /* To avoid infinite recursion, this signal remains
117 reset until explicit re-instated.
118 Don't clear the 'func' field as it is our pointer
119 to the Python handler... */
120 return;
121 }
122#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000123 (void *)signal(sig_num, &PySignal_Handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000124}
Guido van Rossume4485b01994-09-07 14:32:49 +0000125
Guido van Rossum06d511d1995-03-10 15:13:48 +0000126
127#ifndef DONT_HAVE_SIG_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000128static PyObject *
129PySignal_Alarm(self, args)
130 PyObject *self; /* Not used */
131 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132{
133 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000134 if (!PyArg_Parse(args, "i", &t))
135 return (PyObject *)NULL;
136 /* alarm() returns the number of seconds remaining */
137 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000138}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000139#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140
Guido van Rossum06d511d1995-03-10 15:13:48 +0000141#ifndef DONT_HAVE_SIG_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000142static PyObject *
Guido van Rossume4485b01994-09-07 14:32:49 +0000143PySignal_Pause(self, args)
144 PyObject *self; /* Not used */
145 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000146{
Guido van Rossume4485b01994-09-07 14:32:49 +0000147 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000148 return NULL;
Guido van Rossuma597dde1995-01-10 20:56:29 +0000149 Py_BEGIN_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000150 pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000151 Py_END_ALLOW_THREADS
Guido van Rossume4485b01994-09-07 14:32:49 +0000152 Py_INCREF(Py_None);
153 return Py_None;
154}
Guido van Rossum06d511d1995-03-10 15:13:48 +0000155#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000156
157static PyObject *
158PySignal_Signal(self, args)
159 PyObject *self; /* Not used */
160 PyObject *args;
161{
162 PyObject *obj;
163 int sig_num;
164 PyObject *old_handler;
165 RETSIGTYPE (*func)();
166 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
167 return (PyObject *)NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000168#ifdef WITH_THREAD
169 if (get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000170 PyErr_SetString(PyExc_ValueError,
171 "signal only works in main thread");
Guido van Rossume4485b01994-09-07 14:32:49 +0000172 return (PyObject *)NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000173 }
174#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000175 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000176 PyErr_SetString(PyExc_ValueError,
177 "signal number out of range");
Guido van Rossume4485b01994-09-07 14:32:49 +0000178 return (PyObject *)NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000179 }
Guido van Rossume4485b01994-09-07 14:32:49 +0000180 if (obj == PySignal_SignalIgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000181 func = SIG_IGN;
Guido van Rossume4485b01994-09-07 14:32:49 +0000182 else if (obj == PySignal_SignalDefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000183 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000184 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000185 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000186"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Guido van Rossume4485b01994-09-07 14:32:49 +0000187 return (PyObject *)NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000188 }
189 else
Guido van Rossume4485b01994-09-07 14:32:49 +0000190 func = PySignal_Handler;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000191 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000192 PyErr_SetFromErrno(PyExc_RuntimeError);
193 return (PyObject *)NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000194 }
Guido van Rossume4485b01994-09-07 14:32:49 +0000195 old_handler = PySignal_SignalHandlerArray[sig_num].func;
196 PySignal_SignalHandlerArray[sig_num].tripped = 0;
197 Py_INCREF(obj);
198 PySignal_SignalHandlerArray[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000199 return old_handler;
200}
201
Guido van Rossume4485b01994-09-07 14:32:49 +0000202static PyObject *
203PySignal_GetSignal(self, args)
204 PyObject *self; /* Not used */
205 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000206{
207 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000208 PyObject *old_handler;
209 if (!PyArg_Parse(args, "i", &sig_num))
210 return (PyObject *)NULL;
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");
Guido van Rossume4485b01994-09-07 14:32:49 +0000214 return (PyObject *)NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215 }
Guido van Rossume4485b01994-09-07 14:32:49 +0000216 old_handler = PySignal_SignalHandlerArray[sig_num].func;
217 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000218 return old_handler;
219}
220
221
222/* List of functions defined in the module */
223
Guido van Rossume4485b01994-09-07 14:32:49 +0000224static PyMethodDef PySignal_methods[] = {
Guido van Rossum06d511d1995-03-10 15:13:48 +0000225#ifndef DONT_HAVE_SIG_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000226 {"alarm", PySignal_Alarm},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000227#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000228 {"signal", PySignal_Signal},
229 {"getsignal", PySignal_GetSignal},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000230#ifndef DONT_HAVE_SIG_PAUSE
Guido van Rossume4485b01994-09-07 14:32:49 +0000231 {"pause", PySignal_Pause},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000232#endif
Guido van Rossum95588251995-01-07 11:50:04 +0000233 {"default_int_handler", PySignal_CDefaultIntHandler},
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000234 {NULL, NULL} /* sentinel */
235};
236
237void
238initsignal()
239{
Guido van Rossume4485b01994-09-07 14:32:49 +0000240 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000241 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000242
243#ifdef WITH_THREAD
244 main_thread = get_thread_ident();
245 main_pid = getpid();
246#endif
247
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000248 /* Create the module and add the functions */
Guido van Rossume4485b01994-09-07 14:32:49 +0000249 m = Py_InitModule("signal", PySignal_methods);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000250
251 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000252 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000253
Guido van Rossume4485b01994-09-07 14:32:49 +0000254 PySignal_SignalDefaultHandler = PyInt_FromLong((long)SIG_DFL);
255 PyDict_SetItemString(d, "SIG_DFL", PySignal_SignalDefaultHandler);
256 PySignal_SignalIgnoreHandler = PyInt_FromLong((long)SIG_IGN);
257 PyDict_SetItemString(d, "SIG_IGN", PySignal_SignalIgnoreHandler);
258 PyDict_SetItemString(d, "NSIG", PyInt_FromLong((long)NSIG));
Guido van Rossum95588251995-01-07 11:50:04 +0000259 PySignal_DefaultIntHandler =
260 PyDict_GetItemString(d, "default_int_handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000261
Guido van Rossume4485b01994-09-07 14:32:49 +0000262 PySignal_SignalHandlerArray[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000263 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000264 RETSIGTYPE (*t)();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000265 t = signal(i, SIG_IGN);
266 signal(i, t);
Guido van Rossume4485b01994-09-07 14:32:49 +0000267 PySignal_SignalHandlerArray[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000268 if (t == SIG_DFL)
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000269 PySignal_SignalHandlerArray[i].func =
270 PySignal_SignalDefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000271 else if (t == SIG_IGN)
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000272 PySignal_SignalHandlerArray[i].func =
273 PySignal_SignalIgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000274 else
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000275 PySignal_SignalHandlerArray[i].func =
276 Py_None; /* None of our business */
Guido van Rossume4485b01994-09-07 14:32:49 +0000277 Py_INCREF(PySignal_SignalHandlerArray[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000278 }
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000279 if (PySignal_SignalHandlerArray[SIGINT].func ==
280 PySignal_SignalDefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000281 /* Install default int handler */
Guido van Rossume4485b01994-09-07 14:32:49 +0000282 Py_DECREF(PySignal_SignalHandlerArray[SIGINT].func);
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000283 PySignal_SignalHandlerArray[SIGINT].func =
284 PySignal_DefaultIntHandler;
Guido van Rossume4485b01994-09-07 14:32:49 +0000285 Py_INCREF(PySignal_DefaultIntHandler);
286 signal(SIGINT, &PySignal_Handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000287 }
288
289#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000290 x = PyInt_FromLong(SIGHUP);
291 PyDict_SetItemString(d, "SIGHUP", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000292#endif
293#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000294 x = PyInt_FromLong(SIGINT);
295 PyDict_SetItemString(d, "SIGINT", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000296#endif
297#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000298 x = PyInt_FromLong(SIGQUIT);
299 PyDict_SetItemString(d, "SIGQUIT", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000300#endif
301#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000302 x = PyInt_FromLong(SIGILL);
303 PyDict_SetItemString(d, "SIGILL", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000304#endif
305#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000306 x = PyInt_FromLong(SIGTRAP);
307 PyDict_SetItemString(d, "SIGTRAP", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000308#endif
309#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000310 x = PyInt_FromLong(SIGIOT);
311 PyDict_SetItemString(d, "SIGIOT", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000312#endif
313#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000314 x = PyInt_FromLong(SIGABRT);
315 PyDict_SetItemString(d, "SIGABRT", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000316#endif
317#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000318 x = PyInt_FromLong(SIGEMT);
319 PyDict_SetItemString(d, "SIGEMT", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000320#endif
321#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000322 x = PyInt_FromLong(SIGFPE);
323 PyDict_SetItemString(d, "SIGFPE", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000324#endif
325#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000326 x = PyInt_FromLong(SIGKILL);
327 PyDict_SetItemString(d, "SIGKILL", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000328#endif
329#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000330 x = PyInt_FromLong(SIGBUS);
331 PyDict_SetItemString(d, "SIGBUS", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332#endif
333#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000334 x = PyInt_FromLong(SIGSEGV);
335 PyDict_SetItemString(d, "SIGSEGV", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000336#endif
337#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000338 x = PyInt_FromLong(SIGSYS);
339 PyDict_SetItemString(d, "SIGSYS", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340#endif
341#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000342 x = PyInt_FromLong(SIGPIPE);
343 PyDict_SetItemString(d, "SIGPIPE", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000344#endif
345#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000346 x = PyInt_FromLong(SIGALRM);
347 PyDict_SetItemString(d, "SIGALRM", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348#endif
349#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000350 x = PyInt_FromLong(SIGTERM);
351 PyDict_SetItemString(d, "SIGTERM", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000352#endif
353#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000354 x = PyInt_FromLong(SIGUSR1);
355 PyDict_SetItemString(d, "SIGUSR1", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000356#endif
357#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000358 x = PyInt_FromLong(SIGUSR2);
359 PyDict_SetItemString(d, "SIGUSR2", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000360#endif
361#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000362 x = PyInt_FromLong(SIGCLD);
363 PyDict_SetItemString(d, "SIGCLD", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364#endif
365#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000366 x = PyInt_FromLong(SIGCHLD);
367 PyDict_SetItemString(d, "SIGCHLD", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368#endif
369#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000370 x = PyInt_FromLong(SIGPWR);
371 PyDict_SetItemString(d, "SIGPWR", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372#endif
373#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000374 x = PyInt_FromLong(SIGIO);
375 PyDict_SetItemString(d, "SIGIO", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000376#endif
377#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000378 x = PyInt_FromLong(SIGURG);
379 PyDict_SetItemString(d, "SIGURG", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000380#endif
381#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000382 x = PyInt_FromLong(SIGWINCH);
383 PyDict_SetItemString(d, "SIGWINCH", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000384#endif
385#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000386 x = PyInt_FromLong(SIGPOLL);
387 PyDict_SetItemString(d, "SIGPOLL", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000388#endif
389#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000390 x = PyInt_FromLong(SIGSTOP);
391 PyDict_SetItemString(d, "SIGSTOP", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392#endif
393#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000394 x = PyInt_FromLong(SIGTSTP);
395 PyDict_SetItemString(d, "SIGTSTP", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000396#endif
397#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000398 x = PyInt_FromLong(SIGCONT);
399 PyDict_SetItemString(d, "SIGCONT", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000400#endif
401#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000402 x = PyInt_FromLong(SIGTTIN);
403 PyDict_SetItemString(d, "SIGTTIN", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000404#endif
405#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000406 x = PyInt_FromLong(SIGTTOU);
407 PyDict_SetItemString(d, "SIGTTOU", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000408#endif
409#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000410 x = PyInt_FromLong(SIGVTALRM);
411 PyDict_SetItemString(d, "SIGVTALRM", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000412#endif
413#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000414 x = PyInt_FromLong(SIGPROF);
415 PyDict_SetItemString(d, "SIGPROF", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000416#endif
417#ifdef SIGCPU
Guido van Rossume4485b01994-09-07 14:32:49 +0000418 x = PyInt_FromLong(SIGCPU);
419 PyDict_SetItemString(d, "SIGCPU", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000420#endif
421#ifdef SIGFSZ
Guido van Rossume4485b01994-09-07 14:32:49 +0000422 x = PyInt_FromLong(SIGFSZ);
423 PyDict_SetItemString(d, "SIGFSZ", x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000424#endif
425 /* Check for errors */
Guido van Rossume4485b01994-09-07 14:32:49 +0000426 if (PyErr_Occurred())
427 Py_FatalError("can't initialize module signal");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000428}
429
430int
Guido van Rossumec25b911995-01-22 00:46:57 +0000431PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000432{
433 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000434 PyObject *f;
435 if (!PySignal_IsTripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000436 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000437#ifdef WITH_THREAD
438 if (get_thread_ident() != main_thread)
439 return 0;
440#endif
Guido van Rossuma597dde1995-01-10 20:56:29 +0000441 f = PyEval_GetFrame();
Guido van Rossume4485b01994-09-07 14:32:49 +0000442 if (f == (PyObject *)NULL)
443 f = Py_None;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000444 for (i = 1; i < NSIG; i++) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000445 if (PySignal_SignalHandlerArray[i].tripped) {
446 PyObject *arglist, *result;
447 PySignal_SignalHandlerArray[i].tripped = 0;
448 arglist = Py_BuildValue("(iO)", i, f);
449 if (arglist == (PyObject *)NULL)
450 result = (PyObject *)NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000451 else {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000452 result = PyEval_CallObject(
453 PySignal_SignalHandlerArray[i].func, arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000454 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000455 }
Guido van Rossume4485b01994-09-07 14:32:49 +0000456 if (result == (PyObject *)NULL) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000457 return 1;
458 } else {
Guido van Rossume4485b01994-09-07 14:32:49 +0000459 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000460 }
461 }
462 }
Guido van Rossume4485b01994-09-07 14:32:49 +0000463 PySignal_IsTripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000464 return 0;
465}
466
467/* Replacement for intrcheck.c functionality */
468
469void
Guido van Rossuma597dde1995-01-10 20:56:29 +0000470PyOS_InitInterrupts ()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000471{
472 initsignal();
473}
474
475int
Guido van Rossuma597dde1995-01-10 20:56:29 +0000476PyOS_InterruptOccurred ()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477{
Guido van Rossume4485b01994-09-07 14:32:49 +0000478 if (PySignal_SignalHandlerArray[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000479#ifdef WITH_THREAD
480 if (get_thread_ident() != main_thread)
481 return 0;
482#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000483 PySignal_SignalHandlerArray[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000484 return 1;
485 }
486 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000487}