|
| 1 | +/* This file was removed from newer python versions, so it was |
| 2 | +rescued from a python 2.7 branch... */ |
| 3 | + |
| 4 | +/* Check for interrupts */ |
| 5 | + |
| 6 | +#include "Python.h" |
| 7 | +#include "pythread.h" |
| 8 | + |
| 9 | +#ifdef QUICKWIN |
| 10 | + |
| 11 | +#include <io.h> |
| 12 | + |
| 13 | +void |
| 14 | +PyOS_InitInterrupts(void) |
| 15 | +{ |
| 16 | +} |
| 17 | + |
| 18 | +void |
| 19 | +PyOS_FiniInterrupts(void) |
| 20 | +{ |
| 21 | +} |
| 22 | + |
| 23 | +int |
| 24 | +PyOS_InterruptOccurred(void) |
| 25 | +{ |
| 26 | + _wyield(); |
| 27 | +} |
| 28 | + |
| 29 | +#define OK |
| 30 | + |
| 31 | +#endif /* QUICKWIN */ |
| 32 | + |
| 33 | +#if defined(_M_IX86) && !defined(__QNX__) |
| 34 | +#include <io.h> |
| 35 | +#endif |
| 36 | + |
| 37 | +#if defined(MSDOS) && !defined(QUICKWIN) |
| 38 | + |
| 39 | +#ifdef __GNUC__ |
| 40 | + |
| 41 | +/* This is for DJGPP's GO32 extender. I don't know how to trap |
| 42 | + * control-C (There's no API for ctrl-C, and I don't want to mess with |
| 43 | + * the interrupt vectors.) However, this DOES catch control-break. |
| 44 | + * --Amrit |
| 45 | + */ |
| 46 | + |
| 47 | +#include <go32.h> |
| 48 | + |
| 49 | +void |
| 50 | +PyOS_InitInterrupts(void) |
| 51 | +{ |
| 52 | + _go32_want_ctrl_break(1 /* TRUE */); |
| 53 | +} |
| 54 | + |
| 55 | +void |
| 56 | +PyOS_FiniInterrupts(void) |
| 57 | +{ |
| 58 | +} |
| 59 | + |
| 60 | +int |
| 61 | +PyOS_InterruptOccurred(void) |
| 62 | +{ |
| 63 | + return _go32_was_ctrl_break_hit(); |
| 64 | +} |
| 65 | + |
| 66 | +#else /* !__GNUC__ */ |
| 67 | + |
| 68 | +/* This might work for MS-DOS (untested though): */ |
| 69 | + |
| 70 | +void |
| 71 | +PyOS_InitInterrupts(void) |
| 72 | +{ |
| 73 | +} |
| 74 | + |
| 75 | +void |
| 76 | +PyOS_FiniInterrupts(void) |
| 77 | +{ |
| 78 | +} |
| 79 | + |
| 80 | +int |
| 81 | +PyOS_InterruptOccurred(void) |
| 82 | +{ |
| 83 | + int interrupted = 0; |
| 84 | + while (kbhit()) { |
| 85 | + if (getch() == '\003') |
| 86 | + interrupted = 1; |
| 87 | + } |
| 88 | + return interrupted; |
| 89 | +} |
| 90 | + |
| 91 | +#endif /* __GNUC__ */ |
| 92 | + |
| 93 | +#define OK |
| 94 | + |
| 95 | +#endif /* MSDOS && !QUICKWIN */ |
| 96 | + |
| 97 | + |
| 98 | +#ifndef OK |
| 99 | + |
| 100 | +/* Default version -- for real operating systems and for Standard C */ |
| 101 | + |
| 102 | +#include <stdio.h> |
| 103 | +#include <string.h> |
| 104 | +#include <signal.h> |
| 105 | + |
| 106 | +static int interrupted; |
| 107 | + |
| 108 | +void |
| 109 | +PyErr_SetInterrupt(void) |
| 110 | +{ |
| 111 | + interrupted = 1; |
| 112 | +} |
| 113 | + |
| 114 | +extern int PyErr_CheckSignals(void); |
| 115 | + |
| 116 | +static int |
| 117 | +checksignals_witharg(void * arg) |
| 118 | +{ |
| 119 | + return PyErr_CheckSignals(); |
| 120 | +} |
| 121 | + |
| 122 | +static void |
| 123 | +intcatcher(int sig) |
| 124 | +{ |
| 125 | + extern void Py_Exit(int); |
| 126 | + static char message[] = |
| 127 | +"python: to interrupt a truly hanging Python program, interrupt once more.\n"; |
| 128 | + switch (interrupted++) { |
| 129 | + case 0: |
| 130 | + break; |
| 131 | + case 1: |
| 132 | +#ifdef RISCOS |
| 133 | + fprintf(stderr, message); |
| 134 | +#else |
| 135 | + write(2, message, strlen(message)); |
| 136 | +#endif |
| 137 | + break; |
| 138 | + case 2: |
| 139 | + interrupted = 0; |
| 140 | + Py_Exit(1); |
| 141 | + break; |
| 142 | + } |
| 143 | + PyOS_setsig(SIGINT, intcatcher); |
| 144 | + Py_AddPendingCall(checksignals_witharg, NULL); |
| 145 | +} |
| 146 | + |
| 147 | +static void (*old_siginthandler)(int) = SIG_DFL; |
| 148 | + |
| 149 | +void |
| 150 | +PyOS_InitInterrupts(void) |
| 151 | +{ |
| 152 | + if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN) |
| 153 | + PyOS_setsig(SIGINT, intcatcher); |
| 154 | +} |
| 155 | + |
| 156 | +void |
| 157 | +PyOS_FiniInterrupts(void) |
| 158 | +{ |
| 159 | + PyOS_setsig(SIGINT, old_siginthandler); |
| 160 | +} |
| 161 | + |
| 162 | +int |
| 163 | +PyOS_InterruptOccurred(void) |
| 164 | +{ |
| 165 | + if (!interrupted) |
| 166 | + return 0; |
| 167 | + interrupted = 0; |
| 168 | + return 1; |
| 169 | +} |
| 170 | + |
| 171 | +#endif /* !OK */ |
| 172 | + |
| 173 | +void |
| 174 | +PyOS_AfterFork(void) |
| 175 | +{ |
| 176 | +#ifdef WITH_THREAD |
| 177 | + PyEval_ReInitThreads(); |
| 178 | + PyThread_ReInitTLS(); |
| 179 | +#endif |
| 180 | +} |
0 commit comments