8000 bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module · python/cpython@fdd38cb · GitHub
[go: up one dir, main page]

Skip to content

Commit fdd38cb

Browse files
committed
bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module
1 parent 5081e78 commit fdd38cb

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`signal` module no longer assumes that :const:`~signal.SIG_IGN` and
2+
:const:`~signal.SIG_DFL` are small int singletons.

Modules/signalmodule.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
496496
_signal_module_state *modstate = get_signal_state(module);
497497
PyObject *old_handler;
498498
void (*func)(int);
499+
int match = 0; // cannot use func == NULL as sentinel, SIG_DFL == 0
499500
#ifdef MS_WINDOWS
500501
/* Validate that signalnum is one of the allowable signals */
501502
switch (signalnum) {
@@ -528,21 +529,38 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
528529
"signal number out of range");
529530
return NULL;
530531
}
531-
if (handler == modstate->ignore_handler) {
532-
func = SIG_IGN;
532+
if (PyCallable_Check(handler)) {
533+
func = signal_handler;
534+
match = 1;
533535
}
534-
else if (handler == modstate->default_handler) {
535-
func = SIG_DFL;
536+
if (!match) {
537+
int cmp = PyObject_RichCompareBool(handler, modstate->ignore_handler, Py_EQ);
538+
switch (cmp) {
539+
case -1:
540+
return NULL;
541+
case 1:
542+
func = SIG_IGN;
543+
match = 1;
544+
break;
545+
}
536546
}
537-
else if (!PyCallable_Check(handler)) {
547+
if (!match) {
548+
int cmp = PyObject_RichCompareBool(handler, modstate->default_handler, Py_EQ);
549+
switch (cmp) {
550+
case -1:
551+
return NULL;
552+
case 1:
553+
func = SIG_DFL;
554+
match = 1;
555+
break;
556+
}
557+
}
558+
if (!match) {
538559
_PyErr_SetString(tstate, PyExc_TypeError,
539560
"signal handler must be signal.SIG_IGN, "
540561
"signal.SIG_DFL, or a callable object");
541562
return NULL;
542563
}
543-
else {
544-
func = signal_handler;
545-
}
546564

547565
/* Check for pending signals before changing signal handler */
548566
if (_PyErr_CheckSignalsTstate(tstate)) {

0 commit comments

Comments
 (0)
0