diff -r 2363e71757cc c_lib/include/interrupt.h
a
|
b
|
|
26 | 26 | to get signal handling capabilities. |
27 | 27 | |
28 | 28 | VERY VERY IMPORTANT: |
29 | | 1. These *must* always come in pairs. E.g., if you have just |
30 | | a _sig_off without a corresponding _sig_on, then ctrl-c |
31 | | later in the interpreter will sigfault! |
| 29 | 1. These *must* always come in pairs. |
32 | 30 | |
33 | 31 | 2. Do *not* put these in the __init__ method of a Pyrex extension |
34 | 32 | class, or you'll get crashes. |
35 | 33 | |
36 | 34 | 3. Do not put these around any non-pure C code!!!! |
37 | | |
38 | | 4. If you need to do a check of control-c inside a loop, e.g., |
39 | | when constructing a matrix, put _sig_check instead of using |
40 | | _sig_on then _sig_off. |
41 | 35 | */ |
42 | 36 | |
43 | 37 | /** |
… |
… |
|
164 | 158 | |
165 | 159 | /** |
166 | 160 | * Enables SAGE signal handling for the following C block. This macro |
167 | | * *MUST* be followed by _sig_off. |
| 161 | * *MUST* be followed by ``_sig_off``. |
168 | 162 | * |
| 163 | * INPUT: |
169 | 164 | * |
170 | | * See also @ref _sig_str |
| 165 | * - ``str`` - a string to be displayed as error message when the code |
| 166 | * between ``_sig_on`` and ``_sig_off`` fails. |
| 167 | * |
| 168 | * - ``except`` - the value to return when an exception is raised. |
| 169 | * This should correspond to the "except" value in the Cython |
| 170 | * declaration of the function. For example, the function |
| 171 | * cdef int my_cython_function() except -1: |
| 172 | * should use ``except`` = -1. |
| 173 | * A function which return a Python object should NOT have an except |
| 174 | * declaration and should use ``except`` = 0: |
| 175 | * cdef function_returning_a_python_object(): |
| 176 | * |
| 177 | * This is the most general macro, in practice you must use one of the |
| 178 | * shortcuts macros like ``_sig_on`` defined later. |
| 179 | * |
171 | 180 | */ |
172 | | |
173 | | /* The function sigsetjmp() below can return the following: |
| 181 | #define _sig_on_str_except(str, except) \ |
| 182 | if (_signals.mpio == 0) { \ |
| 183 | _signals.mpio = 1+2; _signals.s = str;\ |
| 184 | if (sigsetjmp(_signals.env,1) > 0) { \ |
| 185 | _signals.mpio = 0; \ |
| 186 | return(except); \ |
| 187 | } } |
| 188 | /* The function sigsetjmp() in the macro above can return the following: |
174 | 189 | * - zero: this happens in the actual _sig_on call and sets up the |
175 | 190 | * address for the Sage signal handler to jump to. The program |
176 | 191 | * continues normally. |
… |
… |
|
183 | 198 | * this case, the program continues as if nothing happened between |
184 | 199 | * _sig_on and _sig_retry. |
185 | 200 | */ |
186 | | #define _sig_on if (_signals.mpio == 0) { _signals.mpio = 1+2; _signals.s = NULL;\ |
187 | | if (sigsetjmp(_signals.env,1) > 0) { \ |
188 | | _signals.mpio = 0; \ |
189 | | return(0); \ |
190 | | } } // else { _signals.s = "Unbalanced _sig_on/_sig_off\n"; fprintf(stderr, _signals.s); sage_signal_handler(SIGABRT); } |
191 | 201 | |
192 | | /* /\** */ |
193 | | /* * Enables SAGE signal handling for the following C block. This macro */ |
194 | | /* * *MUST* be followed by _sig_off_short. */ |
195 | | /* * */ |
196 | | /* * If the following block takes very little time to compute this macro */ |
197 | | /* * is the right choice. Otherwise _sig_on is appropriate. */ |
198 | | /* * */ |
199 | | /* * See also _sig_on and _sig_str. */ |
200 | | /* * */ |
201 | | /* *\/ */ |
202 | 202 | |
203 | | /* #define _sig_on_short _signals.mpio = 1 */ |
204 | | |
205 | | |
206 | | /** |
207 | | * Same as @ref _sig_on but with string support |
208 | | * |
209 | | */ |
210 | | |
211 | | #define _sig_str(mstring) if (_signals.mpio == 0) { _signals.mpio = 1+2; \ |
212 | | _signals.s = mstring; \ |
213 | | if (sigsetjmp(_signals.env,1) > 0) { \ |
214 | | _signals.mpio = 0; \ |
215 | | return(0); \ |
216 | | } } //else { _signals.s = "Unbalanced _sig_str/_sig_off\n"; fprintf(stderr, _signals.s); sage_signal_handler(SIGABRT); } |
| 203 | /* Some very useful shortcuts. |
| 204 | * Note that we need to define the except values with a type in |
| 205 | * sage/ext/interrupt.pxi, but we can define the macros the same |
| 206 | * regardless of the type. */ |
| 207 | #define _sig_on _sig_on_str_except(NULL, 0) |
| 208 | #define _sig_str(str) _sig_on_str_except(str, 0) |
| 209 | #define _sig_on_except_int(except) _sig_on_str_except(NULL, except) |
| 210 | #define _sig_str_except_int(str, except) _sig_on_str_except(str, except) |
217 | 211 | |
218 | 212 | |
219 | 213 | /* |
… |
… |
|
229 | 223 | #define SAGE_SIGNAL_HANDLER_MESSAGE_LEN 256 |
230 | 224 | |
231 | 225 | |
232 | | /** |
233 | | * |
234 | | * |
235 | | */ |
236 | | |
237 | 226 | #define _sig_off _signals.mpio = 0; |
238 | 227 | |
239 | 228 | |
240 | | /* /\** */ |
241 | | /* * */ |
242 | | /* * */ |
243 | | /* *\/ */ |
244 | | |
245 | | /* #define _sig_off_short (_signals.mpio & 4) */ |
246 | | |
247 | | |
248 | | /** |
249 | | * |
250 | | * |
251 | | */ |
252 | | |
253 | | #define _sig_check _sig_on _sig_off |
254 | 229 | |
255 | 230 | /* Retry a failed computation starting from the last _sig_on. The |
256 | 231 | * program will continue as if nothing happened between _sig_on and |
diff -r 2363e71757cc c_lib/src/interrupt.c
a
|
b
|
|
12 | 12 | #include "stdsage.h" |
13 | 13 | #include "interrupt.h" |
14 | 14 | #include <stdio.h> |
| 15 | #include <execinfo.h> |
15 | 16 | |
16 | 17 | |
17 | 18 | char sage_signal_handler_message[SAGE_SIGNAL_HANDLER_MESSAGE_LEN + 1] = ""; |
… |
… |
|
29 | 30 | void msg(char* s); |
30 | 31 | |
31 | 32 | void sage_signal_handler(int sig) { |
| 33 | void* backtracebuffer[1024]; |
32 | 34 | |
33 | 35 | char *s = _signals.s; |
34 | 36 | _signals.s = NULL; |
… |
… |
|
38 | 40 | { |
39 | 41 | s = sage_signal_handler_message; |
40 | 42 | } |
| 43 | |
| 44 | if (sig == SIGINT) |
| 45 | { |
| 46 | fprintf(stderr, "\n*** SIGINT *** %s _sig_on\n", (_signals.mpio & 1) ? "inside" : "outside"); |
| 47 | int btsize = backtrace(backtracebuffer, 1024); |
| 48 | backtrace_symbols_fd(backtracebuffer, btsize, 2); |
| 49 | fflush(stderr); |
| 50 | } |
41 | 51 | |
42 | 52 | //we override the default handler |
43 | 53 | if ( _signals.mpio & 1 ) { |
… |
… |
|
104 | 114 | break; |
105 | 115 | default: |
106 | 116 | _signals.python_handler(sig); |
| 117 | fprintf(stderr, "*** Python signal handler finished\n"); fflush(stderr); |
107 | 118 | break; |
108 | 119 | }; |
109 | 120 | |
diff -r 2363e71757cc sage/ext/interrupt.pxi
a
|
b
|
|
5 | 5 | ################################################################ |
6 | 6 | |
7 | 7 | cdef extern from 'interrupt.h': |
8 | | int _sig_on, _sig_off, _sig_check, _sig_retry |
9 | | void _sig_str(char*) |
| 8 | int _sig_on |
| 9 | int _sig_str(char*) |
| 10 | int _sig_on_except_int(int) |
| 11 | int _sig_str_except_int(char*, int) |
| 12 | int _sig_off, _sig_retry |
diff -r 2363e71757cc sage/libs/singular/polynomial.pyx
a
|
b
|
|
314 | 314 | if(r != currRing): rChangeCurrRing(r) |
315 | 315 | cdef int count = singular_polynomial_length_bounded(p,15) |
316 | 316 | if count >= 15 or exp > 15: |
317 | | _sig_on |
| 317 | _sig_on_except_int(-1) |
318 | 318 | ret[0] = pPower( p_Copy(p,r), exp) |
319 | 319 | if count >= 15 or exp > 15: |
320 | 320 | _sig_off |