blob: 34a3425d48ef39b21b3c02b348910322c139bc10 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Check for interrupts */
12
Guido van Rossum1d5735e1994-08-30 08:27:36 +000013#include "config.h"
Guido van Rossumf2615261998-12-04 18:50:20 +000014
15/* config.h may or may not define DL_IMPORT */
16#ifndef DL_IMPORT /* declarations for DLL import/export */
17#define DL_IMPORT(RTYPE) RTYPE
18#endif
19
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020#include "myproto.h"
Guido van Rossumbae95181997-02-14 21:12:24 +000021#include "mymalloc.h" /* For ANY */
Guido van Rossum189e8f91992-01-19 16:29:05 +000022#include "intrcheck.h"
23
Guido van Rossumbae95181997-02-14 21:12:24 +000024/* Copied here from ceval.h -- can't include that file. */
Tim Petersdbd9ba62000-07-09 03:09:57 +000025int Py_AddPendingCall(int (*func)(ANY *), ANY *arg);
Guido van Rossumbae95181997-02-14 21:12:24 +000026
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000027
Guido van Rossum1d5735e1994-08-30 08:27:36 +000028#ifdef QUICKWIN
29
30#include <io.h>
31
32void
Thomas Wouters23c9e002000-07-22 19:20:54 +000033PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000034{
35}
36
Guido van Rossumaee094c1997-08-02 03:02:27 +000037void
Thomas Wouters23c9e002000-07-22 19:20:54 +000038PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000039{
40}
41
Guido van Rossum1d5735e1994-08-30 08:27:36 +000042int
Thomas Wouters23c9e002000-07-22 19:20:54 +000043PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000044{
45 _wyield();
46}
47
48#define OK
49
50#endif /* QUICKWIN */
51
Guido van Rossum7bf22de1997-12-02 20:34:19 +000052#if defined(_M_IX86) && !defined(__QNX__)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000053#include <io.h>
54#endif
55
56#if defined(MSDOS) && !defined(QUICKWIN)
57
58#ifdef __GNUC__
59
60/* This is for DJGPP's GO32 extender. I don't know how to trap
61 * control-C (There's no API for ctrl-C, and I don't want to mess with
62 * the interrupt vectors.) However, this DOES catch control-break.
63 * --Amrit
64 */
65
66#include <go32.h>
67
68void
Thomas Wouters23c9e002000-07-22 19:20:54 +000069PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000070{
71 _go32_want_ctrl_break(1 /* TRUE */);
72}
73
Guido van Rossumaee094c1997-08-02 03:02:27 +000074void
Thomas Wouters23c9e002000-07-22 19:20:54 +000075PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000076{
77}
78
Guido van Rossum1d5735e1994-08-30 08:27:36 +000079int
Thomas Wouters23c9e002000-07-22 19:20:54 +000080PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000081{
82 return _go32_was_ctrl_break_hit();
83}
84
85#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086
Guido van Rossum3f5da241990-12-20 15:06:42 +000087/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088
89void
Thomas Wouters23c9e002000-07-22 19:20:54 +000090PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091{
92}
93
Guido van Rossumaee094c1997-08-02 03:02:27 +000094void
Thomas Wouters23c9e002000-07-22 19:20:54 +000095PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000096{
97}
98
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000100PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
102 int interrupted = 0;
103 while (kbhit()) {
104 if (getch() == '\003')
105 interrupted = 1;
106 }
107 return interrupted;
108}
109
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000110#endif /* __GNUC__ */
111
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112#define OK
113
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000114#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115
116
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000117#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118
Jack Jansen9fc39891995-01-27 14:40:41 +0000119/* The Mac interrupt code has moved to macglue.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120#define OK
121
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000122#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123
124
125#ifndef OK
126
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128
129#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000130#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131#include <signal.h>
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000132#ifdef HAVE_UNISTD_H
133#include <unistd.h>
134#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135
Guido van Rossum62e376b1995-01-17 16:11:53 +0000136static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137
Guido van Rossum575d5611995-03-10 15:13:03 +0000138void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000139PyErr_SetInterrupt(void)
Guido van Rossum575d5611995-03-10 15:13:03 +0000140{
141 interrupted = 1;
142}
143
Thomas Wouters23c9e002000-07-22 19:20:54 +0000144extern int PyErr_CheckSignals(void);
Guido van Rossumad74fa61997-01-21 06:00:33 +0000145
Thomas Wouters23c9e002000-07-22 19:20:54 +0000146static int
147checksignals_witharg(void * arg)
148{
149 return PyErr_CheckSignals();
150}
151
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000152static RETSIGTYPE
Thomas Wouters23c9e002000-07-22 19:20:54 +0000153intcatcher(int sig)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000155 extern void Py_Exit(int);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000156 static char message[] =
157"python: to interrupt a truly hanging Python program, interrupt once more.\n";
158 switch (interrupted++) {
159 case 0:
160 break;
161 case 1:
162 write(2, message, strlen(message));
163 break;
164 case 2:
165 interrupted = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +0000166 Py_Exit(1);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000167 break;
168 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 signal(SIGINT, intcatcher);
Thomas Wouters23c9e002000-07-22 19:20:54 +0000170 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
Thomas Wouters23c9e002000-07-22 19:20:54 +0000173static RETSIGTYPE (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossumaee094c1997-08-02 03:02:27 +0000174
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000176PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177{
Guido van Rossumaee094c1997-08-02 03:02:27 +0000178 if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000180#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000181 /* This is for SunOS and other modern BSD derivatives.
182 It means that system calls (like read()) are not restarted
183 after an interrupt. This is necessary so interrupting a
184 read() or readline() call works as expected.
185 XXX On old BSD (pure 4.2 or older) you may have to do this
186 differently! */
187 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000188#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189}
190
Guido van Rossumaee094c1997-08-02 03:02:27 +0000191void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000192PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +0000193{
194 signal(SIGINT, old_siginthandler);
195}
196
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000198PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199{
200 if (!interrupted)
201 return 0;
202 interrupted = 0;
203 return 1;
204}
205
206#endif /* !OK */
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000207
208void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000209PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000210{
211}