blob: 686e030e601d00b87a8d8d0bd524cb3b37371c53 [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 Rossumd6a15ad1991-06-24 22:30:42 +000095#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096
Jack Jansen9fc39891995-01-27 14:40:41 +000097/* The Mac interrupt code has moved to macglue.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098#define OK
99
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000100#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101
102
103#ifndef OK
104
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106
107#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000108#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109#include <signal.h>
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000110#ifdef HAVE_UNISTD_H
111#include <unistd.h>
112#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113
Guido van Rossum62e376b1995-01-17 16:11:53 +0000114static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115
Guido van Rossum575d5611995-03-10 15:13:03 +0000116void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000117PyErr_SetInterrupt(void)
Guido van Rossum575d5611995-03-10 15:13:03 +0000118{
119 interrupted = 1;
120}
121
Thomas Wouters23c9e002000-07-22 19:20:54 +0000122extern int PyErr_CheckSignals(void);
Guido van Rossumad74fa61997-01-21 06:00:33 +0000123
Thomas Wouters23c9e002000-07-22 19:20:54 +0000124static int
125checksignals_witharg(void * arg)
126{
127 return PyErr_CheckSignals();
128}
129
Tim Peters4f1b2082000-07-23 21:18:09 +0000130static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000131intcatcher(int sig)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000133 extern void Py_Exit(int);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000134 static char message[] =
135"python: to interrupt a truly hanging Python program, interrupt once more.\n";
136 switch (interrupted++) {
137 case 0:
138 break;
139 case 1:
Guido van Rossum48a680c2001-03-02 06:34:14 +0000140#ifdef RISCOS
141 fprintf(stderr, message);
142#else
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000143 write(2, message, strlen(message));
Guido van Rossum48a680c2001-03-02 06:34:14 +0000144#endif
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000145 break;
146 case 2:
147 interrupted = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +0000148 Py_Exit(1);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000149 break;
150 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 signal(SIGINT, intcatcher);
Thomas Wouters23c9e002000-07-22 19:20:54 +0000152 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153}
154
Tim Peters4f1b2082000-07-23 21:18:09 +0000155static void (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossumaee094c1997-08-02 03:02:27 +0000156
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000158PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159{
Guido van Rossumaee094c1997-08-02 03:02:27 +0000160 if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000162#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000163 /* This is for SunOS and other modern BSD derivatives.
164 It means that system calls (like read()) are not restarted
165 after an interrupt. This is necessary so interrupting a
166 read() or readline() call works as expected.
167 XXX On old BSD (pure 4.2 or older) you may have to do this
168 differently! */
169 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000170#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
Guido van Rossumaee094c1997-08-02 03:02:27 +0000173void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000174PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +0000175{
176 signal(SIGINT, old_siginthandler);
177}
178
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000180PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181{
182 if (!interrupted)
183 return 0;
184 interrupted = 0;
185 return 1;
186}
187
188#endif /* !OK */
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000189
190void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000191PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000192{
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000193#ifdef WITH_THREAD
194 PyEval_ReInitThreads();
195#endif
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000196}