blob: 9dec24f85e43c8a8081b5c9d15ff1e3bcfe18fd9 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +00007#include "intrcheck.h"
8
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009#ifdef MS_WINDOWS
Guido van Rossum644a12b1997-04-09 19:24:53 +000010#include <process.h>
11#endif
12
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013#include <signal.h>
14
Guido van Rossum02de8972007-12-19 19:41:06 +000015#include <sys/stat.h>
16
Guido van Rossumbb4ba121994-06-23 11:25:45 +000017#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000018#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000019#endif
20
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000021#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000022#define NSIG 12
23#include <process.h>
24#endif
25
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000026#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000027# if defined(_NSIG)
28# define NSIG _NSIG /* For BSD/SysV */
29# elif defined(_SIGMAX)
30# define NSIG (_SIGMAX + 1) /* For QNX */
31# elif defined(SIGMAX)
32# define NSIG (SIGMAX + 1) /* For djgpp */
33# else
34# define NSIG 64 /* Use a reasonable default value */
35# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000036#endif
37
38
Guido van Rossumbb4ba121994-06-23 11:25:45 +000039/*
40 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
41
42 When threads are supported, we want the following semantics:
43
44 - only the main thread can set a signal handler
45 - any thread can get a signal handler
46 - signals are only delivered to the main thread
47
48 I.e. we don't support "synchronous signals" like SIGFPE (catching
49 this doesn't make much sense in Python anyway) nor do we support
50 signals as a means of inter-thread communication, since not all
51 thread implementations support that (at least our thread library
52 doesn't).
53
54 We still have the problem that in some implementations signals
55 generated by the keyboard (e.g. SIGINT) are delivered to all
56 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
57 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000058 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000059 a working implementation that works in all three cases -- the
60 handler ignores signals if getpid() isn't the same as in the main
61 thread. XXX This is a hack.
62
Guido van Rossum9e8181b2000-09-19 00:46:46 +000063 GNU pth is a user-space threading library, and as such, all threads
64 run within the same process. In this case, if the currently running
65 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000066*/
67
68#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000069#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000070#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071static long main_thread;
72static pid_t main_pid;
73#endif
74
Barry Warsaw92971171997-01-03 00:14:25 +000075static struct {
76 int tripped;
77 PyObject *func;
78} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000079
Guido van Rossum02de8972007-12-19 19:41:06 +000080static sig_atomic_t wakeup_fd = -1;
81
Guido van Rossum137c49c2007-12-10 23:00:12 +000082/* Speed up sigcheck() when none tripped */
83static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000084
Barry Warsaw92971171997-01-03 00:14:25 +000085static PyObject *DefaultHandler;
86static PyObject *IgnoreHandler;
87static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000088
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000089/* On Solaris 8, gcc will produce a warning that the function
90 declaration is not a prototype. This is caused by the definition of
91 SIG_DFL as (void (*)())0; the correct declaration would have been
92 (void (*)(int))0. */
93
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000094static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000095
Barry Warsaw92971171997-01-03 00:14:25 +000096
Guido van Rossume4485b01994-09-07 14:32:49 +000097static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000098signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099{
Guido van Rossume4485b01994-09-07 14:32:49 +0000100 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000101 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000102}
103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000105"default_int_handler(...)\n\
106\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000107The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000108It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000109
Thomas Wouters0796b002000-07-22 23:49:30 +0000110
111static int
112checksignals_witharg(void * unused)
113{
114 return PyErr_CheckSignals();
115}
116
Tim Peters4f1b2082000-07-23 21:18:09 +0000117static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000118signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000119{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000120#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000121#ifdef WITH_PTH
122 if (PyThread_get_thread_ident() != main_thread) {
123 pth_raise(*(pth_t *) main_thread, sig_num);
124 return;
125 }
126#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000127 /* See NOTES section above */
128 if (getpid() == main_pid) {
129#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000130 Handlers[sig_num].tripped = 1;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000131 /* Set is_tripped after setting .tripped, as it gets
132 cleared in PyErr_CheckSignals() before .tripped. */
133 is_tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000134 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum02de8972007-12-19 19:41:06 +0000135 if (wakeup_fd != -1)
136 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000137#ifdef WITH_THREAD
138 }
139#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000140#ifdef SIGCHLD
141 if (sig_num == SIGCHLD) {
142 /* To avoid infinite recursion, this signal remains
143 reset until explicit re-instated.
144 Don't clear the 'func' field as it is our pointer
145 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000146 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000147 }
148#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000149 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000150}
Guido van Rossume4485b01994-09-07 14:32:49 +0000151
Guido van Rossum06d511d1995-03-10 15:13:48 +0000152
Guido van Rossum1171ee61997-08-22 20:42:00 +0000153#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000154static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000155signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156{
157 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000158 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000159 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000160 /* alarm() returns the number of seconds remaining */
Fred Drakedff3a372001-07-19 21:29:49 +0000161 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000162}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000165"alarm(seconds)\n\
166\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000168#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169
Guido van Rossum1171ee61997-08-22 20:42:00 +0000170#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000171static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000172signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000173{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000174 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000175 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000176 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000177 /* make sure that any exceptions that got raised are propagated
178 * back into Python
179 */
180 if (PyErr_CheckSignals())
181 return NULL;
182
Guido van Rossume4485b01994-09-07 14:32:49 +0000183 Py_INCREF(Py_None);
184 return Py_None;
185}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000186PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000187"pause()\n\
188\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000189Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000190
Guido van Rossum06d511d1995-03-10 15:13:48 +0000191#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000192
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000193
Guido van Rossume4485b01994-09-07 14:32:49 +0000194static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000195signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000196{
197 PyObject *obj;
198 int sig_num;
199 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000200 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000201 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000202 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000203#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000204 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000205 PyErr_SetString(PyExc_ValueError,
206 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000207 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000208 }
209#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000211 PyErr_SetString(PyExc_ValueError,
212 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000213 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000214 }
Barry Warsaw92971171997-01-03 00:14:25 +0000215 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000216 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000217 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000218 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000219 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000220 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000221"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000222 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000223 }
224 else
Barry Warsaw92971171997-01-03 00:14:25 +0000225 func = signal_handler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000226 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000227 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000228 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000229 }
Barry Warsaw92971171997-01-03 00:14:25 +0000230 old_handler = Handlers[sig_num].func;
231 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000232 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000233 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000234 return old_handler;
235}
236
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000238"signal(sig, action) -> action\n\
239\n\
240Set the action for the given signal. The action can be SIG_DFL,\n\
241SIG_IGN, or a callable Python object. The previous action is\n\
242returned. See getsignal() for possible return values.\n\
243\n\
244*** IMPORTANT NOTICE ***\n\
245A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000246the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000247
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000248
Guido van Rossume4485b01994-09-07 14:32:49 +0000249static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000250signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000251{
252 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000253 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000254 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000255 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000256 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000257 PyErr_SetString(PyExc_ValueError,
258 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000259 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260 }
Barry Warsaw92971171997-01-03 00:14:25 +0000261 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000262 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000263 return old_handler;
264}
265
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000266PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000267"getsignal(sig) -> action\n\
268\n\
269Return the current action for the given signal. The return value can be:\n\
270SIG_IGN -- if the signal is being ignored\n\
271SIG_DFL -- if the default action for the signal is in effect\n\
272None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000273anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000274
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000275
Guido van Rossum02de8972007-12-19 19:41:06 +0000276static PyObject *
277signal_set_wakeup_fd(PyObject *self, PyObject *args)
278{
279 struct stat buf;
280 int fd, old_fd;
281 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
282 return NULL;
283#ifdef WITH_THREAD
284 if (PyThread_get_thread_ident() != main_thread) {
285 PyErr_SetString(PyExc_ValueError,
286 "set_wakeup_fd only works in main thread");
287 return NULL;
288 }
289#endif
290 if (fd != -1 && fstat(fd, &buf) != 0) {
291 PyErr_SetString(PyExc_ValueError, "invalid fd");
292 return NULL;
293 }
294 old_fd = wakeup_fd;
295 wakeup_fd = fd;
296 return PyLong_FromLong(old_fd);
297}
298
299PyDoc_STRVAR(set_wakeup_fd_doc,
300"set_wakeup_fd(fd) -> fd\n\
301\n\
302Sets the fd to be written to (with '\\0') when a signal\n\
303comes in. A library can use this to wakeup select or poll.\n\
304The previous fd is returned.\n\
305\n\
306The fd must be non-blocking.");
307
308/* C API for the same, without all the error checking */
309int
310PySignal_SetWakeupFd(int fd)
311{
312 int old_fd = wakeup_fd;
313 if (fd < 0)
314 fd = -1;
315 wakeup_fd = fd;
316 return old_fd;
317}
318
319
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000320/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000321static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000322#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000323 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000324#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000325 {"signal", signal_signal, METH_VARARGS, signal_doc},
326 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Guido van Rossum02de8972007-12-19 19:41:06 +0000327 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000328#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000329 {"pause", (PyCFunction)signal_pause,
330 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000331#endif
Guido van Rossum02de8972007-12-19 19:41:06 +0000332 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000333 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000334 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000335};
336
Barry Warsaw92971171997-01-03 00:14:25 +0000337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000338PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000339"This module provides mechanisms to use signal handlers in Python.\n\
340\n\
341Functions:\n\
342\n\
343alarm() -- cause SIGALRM after a specified time [Unix only]\n\
344signal() -- set the action for a given signal\n\
345getsignal() -- get the signal action for a given signal\n\
346pause() -- wait until a signal arrives [Unix only]\n\
347default_int_handler() -- default SIGINT handler\n\
348\n\
349Constants:\n\
350\n\
351SIG_DFL -- used to refer to the system default handler\n\
352SIG_IGN -- used to ignore the signal\n\
353NSIG -- number of defined signals\n\
354\n\
355SIGINT, SIGTERM, etc. -- signal numbers\n\
356\n\
357*** IMPORTANT NOTICE ***\n\
358A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000360
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000361PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000362initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363{
Guido van Rossume4485b01994-09-07 14:32:49 +0000364 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000365 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000366
367#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000368 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000369 main_pid = getpid();
370#endif
371
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000373 m = Py_InitModule3("signal", signal_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000374 if (m == NULL)
375 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000376
377 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000378 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000379
Guido van Rossum276fa432000-06-30 23:04:18 +0000380 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000381 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
382 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000383
Guido van Rossum276fa432000-06-30 23:04:18 +0000384 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000385 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
386 goto finally;
387
388 x = PyInt_FromLong((long)NSIG);
389 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
390 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000391 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000392
393 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
394 if (!x)
395 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000396 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000397
398 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000399 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000400 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000401 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000402 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000403 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000404 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000405 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000406 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000407 else
Barry Warsaw92971171997-01-03 00:14:25 +0000408 Handlers[i].func = Py_None; /* None of our business */
409 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000410 }
Barry Warsaw92971171997-01-03 00:14:25 +0000411 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000412 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000413 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000414 Py_DECREF(Handlers[SIGINT].func);
415 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000416 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000417 }
418
419#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000420 x = PyInt_FromLong(SIGHUP);
421 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000422 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000423#endif
424#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000425 x = PyInt_FromLong(SIGINT);
426 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000427 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000428#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000429#ifdef SIGBREAK
430 x = PyInt_FromLong(SIGBREAK);
431 PyDict_SetItemString(d, "SIGBREAK", x);
432 Py_XDECREF(x);
433#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000434#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000435 x = PyInt_FromLong(SIGQUIT);
436 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000437 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000438#endif
439#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000440 x = PyInt_FromLong(SIGILL);
441 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000442 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443#endif
444#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000445 x = PyInt_FromLong(SIGTRAP);
446 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000447 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000448#endif
449#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000450 x = PyInt_FromLong(SIGIOT);
451 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000452 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000453#endif
454#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000455 x = PyInt_FromLong(SIGABRT);
456 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000457 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458#endif
459#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000460 x = PyInt_FromLong(SIGEMT);
461 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000462 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000463#endif
464#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000465 x = PyInt_FromLong(SIGFPE);
466 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000467 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000468#endif
469#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000470 x = PyInt_FromLong(SIGKILL);
471 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000472 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000473#endif
474#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000475 x = PyInt_FromLong(SIGBUS);
476 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000477 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478#endif
479#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000480 x = PyInt_FromLong(SIGSEGV);
481 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000482 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483#endif
484#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000485 x = PyInt_FromLong(SIGSYS);
486 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000487 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000488#endif
489#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000490 x = PyInt_FromLong(SIGPIPE);
491 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000492 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493#endif
494#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000495 x = PyInt_FromLong(SIGALRM);
496 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000497 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498#endif
499#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000500 x = PyInt_FromLong(SIGTERM);
501 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000502 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503#endif
504#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000505 x = PyInt_FromLong(SIGUSR1);
506 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000507 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000508#endif
509#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000510 x = PyInt_FromLong(SIGUSR2);
511 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000512 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513#endif
514#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000515 x = PyInt_FromLong(SIGCLD);
516 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000517 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000518#endif
519#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000520 x = PyInt_FromLong(SIGCHLD);
521 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000522 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000523#endif
524#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000525 x = PyInt_FromLong(SIGPWR);
526 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000527 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000528#endif
529#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000530 x = PyInt_FromLong(SIGIO);
531 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000532 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000533#endif
534#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000535 x = PyInt_FromLong(SIGURG);
536 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000537 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000538#endif
539#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000540 x = PyInt_FromLong(SIGWINCH);
541 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000542 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000543#endif
544#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000545 x = PyInt_FromLong(SIGPOLL);
546 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000547 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000548#endif
549#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000550 x = PyInt_FromLong(SIGSTOP);
551 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000552 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000553#endif
554#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000555 x = PyInt_FromLong(SIGTSTP);
556 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000557 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000558#endif
559#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000560 x = PyInt_FromLong(SIGCONT);
561 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000562 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000563#endif
564#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000565 x = PyInt_FromLong(SIGTTIN);
566 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000567 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000568#endif
569#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000570 x = PyInt_FromLong(SIGTTOU);
571 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000572 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000573#endif
574#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000575 x = PyInt_FromLong(SIGVTALRM);
576 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000577 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000578#endif
579#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000580 x = PyInt_FromLong(SIGPROF);
581 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000582 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000583#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000584#ifdef SIGXCPU
585 x = PyInt_FromLong(SIGXCPU);
586 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000587 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000588#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000589#ifdef SIGXFSZ
590 x = PyInt_FromLong(SIGXFSZ);
591 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000592 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000593#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000594#ifdef SIGRTMIN
595 x = PyInt_FromLong(SIGRTMIN);
596 PyDict_SetItemString(d, "SIGRTMIN", x);
597 Py_XDECREF(x);
598#endif
599#ifdef SIGRTMAX
600 x = PyInt_FromLong(SIGRTMAX);
601 PyDict_SetItemString(d, "SIGRTMAX", x);
602 Py_XDECREF(x);
603#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000604#ifdef SIGINFO
605 x = PyInt_FromLong(SIGINFO);
606 PyDict_SetItemString(d, "SIGINFO", x);
607 Py_XDECREF(x);
608#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000609 if (!PyErr_Occurred())
610 return;
611
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000612 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000613 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000614 return;
615}
616
617static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000618finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000619{
620 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000621 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000622
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000623 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000624 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000625
626 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000627 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000628 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000629 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000630 if (i != SIGINT && func != NULL && func != Py_None &&
631 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000632 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000633 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000634 }
635
636 Py_XDECREF(IntHandler);
637 IntHandler = NULL;
638 Py_XDECREF(DefaultHandler);
639 DefaultHandler = NULL;
640 Py_XDECREF(IgnoreHandler);
641 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000642}
643
Barry Warsaw92971171997-01-03 00:14:25 +0000644
Barry Warsaw92971171997-01-03 00:14:25 +0000645/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000646int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000647PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000648{
649 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000650 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000651
652 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000653 return 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000654
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000655#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000656 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000657 return 0;
658#endif
Guido van Rossum137c49c2007-12-10 23:00:12 +0000659
660 /*
661 * The is_stripped variable is meant to speed up the calls to
662 * PyErr_CheckSignals (both directly or via pending calls) when no
663 * signal has arrived. This variable is set to 1 when a signal arrives
664 * and it is set to 0 here, when we know some signals arrived. This way
665 * we can run the registered handlers with no signals blocked.
666 *
667 * NOTE: with this approach we can have a situation where is_tripped is
668 * 1 but we have no more signals to handle (Handlers[i].tripped
669 * is 0 for every signal i). This won't do us any harm (except
670 * we're gonna spent some cycles for nothing). This happens when
671 * we receive a signal i after we zero is_tripped and before we
672 * check Handlers[i].tripped.
673 */
674 is_tripped = 0;
675
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000676 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000677 f = Py_None;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000678
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000679 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000680 if (Handlers[i].tripped) {
681 PyObject *result = NULL;
682 PyObject *arglist = Py_BuildValue("(iO)", i, f);
683 Handlers[i].tripped = 0;
684
685 if (arglist) {
686 result = PyEval_CallObject(Handlers[i].func,
687 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000688 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000689 }
Barry Warsaw92971171997-01-03 00:14:25 +0000690 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000691 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000692
693 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000694 }
695 }
Guido van Rossum137c49c2007-12-10 23:00:12 +0000696
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000697 return 0;
698}
699
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000700
Barry Warsaw92971171997-01-03 00:14:25 +0000701/* Replacements for intrcheck.c functionality
702 * Declared in pyerrors.h
703 */
704void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000705PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000706{
Guido van Rossum137c49c2007-12-10 23:00:12 +0000707 is_tripped = 1;
Barry Warsaw92971171997-01-03 00:14:25 +0000708 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000709 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000710}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000711
712void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000713PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000714{
715 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000716 _PyImport_FixupExtension("signal", "signal");
717}
718
719void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000720PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000721{
722 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000723}
724
725int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000726PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000727{
Barry Warsaw92971171997-01-03 00:14:25 +0000728 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000729#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000730 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000731 return 0;
732#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000733 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000734 return 1;
735 }
736 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000737}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000738
739void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000740PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000741{
742#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000743 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000744 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000745 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000746 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000747#endif
748}