blob: e0f3252db6c44be3e2d24a324909d75fc5f28446 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Check for interrupts */
3
Fred Drakeeb375e42000-08-23 18:17:42 +00004#include "Python.h"
Guido van Rossumd6a15ad1991-06-24 22:30:42 +00005
Guido van Rossum1d5735e1994-08-30 08:27:36 +00006#ifdef QUICKWIN
7
8#include <io.h>
9
10void
Thomas Wouters23c9e002000-07-22 19:20:54 +000011PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000012{
13}
14
Guido van Rossumaee094c1997-08-02 03:02:27 +000015void
Thomas Wouters23c9e002000-07-22 19:20:54 +000016PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000017{
18}
19
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020int
Thomas Wouters23c9e002000-07-22 19:20:54 +000021PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000022{
23 _wyield();
24}
25
26#define OK
27
28#endif /* QUICKWIN */
29
Guido van Rossum7bf22de1997-12-02 20:34:19 +000030#if defined(_M_IX86) && !defined(__QNX__)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000031#include <io.h>
32#endif
33
34#if defined(MSDOS) && !defined(QUICKWIN)
35
36#ifdef __GNUC__
37
38/* This is for DJGPP's GO32 extender. I don't know how to trap
39 * control-C (There's no API for ctrl-C, and I don't want to mess with
40 * the interrupt vectors.) However, this DOES catch control-break.
41 * --Amrit
42 */
43
44#include <go32.h>
45
46void
Thomas Wouters23c9e002000-07-22 19:20:54 +000047PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000048{
49 _go32_want_ctrl_break(1 /* TRUE */);
50}
51
Guido van Rossumaee094c1997-08-02 03:02:27 +000052void
Thomas Wouters23c9e002000-07-22 19:20:54 +000053PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000054{
55}
56
Guido van Rossum1d5735e1994-08-30 08:27:36 +000057int
Thomas Wouters23c9e002000-07-22 19:20:54 +000058PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000059{
60 return _go32_was_ctrl_break_hit();
61}
62
63#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064
Guido van Rossum3f5da241990-12-20 15:06:42 +000065/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066
67void
Thomas Wouters23c9e002000-07-22 19:20:54 +000068PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069{
70}
71
Guido van Rossumaee094c1997-08-02 03:02:27 +000072void
Thomas Wouters23c9e002000-07-22 19:20:54 +000073PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000074{
75}
76
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077int
Thomas Wouters23c9e002000-07-22 19:20:54 +000078PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079{
80 int interrupted = 0;
81 while (kbhit()) {
82 if (getch() == '\003')
83 interrupted = 1;
84 }
85 return interrupted;
86}
87
Guido van Rossum1d5735e1994-08-30 08:27:36 +000088#endif /* __GNUC__ */
89
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090#define OK
91
Guido van Rossum1d5735e1994-08-30 08:27:36 +000092#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093
94
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095#ifndef OK
96
Guido van Rossum3f5da241990-12-20 15:06:42 +000097/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098
99#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101#include <signal.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102
Guido van Rossum62e376b1995-01-17 16:11:53 +0000103static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104
Guido van Rossum575d5611995-03-10 15:13:03 +0000105void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000106PyErr_SetInterrupt(void)
Guido van Rossum575d5611995-03-10 15:13:03 +0000107{
108 interrupted = 1;
109}
110
Thomas Wouters23c9e002000-07-22 19:20:54 +0000111extern int PyErr_CheckSignals(void);
Guido van Rossumad74fa61997-01-21 06:00:33 +0000112
Thomas Wouters23c9e002000-07-22 19:20:54 +0000113static int
114checksignals_witharg(void * arg)
115{
116 return PyErr_CheckSignals();
117}
118
Tim Peters4f1b2082000-07-23 21:18:09 +0000119static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000120intcatcher(int sig)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000122 extern void Py_Exit(int);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000123 static char message[] =
124"python: to interrupt a truly hanging Python program, interrupt once more.\n";
125 switch (interrupted++) {
126 case 0:
127 break;
128 case 1:
Guido van Rossum48a680c2001-03-02 06:34:14 +0000129#ifdef RISCOS
130 fprintf(stderr, message);
131#else
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000132 write(2, message, strlen(message));
Guido van Rossum48a680c2001-03-02 06:34:14 +0000133#endif
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000134 break;
135 case 2:
136 interrupted = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +0000137 Py_Exit(1);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000138 break;
139 }
Anthony Baxter9ceaa722004-10-13 14:48:50 +0000140 PyOS_setsig(SIGINT, intcatcher);
Thomas Wouters23c9e002000-07-22 19:20:54 +0000141 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142}
143
Tim Peters4f1b2082000-07-23 21:18:09 +0000144static void (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossumaee094c1997-08-02 03:02:27 +0000145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000147PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Anthony Baxter9ceaa722004-10-13 14:48:50 +0000149 if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN)
150 PyOS_setsig(SIGINT, intcatcher);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151}
152
Guido van Rossumaee094c1997-08-02 03:02:27 +0000153void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000154PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +0000155{
Anthony Baxter9ceaa722004-10-13 14:48:50 +0000156 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossumaee094c1997-08-02 03:02:27 +0000157}
158
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000160PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161{
162 if (!interrupted)
163 return 0;
164 interrupted = 0;
165 return 1;
166}
167
168#endif /* !OK */
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000169
170void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000171PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000172{
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000173#ifdef WITH_THREAD
174 PyEval_ReInitThreads();
175#endif
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000176}