Kurt B. Kaiser | 98b15ab | 2003-03-10 20:41:07 +0000 | [diff] [blame] | 1 | /*********************************************************************** |
| 2 | * interruptmodule.c |
| 3 | * |
| 4 | * Python extension implementing the interrupt module. |
| 5 | * |
| 6 | **********************************************************************/ |
| 7 | |
| 8 | #include "Python.h" |
| 9 | |
| 10 | #ifndef PyDoc_STR |
| 11 | #define PyDoc_VAR(name) static char name[] |
| 12 | #define PyDoc_STR(str) str |
| 13 | #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) |
| 14 | #endif |
| 15 | |
| 16 | /* module documentation */ |
| 17 | |
| 18 | PyDoc_STRVAR(module_doc, |
| 19 | "Provide a way to interrupt the main thread from a subthread.\n\n\ |
| 20 | In threaded Python code the KeyboardInterrupt is always directed to\n\ |
| 21 | the thread which raised it. This extension provides a method,\n\ |
| 22 | interrupt_main, which a subthread can use to raise a KeyboardInterrupt\n\ |
| 23 | in the main thread."); |
| 24 | |
| 25 | /* module functions */ |
| 26 | |
| 27 | static PyObject * |
| 28 | setinterrupt(PyObject * self, PyObject * args) |
| 29 | { |
| 30 | PyErr_SetInterrupt(); |
| 31 | Py_INCREF(Py_None); |
| 32 | return Py_None; |
| 33 | } |
| 34 | |
| 35 | /* registration table */ |
| 36 | |
| 37 | static struct PyMethodDef methods[] = { |
| 38 | {"interrupt_main", setinterrupt, METH_VARARGS, |
| 39 | PyDoc_STR("Interrupt the main thread")}, |
| 40 | {NULL, NULL} |
| 41 | }; |
| 42 | |
| 43 | /* module initialization */ |
| 44 | |
| 45 | void |
| 46 | initinterrupt(void) |
| 47 | { |
| 48 | (void) Py_InitModule3("interrupt", methods, module_doc); |
| 49 | } |