[Python-checkins] bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module (GH-31759)

miss-islington webhook-mailer at python.org
Tue Mar 8 13:53:33 EST 2022


https://github.com/python/cpython/commit/95b001fe6766f491f4356f8bcf23d6895bab2342
commit: 95b001fe6766f491f4356f8bcf23d6895bab2342
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022年03月08日T10:53:24-08:00
summary:
bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module (GH-31759)
(cherry picked from commit c8a47e76a391c8818bf10a282cdcd3bb5c23ebf6)
Co-authored-by: Christian Heimes <christian at python.org>
files:
A Misc/NEWS.d/next/Library/2022-03-08-11-34-06.bpo-23325.3VQnfo.rst
M Modules/signalmodule.c
diff --git a/Misc/NEWS.d/next/Library/2022-03-08-11-34-06.bpo-23325.3VQnfo.rst b/Misc/NEWS.d/next/Library/2022-03-08-11-34-06.bpo-23325.3VQnfo.rst
new file mode 100644
index 0000000000000..0801cbb448225
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-03-08-11-34-06.bpo-23325.3VQnfo.rst
@@ -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.
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 96881d4a49f10..c3a5237bce434 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -174,6 +174,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
 /* auxiliary functions for setitimer */
 static int
@@ -525,21 +536,18 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
 "signal number out of range");
 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;
- }
- else if (handler == modstate->default_handler) {
+ } else if (compare_handler(handler, modstate->default_handler)) {
 func = SIG_DFL;
- }
- else if (!PyCallable_Check(handler)) {
+ } else {
 _PyErr_SetString(tstate, PyExc_TypeError,
 "signal handler must be signal.SIG_IGN, "
 "signal.SIG_DFL, or a callable object");
 return NULL;
 }
- else {
- func = signal_handler;
- }
 
 /* Check for pending signals before changing signal handler */
 if (_PyErr_CheckSignalsTstate(tstate)) {
@@ -1736,8 +1744,8 @@ _PySignal_Fini(void)
 set_handler(signum, NULL);
 if (func != NULL
 && func != Py_None
- && func != state->default_handler
- && func != state->ignore_handler)
+ && !compare_handler(func, state->default_handler)
+ && !compare_handler(func, state->ignore_handler))
 {
 PyOS_setsig(signum, SIG_DFL);
 }
@@ -1812,8 +1820,9 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate)
 * (see bpo-43406).
 */
 PyObject *func = get_handler(i);
- if (func == NULL || func == Py_None || func == state->ignore_handler ||
- func == state->default_handler) {
+ if (func == NULL || func == Py_None ||
+ compare_handler(func, state->ignore_handler) ||
+ compare_handler(func, state->default_handler)) {
 /* No Python signal handler due to aforementioned race condition.
 * We can't call raise() as it would break the assumption
 * that PyErr_SetInterrupt() only *simulates* an incoming
@@ -1873,7 +1882,8 @@ PyErr_SetInterruptEx(int signum)
 
 signal_state_t *state = &signal_global_state;
 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);
 }
 return 0;


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /