mirror of
https://github.com/python/cpython.git
synced 2025-08-23 10:16:01 +00:00
bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module (GH-31759)
This commit is contained in:
parent
5498a61c7c
commit
c8a47e76a3
2 changed files with 25 additions and 13 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
The :mod:`signal` module no longer assumes that :const:`~signal.SIG_IGN` and
|
||||||
|
:const:`~signal.SIG_DFL` are small int singletons.
|
|
@ -177,6 +177,17 @@ get_signal_state(PyObject *module)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
compare_handler(PyObject *func, PyObject *dfl_ign_handler)
|
||||||
|
{
|
||||||
|
assert(PyLong_CheckExact(dfl_ign_handler));
|
||||||
|
if (!PyLong_CheckExact(func)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Assume that comparison of two PyLong objects will never fail.
|
||||||
|
return PyObject_RichCompareBool(func, dfl_ign_handler, Py_EQ) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef HAVE_GETITIMER
|
#ifdef HAVE_GETITIMER
|
||||||
/* auxiliary functions for setitimer */
|
/* auxiliary functions for setitimer */
|
||||||
static int
|
static int
|
||||||
|
@ -528,21 +539,18 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
|
||||||
"signal number out of range");
|
"signal number out of range");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (handler == modstate->ignore_handler) {
|
if (PyCallable_Check(handler)) {
|
||||||
|
func = signal_handler;
|
||||||
|
} else if (compare_handler(handler, modstate->ignore_handler)) {
|
||||||
func = SIG_IGN;
|
func = SIG_IGN;
|
||||||
}
|
} else if (compare_handler(handler, modstate->default_handler)) {
|
||||||
else if (handler == modstate->default_handler) {
|
|
||||||
func = SIG_DFL;
|
func = SIG_DFL;
|
||||||
}
|
} else {
|
||||||
else if (!PyCallable_Check(handler)) {
|
|
||||||
_PyErr_SetString(tstate, PyExc_TypeError,
|
_PyErr_SetString(tstate, PyExc_TypeError,
|
||||||
"signal handler must be signal.SIG_IGN, "
|
"signal handler must be signal.SIG_IGN, "
|
||||||
"signal.SIG_DFL, or a callable object");
|
"signal.SIG_DFL, or a callable object");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
func = signal_handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check for pending signals before changing signal handler */
|
/* Check for pending signals before changing signal handler */
|
||||||
if (_PyErr_CheckSignalsTstate(tstate)) {
|
if (_PyErr_CheckSignalsTstate(tstate)) {
|
||||||
|
@ -1752,8 +1760,8 @@ _PySignal_Fini(void)
|
||||||
set_handler(signum, NULL);
|
set_handler(signum, NULL);
|
||||||
if (func != NULL
|
if (func != NULL
|
||||||
&& func != Py_None
|
&& func != Py_None
|
||||||
&& func != state->default_handler
|
&& !compare_handler(func, state->default_handler)
|
||||||
&& func != state->ignore_handler)
|
&& !compare_handler(func, state->ignore_handler))
|
||||||
{
|
{
|
||||||
PyOS_setsig(signum, SIG_DFL);
|
PyOS_setsig(signum, SIG_DFL);
|
||||||
}
|
}
|
||||||
|
@ -1824,8 +1832,9 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate)
|
||||||
* (see bpo-43406).
|
* (see bpo-43406).
|
||||||
*/
|
*/
|
||||||
PyObject *func = get_handler(i);
|
PyObject *func = get_handler(i);
|
||||||
if (func == NULL || func == Py_None || func == state->ignore_handler ||
|
if (func == NULL || func == Py_None ||
|
||||||
func == state->default_handler) {
|
compare_handler(func, state->ignore_handler) ||
|
||||||
|
compare_handler(func, state->default_handler)) {
|
||||||
/* No Python signal handler due to aforementioned race condition.
|
/* No Python signal handler due to aforementioned race condition.
|
||||||
* We can't call raise() as it would break the assumption
|
* We can't call raise() as it would break the assumption
|
||||||
* that PyErr_SetInterrupt() only *simulates* an incoming
|
* that PyErr_SetInterrupt() only *simulates* an incoming
|
||||||
|
@ -1893,7 +1902,8 @@ PyErr_SetInterruptEx(int signum)
|
||||||
|
|
||||||
signal_state_t *state = &signal_global_state;
|
signal_state_t *state = &signal_global_state;
|
||||||
PyObject *func = get_handler(signum);
|
PyObject *func = get_handler(signum);
|
||||||
if (func != state->ignore_handler && func != state->default_handler) {
|
if (!compare_handler(func, state->ignore_handler)
|
||||||
|
&& !compare_handler(func, state->default_handler)) {
|
||||||
trip_signal(signum);
|
trip_signal(signum);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue