diff options
author | Antoine Pitrou <antoine@python.org> | 2021-03-11 22:35:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-11 22:35:45 (GMT) |
commit | ba251c2ae6654bfc8abd9d886b219698ad34ac3c (patch) | |
tree | e536a5ba9f661c806ffaa507e2ca4cf1be551b5a /Modules/_threadmodule.c | |
parent | b4fc44bb2d209182390b4f9fdf074a46b0165a2f (diff) | |
download | cpython-ba251c2ae6654bfc8abd9d886b219698ad34ac3c.zip cpython-ba251c2ae6654bfc8abd9d886b219698ad34ac3c.tar.gz cpython-ba251c2ae6654bfc8abd9d886b219698ad34ac3c.tar.bz2 |
bpo-43356: Allow passing a signal number to interrupt_main() (GH-24755)
Also introduce a new C API ``PyErr_SetInterruptEx(int signum)``.
Diffstat (limited to 'Modules/_threadmodule.c')
-rw-r--r-- | Modules/_threadmodule.c | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index f6217e6..0613dfd 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -9,6 +9,10 @@ #include <stddef.h> // offsetof() #include "structmember.h" // PyMemberDef +#ifdef HAVE_SIGNAL_H +# include <signal.h> // SIGINT +#endif + // ThreadError is just an alias to PyExc_RuntimeError #define ThreadError PyExc_RuntimeError @@ -1173,17 +1177,29 @@ This is synonymous to ``raise SystemExit''. It will cause the current\n\ thread to exit silently unless the exception is caught."); static PyObject * -thread_PyThread_interrupt_main(PyObject * self, PyObject *Py_UNUSED(ignored)) +thread_PyThread_interrupt_main(PyObject *self, PyObject *args) { - PyErr_SetInterrupt(); + int signum = SIGINT; + if (!PyArg_ParseTuple(args, "|i:signum", &signum)) { + return NULL; + } + + if (PyErr_SetInterruptEx(signum)) { + PyErr_SetString(PyExc_ValueError, "signal number out of range"); + return NULL; + } Py_RETURN_NONE; } PyDoc_STRVAR(interrupt_doc, -"interrupt_main()\n\ +"interrupt_main(signum=signal.SIGINT, /)\n\ +\n\ +Simulate the arrival of the given signal in the main thread,\n\ +where the corresponding signal handler will be executed.\n\ +If *signum* is omitted, SIGINT is assumed.\n\ +A subthread can use this function to interrupt the main thread.\n\ \n\ -Raise a KeyboardInterrupt in the main thread.\n\ -A subthread can use this function to interrupt the main thread." +Note: the default signal hander for SIGINT raises ``KeyboardInterrupt``." ); static lockobject *newlockobject(PyObject *module); @@ -1527,8 +1543,8 @@ static PyMethodDef thread_methods[] = { METH_NOARGS, exit_doc}, {"exit", thread_PyThread_exit_thread, METH_NOARGS, exit_doc}, - {"interrupt_main", thread_PyThread_interrupt_main, - METH_NOARGS, interrupt_doc}, + {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, + METH_VARARGS, interrupt_doc}, {"get_ident", thread_get_ident, METH_NOARGS, get_ident_doc}, #ifdef PY_HAVE_THREAD_NATIVE_ID |