From fdd38cb16aea0aa44265a5b9395f8fe9bf56db7b Mon Sep 17 00:00:00 2001
From: Christian Heimes <christian@python.org>
Date: Tue, 8 Mar 2022 11:34:41 +0100
Subject: [PATCH 1/4] bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in
 signal module

---
 .../2022-03-08-11-34-06.bpo-23325.3VQnfo.rst  |  2 ++
 Modules/signalmodule.c                        | 34 ++++++++++++++-----
 2 files changed, 28 insertions(+), 8 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Library/2022-03-08-11-34-06.bpo-23325.3VQnfo.rst

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 00000000000000..0801cbb4482256
--- /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 26a1f48470ed23..20518bbd9a8217 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -496,6 +496,7 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
     _signal_module_state *modstate = get_signal_state(module);
     PyObject *old_handler;
     void (*func)(int);
+    int match = 0; // cannot use func == NULL as sentinel, SIG_DFL == 0
 #ifdef MS_WINDOWS
     /* Validate that signalnum is one of the allowable signals */
     switch (signalnum) {
@@ -528,21 +529,38 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
                          "signal number out of range");
         return NULL;
     }
-    if (handler == modstate->ignore_handler) {
-        func = SIG_IGN;
+    if (PyCallable_Check(handler)) {
+        func = signal_handler;
+        match = 1;
     }
-    else if (handler == modstate->default_handler) {
-        func = SIG_DFL;
+    if (!match) {
+        int cmp = PyObject_RichCompareBool(handler, modstate->ignore_handler, Py_EQ);
+        switch (cmp) {
+            case -1:
+                return NULL;
+            case 1:
+                func = SIG_IGN;
+                match = 1;
+                break;
+        }
     }
-    else if (!PyCallable_Check(handler)) {
+    if (!match) {
+        int cmp = PyObject_RichCompareBool(handler, modstate->default_handler, Py_EQ);
+        switch (cmp) {
+            case -1:
+                return NULL;
+            case 1:
+                func = SIG_DFL;
+                match = 1;
+                break;
+        }
+    }
+    if (!match) {
         _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)) {

From 34381041b9c92b78dfe7f76c3bca652b5a315597 Mon Sep 17 00:00:00 2001
From: Christian Heimes <christian@python.org>
Date: Tue, 8 Mar 2022 12:32:05 +0100
Subject: [PATCH 2/4] Fix comparison in PyErr_SetInterruptEx()

---
 Modules/signalmodule.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 20518bbd9a8217..efe522517107ea 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1911,7 +1911,15 @@ 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) {
+    int is_ign = PyObject_RichCompareBool(func, state->ignore_handler, Py_EQ);
+    if (is_ign == -1) {
+        return -1;
+    }
+    int is_dfl = PyObject_RichCompareBool(func, state->default_handler, Py_EQ);
+    if (is_dfl == -1) {
+        return -1;
+    }
+    if ((is_ign == 0) && (is_dfl == 0)) {
         trip_signal(signum);
     }
     return 0;

From cc4df0594e134e262c8e9a5191f6a1fa20baeee8 Mon Sep 17 00:00:00 2001
From: Christian Heimes <christian@python.org>
Date: Tue, 8 Mar 2022 17:27:26 +0100
Subject: [PATCH 3/4] Compare handler in a safe helper function

---
 Modules/signalmodule.c | 62 ++++++++++++++++--------------------------
 1 file changed, 23 insertions(+), 39 deletions(-)

diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index efe522517107ea..be42ddac08a18c 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -177,6 +177,17 @@ get_signal_state(PyObject *module)
 }
 
 
+static inline int
+compare_handler(PyObject *handler, PyObject *func)
+{
+    assert(PyLong_CheckExact(handler));
+    if (!PyLong_CheckExact(func)) {
+        return 0;
+    }
+    // Assume that comparison of two PyLong objects will never fail.
+    return PyObject_RichCompareBool(func, handler, Py_EQ) == 1;
+}
+
 #ifdef HAVE_GETITIMER
 /* auxiliary functions for setitimer */
 static int
@@ -496,7 +507,6 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
     _signal_module_state *modstate = get_signal_state(module);
     PyObject *old_handler;
     void (*func)(int);
-    int match = 0; // cannot use func == NULL as sentinel, SIG_DFL == 0
 #ifdef MS_WINDOWS
     /* Validate that signalnum is one of the allowable signals */
     switch (signalnum) {
@@ -531,31 +541,11 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
     }
     if (PyCallable_Check(handler)) {
         func = signal_handler;
-        match = 1;
-    }
-    if (!match) {
-        int cmp = PyObject_RichCompareBool(handler, modstate->ignore_handler, Py_EQ);
-        switch (cmp) {
-            case -1:
-                return NULL;
-            case 1:
-                func = SIG_IGN;
-                match = 1;
-                break;
-        }
-    }
-    if (!match) {
-        int cmp = PyObject_RichCompareBool(handler, modstate->default_handler, Py_EQ);
-        switch (cmp) {
-            case -1:
-                return NULL;
-            case 1:
-                func = SIG_DFL;
-                match = 1;
-                break;
-        }
-    }
-    if (!match) {
+    } else if (compare_handler(handler, modstate->ignore_handler)) {
+        func = SIG_IGN;
+    } else if (compare_handler(handler, modstate->default_handler)) {
+        func = SIG_DFL;
+    } else {
         _PyErr_SetString(tstate, PyExc_TypeError,
                          "signal handler must be signal.SIG_IGN, "
                          "signal.SIG_DFL, or a callable object");
@@ -1770,8 +1760,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);
         }
@@ -1842,8 +1832,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
@@ -1911,15 +1902,8 @@ PyErr_SetInterruptEx(int signum)
 
     signal_state_t *state = &signal_global_state;
     PyObject *func = get_handler(signum);
-    int is_ign = PyObject_RichCompareBool(func, state->ignore_handler, Py_EQ);
-    if (is_ign == -1) {
-        return -1;
-    }
-    int is_dfl = PyObject_RichCompareBool(func, state->default_handler, Py_EQ);
-    if (is_dfl == -1) {
-        return -1;
-    }
-    if ((is_ign == 0) && (is_dfl == 0)) {
+    if (!compare_handler(func, state->ignore_handler)
+            && !compare_handler(func, state->default_handler)) {
         trip_signal(signum);
     }
     return 0;

From 76e31353d9bb373640b0baad187a0abd3ad5dfcf Mon Sep 17 00:00:00 2001
From: Christian Heimes <christian@python.org>
Date: Tue, 8 Mar 2022 17:38:23 +0100
Subject: [PATCH 4/4] Use right order of arguments

---
 Modules/signalmodule.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index be42ddac08a18c..9566263a0dd87e 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -178,14 +178,14 @@ get_signal_state(PyObject *module)
 
 
 static inline int
-compare_handler(PyObject *handler, PyObject *func)
+compare_handler(PyObject *func, PyObject *dfl_ign_handler)
 {
-    assert(PyLong_CheckExact(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, handler, Py_EQ) == 1;
+    return PyObject_RichCompareBool(func, dfl_ign_handler, Py_EQ) == 1;
 }
 
 #ifdef HAVE_GETITIMER