Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Check for interrupts */ |
| 12 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 13 | #include "config.h" |
Guido van Rossum | f261526 | 1998-12-04 18:50:20 +0000 | [diff] [blame] | 14 | |
| 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 Rossum | 189e8f9 | 1992-01-19 16:29:05 +0000 | [diff] [blame] | 20 | #include "intrcheck.h" |
| 21 | |
Guido van Rossum | bae9518 | 1997-02-14 21:12:24 +0000 | [diff] [blame] | 22 | /* Copied here from ceval.h -- can't include that file. */ |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 23 | int Py_AddPendingCall(int (*func)(void *), void *arg); |
Guido van Rossum | bae9518 | 1997-02-14 21:12:24 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 26 | #ifdef QUICKWIN |
| 27 | |
| 28 | #include <io.h> |
| 29 | |
| 30 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 31 | PyOS_InitInterrupts(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 35 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 36 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 40 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 41 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 42 | { |
| 43 | _wyield(); |
| 44 | } |
| 45 | |
| 46 | #define OK |
| 47 | |
| 48 | #endif /* QUICKWIN */ |
| 49 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 50 | #if defined(_M_IX86) && !defined(__QNX__) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 51 | #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 | |
| 66 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 67 | PyOS_InitInterrupts(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 68 | { |
| 69 | _go32_want_ctrl_break(1 /* TRUE */); |
| 70 | } |
| 71 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 72 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 73 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 77 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 78 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 79 | { |
| 80 | return _go32_was_ctrl_break_hit(); |
| 81 | } |
| 82 | |
| 83 | #else /* !__GNUC__ */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | /* This might work for MS-DOS (untested though): */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | |
| 87 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 88 | PyOS_InitInterrupts(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 92 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 93 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 94 | { |
| 95 | } |
| 96 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 98 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | { |
| 100 | int interrupted = 0; |
| 101 | while (kbhit()) { |
| 102 | if (getch() == '\003') |
| 103 | interrupted = 1; |
| 104 | } |
| 105 | return interrupted; |
| 106 | } |
| 107 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 108 | #endif /* __GNUC__ */ |
| 109 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | #define OK |
| 111 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 112 | #endif /* MSDOS && !QUICKWIN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | |
| 114 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 115 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | |
Jack Jansen | 9fc3989 | 1995-01-27 14:40:41 +0000 | [diff] [blame] | 117 | /* The Mac interrupt code has moved to macglue.c */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | #define OK |
| 119 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 120 | #endif /* macintosh */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | #ifndef OK |
| 124 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 125 | /* Default version -- for real operating systems and for Standard C */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | |
| 127 | #include <stdio.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 128 | #include <string.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 129 | #include <signal.h> |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 130 | #ifdef HAVE_UNISTD_H |
| 131 | #include <unistd.h> |
| 132 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 62e376b | 1995-01-17 16:11:53 +0000 | [diff] [blame] | 134 | static int interrupted; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame] | 136 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 137 | PyErr_SetInterrupt(void) |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame] | 138 | { |
| 139 | interrupted = 1; |
| 140 | } |
| 141 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 142 | extern int PyErr_CheckSignals(void); |
Guido van Rossum | ad74fa6 | 1997-01-21 06:00:33 +0000 | [diff] [blame] | 143 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 144 | static int |
| 145 | checksignals_witharg(void * arg) |
| 146 | { |
| 147 | return PyErr_CheckSignals(); |
| 148 | } |
| 149 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 150 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 151 | intcatcher(int sig) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 153 | extern void Py_Exit(int); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 154 | 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 Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 164 | Py_Exit(1); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 165 | break; |
| 166 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | signal(SIGINT, intcatcher); |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 168 | Py_AddPendingCall(checksignals_witharg, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 171 | static void (*old_siginthandler)(int) = SIG_DFL; |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 174 | PyOS_InitInterrupts(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 175 | { |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 176 | if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | signal(SIGINT, intcatcher); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 178 | #ifdef HAVE_SIGINTERRUPT |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 179 | /* 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 Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 186 | #endif /* HAVE_SIGINTERRUPT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 189 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 190 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 191 | { |
| 192 | signal(SIGINT, old_siginthandler); |
| 193 | } |
| 194 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 196 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | { |
| 198 | if (!interrupted) |
| 199 | return 0; |
| 200 | interrupted = 0; |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | #endif /* !OK */ |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 205 | |
| 206 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 207 | PyOS_AfterFork(void) |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 208 | { |
| 209 | } |