8000 bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module by tiran · Pull Request #31759 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module #31759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module
  • Loading branch information
tiran committed Mar 8, 2022
commit fdd38cb16aea0aa44265a5b9395f8fe9bf56db7b
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 26 additions & 8 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down
0