Repository navigation
Closed
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
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:
assignee = None
closed_at = None
created_at = <Date 2015-01-26.20:38:30.138>
labels = ['type-feature', 'library', '3.9', '3.10', '3.11']
title = 'Turn SIG_DFL and SIG_IGN into functions'
updated_at = <Date 2022-03-31.17:54:28.870>
user = 'https://github.com/serhiy-storchaka'bugs.python.org fields:
activity = <Date 2022-03-31.17:54:28.870>
actor = 'brett.cannon'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2015-01-26.20:38:30.138>
creator = 'serhiy.storchaka'
dependencies = []
files = ['37868']
hgrepos = []
issue_num = 23325
keywords = ['patch']
message_count = 14.0
messages = ['234775', '238327', '238495', '238538', '324075', '324084', '324096', '411037', '411045', '411104', '414747', '414749', '414765', '414767']
nosy_count = 9.0
nosy_names = ['barry', 'brett.cannon', 'vstinner', 'giampaolo.rodola', 'christian.heimes', 'eli.bendersky', 'serhiy.storchaka', 'xiang.zhang', 'miss-islington']
pr_nums = ['8920', '31759', '31768']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue23325'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Activity
serhiy-storchaka commentedon Jan 26, 2015
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).
ethanfurman commentedon Mar 17, 2015
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)'?
serhiy-storchaka commentedon Mar 19, 2015
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.
ethanfurman commentedon Mar 19, 2015
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.
zhangyangyu commentedon Aug 25, 2018
I'm afraid this could break old codes so not sure it's worth or not. I've seen code like
SIGTERM = 15; signal.signal(SIGTERM, handler). I won't be surprised to see a handcraft SIG_DFL/IGN.serhiy-storchaka commentedon Aug 25, 2018
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.
zhangyangyu commentedon Aug 25, 2018
I miss that. So this change seems to be user transparent and make more sense then.
tiran commentedon Jan 20, 2022
Serhiy, could you please rebase your PR to tip of main branch? I'd like to try it out.
serhiy-storchaka commentedon Jan 20, 2022
It may take a time, because the module initialization code has been completely rewritten.
tiran commentedon Jan 21, 2022
Understood, thanks!
A trivial fix for the integer comparison bug would solve my issue:
tiran commentedon Mar 8, 2022
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.
serhiy-storchaka commentedon Mar 8, 2022
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.
miss-islington commentedon Mar 8, 2022
New changeset c8a47e7 by Christian Heimes in branch 'main':
bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module (GH-31759)
c8a47e7
miss-islington commentedon Mar 8, 2022
New changeset 95b001f by Miss Islington (bot) in branch '3.10':
bpo-23325: Fix SIG_IGN and SIG_DFL int comparison in signal module (GH-31759)
95b001f
encukou commentedon Mar 25, 2024
Do we still want to do this?
While accepting
0and1is undocumented, it's been that way for very long. IMO, it needs a deprecation period if it's changed.That said, I don't think they should be changed. Counterpoints to the rationale:
_signal.SIG_DFL, depending on the CPython-specific C module.I recommend closing.
vstinner commentedon Mar 25, 2024
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
SIG_DFLandSIG_IGNbecome functions, people will want to call them, and I expect that it will complicated to implementSIG_DFL: you should callsignal.raise_signal(signum)... but with which signal number? If we store the signal number, the sameSIG_DFLobject cannot be used for two signals. Or they should be fake functions which always fail, for exampleraise NotImplementedError(). But users may expect a different behavior.I would be ok with replacing
SIG_DFLandSIG_IGNwith special non-callable objects, different than int. But I'm not sure the benefits are worth it compared to the risk of breaking existing code. Currently, it's possible to convert them to int. Should it still be the case if they are changed? If yes, what's the point?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:
In Python 3.13, there are now enums:
pickle serializes them as
signal.SIG_IGNassignal.Handlers(*(1,)):Output (commented):
As @ethanfurman wrote, Handlers can be modified for that.
serhiy-storchaka commentedon Mar 26, 2024
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.