Merged revisions 81057 via svnmerge from
[python/dscho.git] / Parser / intrcheck.c
blob4439864d20c054c56c0a75d8ffe343bbb57ac7cc
2 /* Check for interrupts */
4 #include "Python.h"
5 #include "pythread.h"
7 #ifdef QUICKWIN
9 #include <io.h>
11 void
12 PyOS_InitInterrupts(void)
16 void
17 PyOS_FiniInterrupts(void)
21 int
22 PyOS_InterruptOccurred(void)
24 _wyield();
27 #define OK
29 #endif /* QUICKWIN */
31 #if defined(_M_IX86) && !defined(__QNX__)
32 #include <io.h>
33 #endif
35 #if defined(MSDOS) && !defined(QUICKWIN)
37 #ifdef __GNUC__
39 /* This is for DJGPP's GO32 extender. I don't know how to trap
40 * control-C (There's no API for ctrl-C, and I don't want to mess with
41 * the interrupt vectors.) However, this DOES catch control-break.
42 * --Amrit
45 #include <go32.h>
47 void
48 PyOS_InitInterrupts(void)
50 _go32_want_ctrl_break(1 /* TRUE */);
53 void
54 PyOS_FiniInterrupts(void)
58 int
59 PyOS_InterruptOccurred(void)
61 return _go32_was_ctrl_break_hit();
64 #else /* !__GNUC__ */
66 /* This might work for MS-DOS (untested though): */
68 void
69 PyOS_InitInterrupts(void)
73 void
74 PyOS_FiniInterrupts(void)
78 int
79 PyOS_InterruptOccurred(void)
81 int interrupted = 0;
82 while (kbhit()) {
83 if (getch() == '\003')
84 interrupted = 1;
86 return interrupted;
89 #endif /* __GNUC__ */
91 #define OK
93 #endif /* MSDOS && !QUICKWIN */
96 #ifndef OK
98 /* Default version -- for real operating systems and for Standard C */
100 #include <stdio.h>
101 #include <string.h>
102 #include <signal.h>
104 static int interrupted;
106 void
107 PyErr_SetInterrupt(void)
109 interrupted = 1;
112 extern int PyErr_CheckSignals(void);
114 static int
115 checksignals_witharg(void * arg)
117 return PyErr_CheckSignals();
120 static void
121 intcatcher(int sig)
123 extern void Py_Exit(int);
124 static char message[] =
125 "python: to interrupt a truly hanging Python program, interrupt once more.\n";
126 switch (interrupted++) {
127 case 0:
128 break;
129 case 1:
130 write(2, message, strlen(message));
131 break;
132 case 2:
133 interrupted = 0;
134 Py_Exit(1);
135 break;
137 PyOS_setsig(SIGINT, intcatcher);
138 Py_AddPendingCall(checksignals_witharg, NULL);
141 static void (*old_siginthandler)(int) = SIG_DFL;
143 void
144 PyOS_InitInterrupts(void)
146 if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN)
147 PyOS_setsig(SIGINT, intcatcher);
150 void
151 PyOS_FiniInterrupts(void)
153 PyOS_setsig(SIGINT, old_siginthandler);
157 PyOS_InterruptOccurred(void)
159 if (!interrupted)
160 return 0;
161 interrupted = 0;
162 return 1;
165 #endif /* !OK */
167 void
168 PyOS_AfterFork(void)
170 #ifdef WITH_THREAD
171 PyEval_ReInitThreads();
172 PyThread_ReInitTLS();
173 #endif