Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Check for interrupts */ |
| 3 | |
Fred Drake | eb375e4 | 2000-08-23 18:17:42 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 6 | #ifdef QUICKWIN |
| 7 | |
| 8 | #include <io.h> |
| 9 | |
| 10 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 11 | PyOS_InitInterrupts(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 12 | { |
| 13 | } |
| 14 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 15 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 16 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 17 | { |
| 18 | } |
| 19 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 20 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 21 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 22 | { |
| 23 | _wyield(); |
| 24 | } |
| 25 | |
| 26 | #define OK |
| 27 | |
| 28 | #endif /* QUICKWIN */ |
| 29 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 30 | #if defined(_M_IX86) && !defined(__QNX__) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 31 | #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 | |
| 46 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 47 | PyOS_InitInterrupts(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 48 | { |
| 49 | _go32_want_ctrl_break(1 /* TRUE */); |
| 50 | } |
| 51 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 52 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 53 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 57 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 58 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 59 | { |
| 60 | return _go32_was_ctrl_break_hit(); |
| 61 | } |
| 62 | |
| 63 | #else /* !__GNUC__ */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | /* This might work for MS-DOS (untested though): */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
| 67 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 68 | PyOS_InitInterrupts(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 69 | { |
| 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 | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 78 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | { |
| 80 | int interrupted = 0; |
| 81 | while (kbhit()) { |
| 82 | if (getch() == '\003') |
| 83 | interrupted = 1; |
| 84 | } |
| 85 | return interrupted; |
| 86 | } |
| 87 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 88 | #endif /* __GNUC__ */ |
| 89 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | #define OK |
| 91 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 92 | #endif /* MSDOS && !QUICKWIN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | |
| 94 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 95 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | |
Jack Jansen | 9fc3989 | 1995-01-27 14:40:41 +0000 | [diff] [blame] | 97 | /* The Mac interrupt code has moved to macglue.c */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | #define OK |
| 99 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 100 | #endif /* macintosh */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | #ifndef OK |
| 104 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 105 | /* Default version -- for real operating systems and for Standard C */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | |
| 107 | #include <stdio.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 108 | #include <string.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | #include <signal.h> |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 110 | #ifdef HAVE_UNISTD_H |
| 111 | #include <unistd.h> |
| 112 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 62e376b | 1995-01-17 16:11:53 +0000 | [diff] [blame] | 114 | static int interrupted; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame] | 116 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 117 | PyErr_SetInterrupt(void) |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame] | 118 | { |
| 119 | interrupted = 1; |
| 120 | } |
| 121 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 122 | extern int PyErr_CheckSignals(void); |
Guido van Rossum | ad74fa6 | 1997-01-21 06:00:33 +0000 | [diff] [blame] | 123 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 124 | static int |
| 125 | checksignals_witharg(void * arg) |
| 126 | { |
| 127 | return PyErr_CheckSignals(); |
| 128 | } |
| 129 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 130 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 131 | intcatcher(int sig) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 133 | extern void Py_Exit(int); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 134 | 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 Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 140 | #ifdef RISCOS |
| 141 | fprintf(stderr, message); |
| 142 | #else |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 143 | write(2, message, strlen(message)); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 144 | #endif |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 145 | break; |
| 146 | case 2: |
| 147 | interrupted = 0; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 148 | Py_Exit(1); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 149 | break; |
| 150 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 151 | signal(SIGINT, intcatcher); |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 152 | Py_AddPendingCall(checksignals_witharg, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 155 | static void (*old_siginthandler)(int) = SIG_DFL; |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 158 | PyOS_InitInterrupts(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | { |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 160 | if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | signal(SIGINT, intcatcher); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 162 | #ifdef HAVE_SIGINTERRUPT |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 163 | /* 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 Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 170 | #endif /* HAVE_SIGINTERRUPT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 173 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 174 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 175 | { |
| 176 | signal(SIGINT, old_siginthandler); |
| 177 | } |
| 178 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 180 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | { |
| 182 | if (!interrupted) |
| 183 | return 0; |
| 184 | interrupted = 0; |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | #endif /* !OK */ |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 189 | |
| 190 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 191 | PyOS_AfterFork(void) |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 192 | { |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 193 | #ifdef WITH_THREAD |
| 194 | PyEval_ReInitThreads(); |
| 195 | #endif |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 196 | } |