blob: 9d223d575a707f81a4952cf8962a0cacdf4d8fc5 [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 Rossumbb4ba121994-06-23 11:25:45 +000015#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000016#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000017#endif
18
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000019#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000020#define NSIG 12
21#include <process.h>
22#endif
23
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000024#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000025# if defined(_NSIG)
26# define NSIG _NSIG /* For BSD/SysV */
27# elif defined(_SIGMAX)
28# define NSIG (_SIGMAX + 1) /* For QNX */
29# elif defined(SIGMAX)
30# define NSIG (SIGMAX + 1) /* For djgpp */
31# else
32# define NSIG 64 /* Use a reasonable default value */
33# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000034#endif
35
36
Guido van Rossumbb4ba121994-06-23 11:25:45 +000037/*
38 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
39
40 When threads are supported, we want the following semantics:
41
42 - only the main thread can set a signal handler
43 - any thread can get a signal handler
44 - signals are only delivered to the main thread
45
46 I.e. we don't support "synchronous signals" like SIGFPE (catching
47 this doesn't make much sense in Python anyway) nor do we support
48 signals as a means of inter-thread communication, since not all
49 thread implementations support that (at least our thread library
50 doesn't).
51
52 We still have the problem that in some implementations signals
53 generated by the keyboard (e.g. SIGINT) are delivered to all
54 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
55 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000056 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000057 a working implementation that works in all three cases -- the
58 handler ignores signals if getpid() isn't the same as in the main
59 thread. XXX This is a hack.
60
Guido van Rossum9e8181b2000-09-19 00:46:46 +000061 GNU pth is a user-space threading library, and as such, all threads
62 run within the same process. In this case, if the currently running
63 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000064*/
65
66#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000067#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000068#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000069static long main_thread;
70static pid_t main_pid;
71#endif
72
Barry Warsaw92971171997-01-03 00:14:25 +000073static struct {
74 int tripped;
75 PyObject *func;
76} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000077
Guido van Rossum47485a42007-12-10 23:03:55 +000078/* Speed up sigcheck() when none tripped */
79static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000080
Barry Warsaw92971171997-01-03 00:14:25 +000081static PyObject *DefaultHandler;
82static PyObject *IgnoreHandler;
83static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000084
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000085/* On Solaris 8, gcc will produce a warning that the function
86 declaration is not a prototype. This is caused by the definition of
87 SIG_DFL as (void (*)())0; the correct declaration would have been
88 (void (*)(int))0. */
89
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000090static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000091
Barry Warsaw92971171997-01-03 00:14:25 +000092
Guido van Rossume4485b01994-09-07 14:32:49 +000093static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000094signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000095{
Guido van Rossume4485b01994-09-07 14:32:49 +000096 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +000097 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000098}
99
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000101"default_int_handler(...)\n\
102\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000103The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000105
Thomas Wouters0796b002000-07-22 23:49:30 +0000106
107static int
108checksignals_witharg(void * unused)
109{
110 return PyErr_CheckSignals();
111}
112
Tim Peters4f1b2082000-07-23 21:18:09 +0000113static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000114signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000116#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000117#ifdef WITH_PTH
118 if (PyThread_get_thread_ident() != main_thread) {
119 pth_raise(*(pth_t *) main_thread, sig_num);
120 return;
121 }
122#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000123 /* See NOTES section above */
124 if (getpid() == main_pid) {
125#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000126 Handlers[sig_num].tripped = 1;
Guido van Rossum47485a42007-12-10 23:03:55 +0000127 /* Set is_tripped after setting .tripped, as it gets
128 cleared in PyErr_CheckSignals() before .tripped. */
129 is_tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000130 Py_AddPendingCall(checksignals_witharg, 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... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000140 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000141 }
142#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000143 PyOS_setsig(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
Guido van Rossum1171ee61997-08-22 20:42:00 +0000147#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000148static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000149signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150{
151 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000152 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000153 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000154 /* alarm() returns the number of seconds remaining */
Fred Drakedff3a372001-07-19 21:29:49 +0000155 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000156}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000158PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000159"alarm(seconds)\n\
160\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000161Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000162#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163
Guido van Rossum1171ee61997-08-22 20:42:00 +0000164#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000165static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000166signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000167{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000168 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000169 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000170 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000171 /* make sure that any exceptions that got raised are propagated
172 * back into Python
173 */
174 if (PyErr_CheckSignals())
175 return NULL;
176
Guido van Rossume4485b01994-09-07 14:32:49 +0000177 Py_INCREF(Py_None);
178 return Py_None;
179}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000180PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000181"pause()\n\
182\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000183Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000184
Guido van Rossum06d511d1995-03-10 15:13:48 +0000185#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000186
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000187
Guido van Rossume4485b01994-09-07 14:32:49 +0000188static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000189signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000190{
191 PyObject *obj;
192 int sig_num;
193 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000194 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000195 if (!PyArg_ParseTuple(args, "iO:signal", &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
Guido van Rossum65d5b571998-12-21 19:32:43 +0000198 if (PyThread_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 Rossumd2cd7ad2000-09-16 16:35:28 +0000220 if (PyOS_setsig(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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000231PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000232"signal(sig, action) -> action\n\
233\n\
234Set the action for the given signal. The action can be SIG_DFL,\n\
235SIG_IGN, or a callable Python object. The previous action is\n\
236returned. See getsignal() for possible return values.\n\
237\n\
238*** IMPORTANT NOTICE ***\n\
239A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000241
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000242
Guido van Rossume4485b01994-09-07 14:32:49 +0000243static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000244signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000245{
246 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000247 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000248 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000249 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000250 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000251 PyErr_SetString(PyExc_ValueError,
252 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000253 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000254 }
Barry Warsaw92971171997-01-03 00:14:25 +0000255 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000256 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000257 return old_handler;
258}
259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000261"getsignal(sig) -> action\n\
262\n\
263Return the current action for the given signal. The return value can be:\n\
264SIG_IGN -- if the signal is being ignored\n\
265SIG_DFL -- if the default action for the signal is in effect\n\
266None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000267anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000268
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000269
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000270/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000271static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000272#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000273 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000274#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000275 {"signal", signal_signal, METH_VARARGS, signal_doc},
276 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000277#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000278 {"pause", (PyCFunction)signal_pause,
279 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000280#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000281 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000282 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000283 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000284};
285
Barry Warsaw92971171997-01-03 00:14:25 +0000286
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000287PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000288"This module provides mechanisms to use signal handlers in Python.\n\
289\n\
290Functions:\n\
291\n\
292alarm() -- cause SIGALRM after a specified time [Unix only]\n\
293signal() -- set the action for a given signal\n\
294getsignal() -- get the signal action for a given signal\n\
295pause() -- wait until a signal arrives [Unix only]\n\
296default_int_handler() -- default SIGINT handler\n\
297\n\
298Constants:\n\
299\n\
300SIG_DFL -- used to refer to the system default handler\n\
301SIG_IGN -- used to ignore the signal\n\
302NSIG -- number of defined signals\n\
303\n\
304SIGINT, SIGTERM, etc. -- signal numbers\n\
305\n\
306*** IMPORTANT NOTICE ***\n\
307A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000308the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000309
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000310PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000311initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000312{
Guido van Rossume4485b01994-09-07 14:32:49 +0000313 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000314 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000315
316#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000317 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000318 main_pid = getpid();
319#endif
320
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000321 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000322 m = Py_InitModule3("signal", signal_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000323 if (m == NULL)
324 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325
326 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000327 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000328
Guido van Rossum276fa432000-06-30 23:04:18 +0000329 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000330 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
331 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332
Guido van Rossum276fa432000-06-30 23:04:18 +0000333 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000334 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
335 goto finally;
336
337 x = PyInt_FromLong((long)NSIG);
338 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
339 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000340 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000341
342 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
343 if (!x)
344 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000345 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000346
347 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000349 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000350 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000351 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000352 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000353 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000354 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000355 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000356 else
Barry Warsaw92971171997-01-03 00:14:25 +0000357 Handlers[i].func = Py_None; /* None of our business */
358 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000359 }
Barry Warsaw92971171997-01-03 00:14:25 +0000360 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000361 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000362 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000363 Py_DECREF(Handlers[SIGINT].func);
364 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000365 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000366 }
367
368#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000369 x = PyInt_FromLong(SIGHUP);
370 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000371 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372#endif
373#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000374 x = PyInt_FromLong(SIGINT);
375 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000376 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000377#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000378#ifdef SIGBREAK
379 x = PyInt_FromLong(SIGBREAK);
380 PyDict_SetItemString(d, "SIGBREAK", x);
381 Py_XDECREF(x);
382#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000383#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000384 x = PyInt_FromLong(SIGQUIT);
385 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000386 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000387#endif
388#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000389 x = PyInt_FromLong(SIGILL);
390 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000391 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392#endif
393#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000394 x = PyInt_FromLong(SIGTRAP);
395 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000396 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000397#endif
398#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000399 x = PyInt_FromLong(SIGIOT);
400 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000401 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000402#endif
403#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000404 x = PyInt_FromLong(SIGABRT);
405 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000406 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000407#endif
408#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000409 x = PyInt_FromLong(SIGEMT);
410 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000411 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000412#endif
413#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000414 x = PyInt_FromLong(SIGFPE);
415 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000416 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000417#endif
418#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000419 x = PyInt_FromLong(SIGKILL);
420 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000421 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000422#endif
423#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000424 x = PyInt_FromLong(SIGBUS);
425 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000426 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000427#endif
428#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000429 x = PyInt_FromLong(SIGSEGV);
430 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000431 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000432#endif
433#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000434 x = PyInt_FromLong(SIGSYS);
435 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000436 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000437#endif
438#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000439 x = PyInt_FromLong(SIGPIPE);
440 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000441 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000442#endif
443#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000444 x = PyInt_FromLong(SIGALRM);
445 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000446 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000447#endif
448#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000449 x = PyInt_FromLong(SIGTERM);
450 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000451 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000452#endif
453#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000454 x = PyInt_FromLong(SIGUSR1);
455 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000456 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000457#endif
458#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000459 x = PyInt_FromLong(SIGUSR2);
460 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000461 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000462#endif
463#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000464 x = PyInt_FromLong(SIGCLD);
465 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000466 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000467#endif
468#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000469 x = PyInt_FromLong(SIGCHLD);
470 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000471 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000472#endif
473#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000474 x = PyInt_FromLong(SIGPWR);
475 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000476 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477#endif
478#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000479 x = PyInt_FromLong(SIGIO);
480 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000481 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000482#endif
483#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000484 x = PyInt_FromLong(SIGURG);
485 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000486 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000487#endif
488#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000489 x = PyInt_FromLong(SIGWINCH);
490 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000491 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000492#endif
493#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000494 x = PyInt_FromLong(SIGPOLL);
495 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000496 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000497#endif
498#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000499 x = PyInt_FromLong(SIGSTOP);
500 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000501 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000502#endif
503#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000504 x = PyInt_FromLong(SIGTSTP);
505 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000506 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000507#endif
508#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000509 x = PyInt_FromLong(SIGCONT);
510 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000511 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000512#endif
513#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000514 x = PyInt_FromLong(SIGTTIN);
515 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000516 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000517#endif
518#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000519 x = PyInt_FromLong(SIGTTOU);
520 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000521 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000522#endif
523#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000524 x = PyInt_FromLong(SIGVTALRM);
525 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000526 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000527#endif
528#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000529 x = PyInt_FromLong(SIGPROF);
530 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000531 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000533#ifdef SIGXCPU
534 x = PyInt_FromLong(SIGXCPU);
535 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000536 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000538#ifdef SIGXFSZ
539 x = PyInt_FromLong(SIGXFSZ);
540 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000541 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000542#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000543#ifdef SIGRTMIN
544 x = PyInt_FromLong(SIGRTMIN);
545 PyDict_SetItemString(d, "SIGRTMIN", x);
546 Py_XDECREF(x);
547#endif
548#ifdef SIGRTMAX
549 x = PyInt_FromLong(SIGRTMAX);
550 PyDict_SetItemString(d, "SIGRTMAX", x);
551 Py_XDECREF(x);
552#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000553#ifdef SIGINFO
554 x = PyInt_FromLong(SIGINFO);
555 PyDict_SetItemString(d, "SIGINFO", x);
556 Py_XDECREF(x);
557#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000558 if (!PyErr_Occurred())
559 return;
560
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000561 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000562 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000563 return;
564}
565
566static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000567finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000568{
569 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000570 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000571
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000572 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000573 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000574
575 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000576 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000577 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000578 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000579 if (i != SIGINT && func != NULL && func != Py_None &&
580 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000581 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000582 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000583 }
584
585 Py_XDECREF(IntHandler);
586 IntHandler = NULL;
587 Py_XDECREF(DefaultHandler);
588 DefaultHandler = NULL;
589 Py_XDECREF(IgnoreHandler);
590 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000591}
592
Barry Warsaw92971171997-01-03 00:14:25 +0000593
Barry Warsaw92971171997-01-03 00:14:25 +0000594/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000595int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000596PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000597{
598 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000599 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000600
601 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000602 return 0;
Guido van Rossum47485a42007-12-10 23:03:55 +0000603
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000604#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000605 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000606 return 0;
607#endif
Guido van Rossum47485a42007-12-10 23:03:55 +0000608
609 /*
610 * The is_stripped variable is meant to speed up the calls to
611 * PyErr_CheckSignals (both directly or via pending calls) when no
612 * signal has arrived. This variable is set to 1 when a signal arrives
613 * and it is set to 0 here, when we know some signals arrived. This way
614 * we can run the registered handlers with no signals blocked.
615 *
616 * NOTE: with this approach we can have a situation where is_tripped is
617 * 1 but we have no more signals to handle (Handlers[i].tripped
618 * is 0 for every signal i). This won't do us any harm (except
619 * we're gonna spent some cycles for nothing). This happens when
620 * we receive a signal i after we zero is_tripped and before we
621 * check Handlers[i].tripped.
622 */
623 is_tripped = 0;
624
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000625 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000626 f = Py_None;
Guido van Rossum47485a42007-12-10 23:03:55 +0000627
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000628 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000629 if (Handlers[i].tripped) {
630 PyObject *result = NULL;
631 PyObject *arglist = Py_BuildValue("(iO)", i, f);
632 Handlers[i].tripped = 0;
633
634 if (arglist) {
635 result = PyEval_CallObject(Handlers[i].func,
636 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000637 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638 }
Barry Warsaw92971171997-01-03 00:14:25 +0000639 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000640 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000641
642 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643 }
644 }
Guido van Rossum47485a42007-12-10 23:03:55 +0000645
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000646 return 0;
647}
648
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000649
Barry Warsaw92971171997-01-03 00:14:25 +0000650/* Replacements for intrcheck.c functionality
651 * Declared in pyerrors.h
652 */
653void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000654PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000655{
Guido van Rossum47485a42007-12-10 23:03:55 +0000656 is_tripped = 1;
Barry Warsaw92971171997-01-03 00:14:25 +0000657 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000658 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000659}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000660
661void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000662PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000663{
664 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000665 _PyImport_FixupExtension("signal", "signal");
666}
667
668void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000669PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000670{
671 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000672}
673
674int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000675PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000676{
Barry Warsaw92971171997-01-03 00:14:25 +0000677 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000678#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000679 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000680 return 0;
681#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000682 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000683 return 1;
684 }
685 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000686}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000687
688void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000689PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000690{
691#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000692 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000693 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000694 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000695 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000696#endif
697}