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 | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | #ifndef OK |
| 96 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 97 | /* Default version -- for real operating systems and for Standard C */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | |
| 99 | #include <stdio.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 100 | #include <string.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | #include <signal.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | 62e376b | 1995-01-17 16:11:53 +0000 | [diff] [blame] | 103 | static int interrupted; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame] | 105 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 106 | PyErr_SetInterrupt(void) |
Guido van Rossum | 575d561 | 1995-03-10 15:13:03 +0000 | [diff] [blame] | 107 | { |
| 108 | interrupted = 1; |
| 109 | } |
| 110 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 111 | extern int PyErr_CheckSignals(void); |
Guido van Rossum | ad74fa6 | 1997-01-21 06:00:33 +0000 | [diff] [blame] | 112 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 113 | static int |
| 114 | checksignals_witharg(void * arg) |
| 115 | { |
| 116 | return PyErr_CheckSignals(); |
| 117 | } |
| 118 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 119 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 120 | intcatcher(int sig) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 122 | extern void Py_Exit(int); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 123 | static char message[] = |
| 124 | "python: to interrupt a truly hanging Python program, interrupt once more.\n"; |
| 125 | switch (interrupted++) { |
| 126 | case 0: |
| 127 | break; |
| 128 | case 1: |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 129 | #ifdef RISCOS |
| 130 | fprintf(stderr, message); |
| 131 | #else |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 132 | write(2, message, strlen(message)); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 133 | #endif |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 134 | break; |
| 135 | case 2: |
| 136 | interrupted = 0; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 137 | Py_Exit(1); |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 140 | PyOS_setsig(SIGINT, intcatcher); |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 141 | Py_AddPendingCall(checksignals_witharg, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 144 | static void (*old_siginthandler)(int) = SIG_DFL; |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 147 | PyOS_InitInterrupts(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 149 | if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN) |
| 150 | PyOS_setsig(SIGINT, intcatcher); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 153 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 154 | PyOS_FiniInterrupts(void) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 155 | { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 156 | PyOS_setsig(SIGINT, old_siginthandler); |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 160 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | { |
| 162 | if (!interrupted) |
| 163 | return 0; |
| 164 | interrupted = 0; |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | #endif /* !OK */ |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 169 | |
| 170 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 171 | PyOS_AfterFork(void) |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 172 | { |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 173 | #ifdef WITH_THREAD |
| 174 | PyEval_ReInitThreads(); |
| 175 | #endif |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 176 | } |