-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Turn SIG_DFL and SIG_IGN into functions #67514
Comments
In C the SIG_DFL and SIG_IGN macros expand into integral expressions that are not equal to an address of any function. In Python they are int objects with platform depended values. Second argument of the signal() function should be SIG_DFL, SIG_IGN, or a function. The getsignal() function returns SIG_DFL, SIG_IGN, None, or a function. They are tested for identity in signal() and getsignal() returns identical values. So actually SIG_DFL and SIG_IGN are just singletons. I propose to turn SIG_DFL and SIG_IGN into functions. Benefits:
This patch depends on the backout of turning these constants to enums (bpo-21076). |
A private method is being added to Enum to better support Enum replacement of constants, part of which includes changing __reduce_ex__ to return the string of the name. These changes answer points 1 and 4. Point 2 would be nice, but seems somewhat less important if Enums are being used. Point 3 -- I don't see how 'Signal(xxx, yyy)' is more complicated than 'Signal(xxx, zzz)'? |
Currently the action parameter of signal.signal() can be SIG_DFL, SIG_IGN, or a callable Python object. Would be just a callable Python object. This is simpler. The main benefit is that they will be better look in pydoc output. |
Thanks for the explanation, point taken. However, it seems to me that changing the type of the constants from 'int' to 'function' is backwards incompatible, will break code, and is probably not worth it (and would require a deprecation period if it was worth it). I still think the better answer is to delve into Modules/signalmodule.c and fix the comparisons. |
I'm afraid this could break old codes so not sure it's worth or not. I've seen code like |
Numerical values of signal numbers are standardized. signal.signal(15, handler) is valid. SIGTERM is just a handy name for the constant 15. But SIG_DFL and SIG_IGN are not integers in C (they are special pointers), and it is not declared anywhere that they should be integers in Python. The code that converts them to Python integer is implementation depending (PyLong_FromVoidPtr). Handcrafted SIG_DFL and SIG_IGN are broken by design, because the signal module uses identity comparison for SIG_DFL and SIG_IGN. |
I miss that. So this change seems to be user transparent and make more sense then. |
Serhiy, could you please rebase your PR to tip of main branch? I'd like to try it out. |
It may take a time, because the module initialization code has been completely rewritten. |
Understood, thanks! A trivial fix for the integer comparison bug would solve my issue: --- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -527,21 +527,20 @@ 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 (PyObject_RichCompareBool(handler, modstate->ignore_handler, Py_EQ) == 1) {
func = SIG_IGN;
}
- else if (handler == modstate->default_handler) {
+ else if (PyObject_RichCompareBool(handler, modstate->default_handler, Py_EQ) == 1) {
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)) { |
My PR 31759 removes the assumption of small int singletons and replaces C comparison with PyObject_RichCompareBool() Py_EQ calls. I still prefer Serhiy's solution, but it may cause backwards incompatible breakage. My fix can be backported to 3.9 and 3.10 without breakage. It resolves my failing tests on Emscripten, too. |
Agree. There were too many changes in this code, and making SIG_DFL and SIG_IGN functions exposes some issues with sharing objects between interpreters. It is easier to keep them integers for now. |
Do we still want to do this? That said, I don't think they should be changed. Counterpoints to the rationale:
I recommend closing. |
I agree to close the issue. I close it. If tomorrow someone comes with a stronger use case, I can change my mind :-) Anyway, @serhiy-storchaka, it was interesting to explore this idea. Maybe if it was done 10 or 15 years ago, it would make more sense. Today, we have to learn with these special cases. If I would be ok with replacing
I'm not convinced that these problems are important enough compared to drawbacks. I don't think that it's common to serialize/deserialize signal handlers! If your program does that, add special a case in your problem, no? At the C level, these values are special function pointers: /usr/include/bits/signum-generic.h:#define SIG_ERR ((__sighandler_t) -1) /* Error return. */
/usr/include/bits/signum-generic.h:#define SIG_DFL ((__sighandler_t) 0) /* Default action. */
/usr/include/bits/signum-generic.h:#define SIG_IGN ((__sighandler_t) 1) /* Ignore signal. */ In Python 3.13, there are now enums:
pickle serializes them as import pickle, pickletools, signal
pickletools.dis(pickle.dumps(signal.SIG_IGN)) Output (commented):
As @ethanfurman wrote, Handlers can be modified for that. |
I should have commit a patch 9 years ago without waiting for a review. Since then, the code has been significantly modified several times, making it difficult to keep the patch up-to-date. I strongly suspect that the current code contains severe bugs in the case when subinterpreters are involved. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: