blob: 4439864d20c054c56c0a75d8ffe343bbb57ac7cc [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"
Benjamin Petersone68df0f2008-06-13 00:26:50 +00005#include "pythread.h"
Guido van Rossumd6a15ad1991-06-24 22:30:42 +00006
Guido van Rossum1d5735e1994-08-30 08:27:36 +00007#ifdef QUICKWIN
8
9#include <io.h>
10
11void
Thomas Wouters23c9e002000-07-22 19:20:54 +000012PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000013{
14}
15
Guido van Rossumaee094c1997-08-02 03:02:27 +000016void
Thomas Wouters23c9e002000-07-22 19:20:54 +000017PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000018{
19}
20
Guido van Rossum1d5735e1994-08-30 08:27:36 +000021int
Thomas Wouters23c9e002000-07-22 19:20:54 +000022PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000024 _wyield();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000025}
26
27#define OK
28
29#endif /* QUICKWIN */
30
Guido van Rossum7bf22de1997-12-02 20:34:19 +000031#if defined(_M_IX86) && !defined(__QNX__)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000032#include <io.h>
33#endif
34
35#if defined(MSDOS) && !defined(QUICKWIN)
36
37#ifdef __GNUC__
38
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
43 */
44
45#include <go32.h>
46
47void
Thomas Wouters23c9e002000-07-22 19:20:54 +000048PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000049{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000050 _go32_want_ctrl_break(1 /* TRUE */);
Guido van Rossum1d5735e1994-08-30 08:27:36 +000051}
52
Guido van Rossumaee094c1997-08-02 03:02:27 +000053void
Thomas Wouters23c9e002000-07-22 19:20:54 +000054PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000055{
56}
57
Guido van Rossum1d5735e1994-08-30 08:27:36 +000058int
Thomas Wouters23c9e002000-07-22 19:20:54 +000059PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000060{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000061 return _go32_was_ctrl_break_hit();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000062}
63
64#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065
Guido van Rossum3f5da241990-12-20 15:06:42 +000066/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067
68void
Thomas Wouters23c9e002000-07-22 19:20:54 +000069PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070{
71}
72
Guido van Rossumaee094c1997-08-02 03:02:27 +000073void
Thomas Wouters23c9e002000-07-22 19:20:54 +000074PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000075{
76}
77
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078int
Thomas Wouters23c9e002000-07-22 19:20:54 +000079PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000081 int interrupted = 0;
82 while (kbhit()) {
83 if (getch() == '\003')
84 interrupted = 1;
85 }
86 return interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087}
88
Guido van Rossum1d5735e1994-08-30 08:27:36 +000089#endif /* __GNUC__ */
90
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091#define OK
92
Guido van Rossum1d5735e1994-08-30 08:27:36 +000093#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094
95
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096#ifndef OK
97
Guido van Rossum3f5da241990-12-20 15:06:42 +000098/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099
100#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000101#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102#include <signal.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103
Guido van Rossum62e376b1995-01-17 16:11:53 +0000104static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105
Guido van Rossum575d5611995-03-10 15:13:03 +0000106void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000107PyErr_SetInterrupt(void)
Guido van Rossum575d5611995-03-10 15:13:03 +0000108{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000109 interrupted = 1;
Guido van Rossum575d5611995-03-10 15:13:03 +0000110}
111
Thomas Wouters23c9e002000-07-22 19:20:54 +0000112extern int PyErr_CheckSignals(void);
Guido van Rossumad74fa61997-01-21 06:00:33 +0000113
Thomas Wouters23c9e002000-07-22 19:20:54 +0000114static int
115checksignals_witharg(void * arg)
116{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 return PyErr_CheckSignals();
Thomas Wouters23c9e002000-07-22 19:20:54 +0000118}
119
Tim Peters4f1b2082000-07-23 21:18:09 +0000120static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000121intcatcher(int sig)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000123 extern void Py_Exit(int);
124 static char message[] =
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000125"python: to interrupt a truly hanging Python program, interrupt once more.\n";
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 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;
136 }
137 PyOS_setsig(SIGINT, intcatcher);
138 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139}
140
Tim Peters4f1b2082000-07-23 21:18:09 +0000141static void (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossumaee094c1997-08-02 03:02:27 +0000142
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000144PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000146 if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN)
147 PyOS_setsig(SIGINT, intcatcher);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148}
149
Guido van Rossumaee094c1997-08-02 03:02:27 +0000150void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000151PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +0000152{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000153 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossumaee094c1997-08-02 03:02:27 +0000154}
155
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000157PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000159 if (!interrupted)
160 return 0;
161 interrupted = 0;
162 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
165#endif /* !OK */
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000166
167void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000168PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000169{
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000170#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 PyEval_ReInitThreads();
172 PyThread_ReInitTLS();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000173#endif
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000174}