blob: 6ed0e5561b9bea98dd74dd2f4e2638a37ef10090 [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 Rossum189e8f91992-01-19 16:29:05 +000020#include "intrcheck.h"
21
Guido van Rossumbae95181997-02-14 21:12:24 +000022/* Copied here from ceval.h -- can't include that file. */
Thomas Wouters334fb892000-07-25 12:56:38 +000023int Py_AddPendingCall(int (*func)(void *), void *arg);
Guido van Rossumbae95181997-02-14 21:12:24 +000024
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000025
Guido van Rossum1d5735e1994-08-30 08:27:36 +000026#ifdef QUICKWIN
27
28#include <io.h>
29
30void
Thomas Wouters23c9e002000-07-22 19:20:54 +000031PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000032{
33}
34
Guido van Rossumaee094c1997-08-02 03:02:27 +000035void
Thomas Wouters23c9e002000-07-22 19:20:54 +000036PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000037{
38}
39
Guido van Rossum1d5735e1994-08-30 08:27:36 +000040int
Thomas Wouters23c9e002000-07-22 19:20:54 +000041PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000042{
43 _wyield();
44}
45
46#define OK
47
48#endif /* QUICKWIN */
49
Guido van Rossum7bf22de1997-12-02 20:34:19 +000050#if defined(_M_IX86) && !defined(__QNX__)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000051#include <io.h>
52#endif
53
54#if defined(MSDOS) && !defined(QUICKWIN)
55
56#ifdef __GNUC__
57
58/* This is for DJGPP's GO32 extender. I don't know how to trap
59 * control-C (There's no API for ctrl-C, and I don't want to mess with
60 * the interrupt vectors.) However, this DOES catch control-break.
61 * --Amrit
62 */
63
64#include <go32.h>
65
66void
Thomas Wouters23c9e002000-07-22 19:20:54 +000067PyOS_InitInterrupts(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000068{
69 _go32_want_ctrl_break(1 /* TRUE */);
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 Rossum1d5735e1994-08-30 08:27:36 +000077int
Thomas Wouters23c9e002000-07-22 19:20:54 +000078PyOS_InterruptOccurred(void)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000079{
80 return _go32_was_ctrl_break_hit();
81}
82
83#else /* !__GNUC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
Guido van Rossum3f5da241990-12-20 15:06:42 +000085/* This might work for MS-DOS (untested though): */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086
87void
Thomas Wouters23c9e002000-07-22 19:20:54 +000088PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089{
90}
91
Guido van Rossumaee094c1997-08-02 03:02:27 +000092void
Thomas Wouters23c9e002000-07-22 19:20:54 +000093PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +000094{
95}
96
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097int
Thomas Wouters23c9e002000-07-22 19:20:54 +000098PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099{
100 int interrupted = 0;
101 while (kbhit()) {
102 if (getch() == '\003')
103 interrupted = 1;
104 }
105 return interrupted;
106}
107
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000108#endif /* __GNUC__ */
109
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110#define OK
111
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000112#endif /* MSDOS && !QUICKWIN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113
114
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000115#ifdef macintosh
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116
Jack Jansen9fc39891995-01-27 14:40:41 +0000117/* The Mac interrupt code has moved to macglue.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118#define OK
119
Guido van Rossumd6a15ad1991-06-24 22:30:42 +0000120#endif /* macintosh */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121
122
123#ifndef OK
124
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125/* Default version -- for real operating systems and for Standard C */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126
127#include <stdio.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000128#include <string.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129#include <signal.h>
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000130#ifdef HAVE_UNISTD_H
131#include <unistd.h>
132#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133
Guido van Rossum62e376b1995-01-17 16:11:53 +0000134static int interrupted;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135
Guido van Rossum575d5611995-03-10 15:13:03 +0000136void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000137PyErr_SetInterrupt(void)
Guido van Rossum575d5611995-03-10 15:13:03 +0000138{
139 interrupted = 1;
140}
141
Thomas Wouters23c9e002000-07-22 19:20:54 +0000142extern int PyErr_CheckSignals(void);
Guido van Rossumad74fa61997-01-21 06:00:33 +0000143
Thomas Wouters23c9e002000-07-22 19:20:54 +0000144static int
145checksignals_witharg(void * arg)
146{
147 return PyErr_CheckSignals();
148}
149
Tim Peters4f1b2082000-07-23 21:18:09 +0000150static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000151intcatcher(int sig)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000153 extern void Py_Exit(int);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000154 static char message[] =
155"python: to interrupt a truly hanging Python program, interrupt once more.\n";
156 switch (interrupted++) {
157 case 0:
158 break;
159 case 1:
160 write(2, message, strlen(message));
161 break;
162 case 2:
163 interrupted = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +0000164 Py_Exit(1);
Guido van Rossumf1dc5661993-07-05 10:31:29 +0000165 break;
166 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 signal(SIGINT, intcatcher);
Thomas Wouters23c9e002000-07-22 19:20:54 +0000168 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169}
170
Tim Peters4f1b2082000-07-23 21:18:09 +0000171static void (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossumaee094c1997-08-02 03:02:27 +0000172
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000174PyOS_InitInterrupts(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Guido van Rossumaee094c1997-08-02 03:02:27 +0000176 if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177 signal(SIGINT, intcatcher);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000178#ifdef HAVE_SIGINTERRUPT
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000179 /* This is for SunOS and other modern BSD derivatives.
180 It means that system calls (like read()) are not restarted
181 after an interrupt. This is necessary so interrupting a
182 read() or readline() call works as expected.
183 XXX On old BSD (pure 4.2 or older) you may have to do this
184 differently! */
185 siginterrupt(SIGINT, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000186#endif /* HAVE_SIGINTERRUPT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
Guido van Rossumaee094c1997-08-02 03:02:27 +0000189void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000190PyOS_FiniInterrupts(void)
Guido van Rossumaee094c1997-08-02 03:02:27 +0000191{
192 signal(SIGINT, old_siginthandler);
193}
194
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000196PyOS_InterruptOccurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197{
198 if (!interrupted)
199 return 0;
200 interrupted = 0;
201 return 1;
202}
203
204#endif /* !OK */
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000205
206void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000207PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000208{
209}