bpo-26868: Fix example usage of PyModule_AddObject.#15725
bpo-26868: Fix example usage of PyModule_AddObject.#15725matrixise merged 7 commits intopython:masterfrom
Conversation
|
Thanks for the feedback @serhiy-storchaka! Changes applied, tests are passing. |
Doc/extending/extending.rst
Outdated
| Py_INCREF(SpamError); | ||
| PyModule_AddObject(m, "error", SpamError); | ||
| Py_XINCREF(SpamError); | ||
| if (PyModule_AddObject(m, "error", SpamError)) { |
There was a problem hiding this comment.
I suggest to add also < 0 in all other checks for PyModule_AddObject() result.
There was a problem hiding this comment.
Is this an agreed-upon style? I had always thought it was a matter of taste.
In either case, I guess all of these files should at least be consistent...
|
There are three sytles currently:
After some thinking, I think the second one is not bad. Usually APIs use a negative return number to indicate failure. The third style may produce a very slightly longer machine code: ; 19 : if (PyModule_AddObject())
00004 e8 00 00 00 00 call ?PyModule_AddObject@@YAHXZ ; PyModule_AddObject
00009 85 c0 test eax, eax
0000b 74 07 je SHORT $LN2@main
; 21 : if (PyModule_AddObject() < 0)
00004 e8 00 00 00 00 call ?PyModule_AddObject@@YAHXZ ; PyModule_AddObject
00009 85 c0 test eax, eax
0000b 7d 07 jge SHORT $LN2@main
; 23 : if (PyModule_AddObject() == -1)
00004 e8 00 00 00 00 call ?PyModule_AddObject@@YAHXZ ; PyModule_AddObject
00009 83 f8 ff cmp eax, -1
0000c 75 07 jne SHORT $LN2@mainAnd There are reasons, although the reasons are weak. 😂 |
matrixise
left a comment
There was a problem hiding this comment.
Hi, Thank you for your contribution but could you add the small requested changes.
Thank you
Doc/extending/extending.rst
Outdated
|
|
||
| if (c_api_object != NULL) | ||
| PyModule_AddObject(m, "_C_API", c_api_object); | ||
| if (PyModule_AddObject(m, "_C_API", c_api_object)) { |
There was a problem hiding this comment.
ditto, change the example with if (PyModule_AddObject(m, "_C_API", c_api_object) < 0)
Doc/extending/newtypes_tutorial.rst
Outdated
|
|
||
| PyModule_AddObject(m, "Custom", (PyObject *) &CustomType); | ||
| Py_INCREF(&CustomType); | ||
| if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) { |
Doc/extending/newtypes_tutorial.rst
Outdated
|
|
||
| Py_INCREF(&SubListType); | ||
| PyModule_AddObject(m, "SubList", (PyObject *) &SubListType); | ||
| if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType)) { |
Doc/includes/custom.c
Outdated
|
|
||
| Py_INCREF(&CustomType); | ||
| PyModule_AddObject(m, "Custom", (PyObject *) &CustomType); | ||
| if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) { |
Doc/includes/custom2.c
Outdated
|
|
||
| Py_INCREF(&CustomType); | ||
| PyModule_AddObject(m, "Custom", (PyObject *) &CustomType); | ||
| if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) { |
Doc/includes/custom3.c
Outdated
|
|
||
| Py_INCREF(&CustomType); | ||
| PyModule_AddObject(m, "Custom", (PyObject *) &CustomType); | ||
| if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) { |
Doc/includes/custom4.c
Outdated
|
|
||
| Py_INCREF(&CustomType); | ||
| PyModule_AddObject(m, "Custom", (PyObject *) &CustomType); | ||
| if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) { |
Doc/includes/sublist.c
Outdated
|
|
||
| Py_INCREF(&SubListType); | ||
| PyModule_AddObject(m, "SubList", (PyObject *) &SubListType); | ||
| if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType)) { |
matrixise
left a comment
There was a problem hiding this comment.
Thank you for your contribution, I have fixed the remarks with < 0 in the examples. I am going to merge it.
|
@matrixise: Please replace |
|
Thanks @brandtbucher for the PR, and @matrixise for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7, 3.8. |
* Add a note to the PyModule_AddObject docs. * Correct example usages of PyModule_AddObject. * Whitespace. * Clean up wording. * 📜🤖 Added by blurb_it. * First code review. * Add < 0 in the tests with PyModule_AddObject (cherry picked from commit 224b8aa) Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
|
GH-16037 is a backport of this pull request to the 3.8 branch. |
* Add a note to the PyModule_AddObject docs. * Correct example usages of PyModule_AddObject. * Whitespace. * Clean up wording. * 📜🤖 Added by blurb_it. * First code review. * Add < 0 in the tests with PyModule_AddObject (cherry picked from commit 224b8aa) Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
|
GH-16038 is a backport of this pull request to the 3.7 branch. |
* Add a note to the PyModule_AddObject docs. * Correct example usages of PyModule_AddObject. * Whitespace. * Clean up wording. * 📜🤖 Added by blurb_it. * First code review. * Add < 0 in the tests with PyModule_AddObject (cherry picked from commit 224b8aa) Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
* Add a note to the PyModule_AddObject docs. * Correct example usages of PyModule_AddObject. * Whitespace. * Clean up wording. * 📜🤖 Added by blurb_it. * First code review. * Add < 0 in the tests with PyModule_AddObject (cherry picked from commit 224b8aa) Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
|
Thanks @matrixise! |
Unlike other APIs that steal references,
PyModule_AddObjectonly does it on success. However, most calls throughout the codebase don't manuallyPy_DECREFon failure as required by the implementation, which leads contributors to believe that it is not necessary. This PR fixes example usage and adds a note on this behavior in the documentation only.There are over 200 bad calls throughout the codebase that are not addressed by this patch. Because there are so many bugs, fixing them all will likely require more care and likely several PRs (or a possible deprecation period and behavior change... it appears incorrect usage is actually more common than correct usage). I'm willing to do the work, but some direction on this would be nice.
https://bugs.python.org/issue26868