bpo-38037: Fix reference counters in signal module#15701
Conversation
brandtbucher
left a comment
There was a problem hiding this comment.
I'm not familiar with this file, but it looks like these Py_XINCREF calls should be made after the calls to PyModule_AddObject, correct?
If PyModule_AddObject fails, the additional reference isn't stolen... so it isn't needed.
Search If |
|
No need This is a regression caused by commit 9541bd3 (22 Apr 2019) |
Ah, yes. I at least think that we should |
|
The failure looks like it's due to https://bugs.python.org/issue34037, unrelated. Closing and reopening the PR should trigger a rebuild! |
Search in CPython code, some sites don't check I did all in the last commit. |
vstinner
left a comment
There was a problem hiding this comment.
LGTM, but I have a few minor comments.
@nanjekyejoannah: Would you mind to also review this change? I would prefer to have a second reviewer.
Misc/NEWS.d/next/Library/2019-09-05-22-09-38.bpo-38037.xA66nY.rst
Outdated
Show resolved
Hide resolved
|
@vstinner It looks like the idiom of calling I don't want other contributors to think that this is the correct way to use it. |
Co-Authored-By: Victor Stinner <vstinner@python.org>
|
Comments addressed. |
|
If you don't mind, I want to reuse compare DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
if (!DefaultHandler ||
PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
goto finally;
}to DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
if (!DefaultHandler) {
goto finally;
}
Py_INCREF(DefaultHandler);
if (PyModule_AddObject(m, "SIG_DFL", DefaultHandler)) {
Py_DECREF(DefaultHandler);
goto finally;
} |
nanjekyejoannah
left a comment
There was a problem hiding this comment.
If you don't mind, I want to reuse
PyDict_SetItemString().
It keeps the code neat, and saves a pair ofPy_INCREF()/Py_DECREF().compare
DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); if (!DefaultHandler || PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) { goto finally; }to
DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); if (!DefaultHandler) { goto finally; } Py_INCREF(DefaultHandler); if (PyModule_AddObject(m, "SIG_DFL", DefaultHandler)) { Py_DECREF(DefaultHandler); goto finally; }
Follow the discussion here : https://bugs.python.org/issue24011 on replacing PyDict_SetItemString() calls with PyModule_AddIntMacro() . By the time I made this change, all the PyModule_AddIntMacro() changes had been made save for this in this commit 6782b14
|
Moving this to a comment : Follow the discussion here : https://bugs.python.org/issue24011 on replacing PyDict_SetItemString() calls with PyModule_AddIntMacro() . By the time I made this change, all the PyModule_AddIntMacro() changes had been made save for this in this commit 6782b14 |
|
I still prefer to use IMO it's suitable for this situation, I have another problem. + finally:
if (PyErr_Occurred()) {
Py_DECREF(m);
m = NULL;
}
- finally:
return m;
}But this change has not been adopted, is it intentional or negligent? @tiran |
|
I don't think that PyModule_AddIntMacro() can be used in this PR. I like the removal of "x" variable, and reuse DefaultHandler, IgnoreHandler and ItimerError. |
I suggest you to open a new issue at bugs.python.org for that. Before fixing the usage, I suggest to enhance the documentation. Maybe add a short code example: |
|
@nanjekyejoannah: PyModule_AddObject() has terrible API, it's easy to misuse it... |
|
@vstinner @animalize agreed. |
If the PR looks good to you, would you mind to use GitHub UI to approve the PR? |
|
@animalize: If you want to use PyDict_SetItemString(), please open a separated PR so I can compare the two approaches. Using PyDict_SetItemString() sounds like a good idea: internally, PyModule_AddObject calls PyDict_SetItemString, but in PyInit__signal() we already have have "d" (module dictionary). |
|
In terms of fixing the current regression, this LGTM. |
Please see this PR, it uses |
|
I merged PR #15753 which is simpler than this PR. |
|
Thanks for your reviews! |
I'll open an issue about live code changes. I've already got a doc patch at #15725! |
Only 3.8 and 3.9 branches are affected.
https://bugs.python.org/issue38037