blob: 1c888a598e9d0366d434325078f9eebf30a5ce55 [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 Peterson114f7e52008-06-13 00:09:47 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000126 switch (interrupted++) {
127 case 0:
128 break;
129 case 1:
Guido van Rossum48a680c2001-03-02 06:34:14 +0000130#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 fprintf(stderr, message);
Guido van Rossum48a680c2001-03-02 06:34:14 +0000132#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000133 write(2, message, strlen(message));
Guido van Rossum48a680c2001-03-02 06:34:14 +0000134#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000135 break;
136 case 2:
137 interrupted = 0;
138 Py_Exit(1);
139 break;
140 }
141 PyOS_setsig(SIGINT, intcatcher);
142 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143}
144
Tim Peters4f1b2082000-07-23 21:18:09 +0000145static void (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossumaee094c1997-08-02 03:02:27 +0000146
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000148PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000150 if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN)
151 PyOS_setsig(SIGINT, intcatcher);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152}
153
Guido van Rossumaee094c1997-08-02 03:02:27 +0000154void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000155PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +0000156{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossumaee094c1997-08-02 03:02:27 +0000158}
159
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000161PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 if (!interrupted)
164 return 0;
165 interrupted = 0;
166 return 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167}
168
169#endif /* !OK */
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000170
171void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000172PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000173{
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000174#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 PyEval_ReInitThreads();
176 PyThread_ReInitTLS();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000177#endif
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000178}