bpo-40217: Ensure Py_VISIT(Py_TYPE(self)) is always called for PyType_FromSpec types#19414
bpo-40217: Ensure Py_VISIT(Py_TYPE(self)) is always called for PyType_FromSpec types#19414vstinner merged 1 commit intopython:masterfrom
Conversation
ea96f0b to
88f807c
Compare
|
Before this PR: With this PR: |
353f682 to
ab1ca32
Compare
|
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit ab1ca32 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
vstinner
left a comment
There was a problem hiding this comment.
LGTM, but I have a few minor remarks.
| if (obj == NULL) | ||
| return PyErr_NoMemory(); | ||
|
|
||
| obj = obj; |
| /* note that we need to add one, for the sentinel and space for the | ||
| provided tp-traverse: See bpo-40217 for more details */ | ||
|
|
||
| if (PyType_IS_GC(type)) |
There was a problem hiding this comment.
PEP 7: please add { ... }. Same remark for other if.
| } | ||
| else if (slot->slot == Py_tp_traverse) { | ||
|
|
||
| /* Types created by PyType_FromSpec own a strong reference to their |
|
@pablogsal: I added "skip news" to be able to merge your PR, but IMHO it's worth it to add a NEWS entry for it (I suggest the C API category). Would you mind to add one? |
Will do but I didn't had time to add your suggestions for this PR before you merged it. Should I add this in the future PR? |
Yeah. |
…r PyType_FromSpec types (pythonGH-19414)" This reverts commit 0169d30.
https://bugs.python.org/issue40217
Types created by PyType_FromSpec own a strong reference to their type, but this was added in Python 3.8. The tp_traverse function needs to call Py_VISIT on the type but all existing traverse functions cannot be updated (especially the ones from existing user functions) so we need to provide a tp_traverse that manually calls Py_VISIT(Py_TYPE(self)) and then call the provided tp_traverse. In this way, user functions do not need to be updated, preserve backwards compatibility.
To do this, we store the user-provided traverse function at the end of the type (we need to allocate space for it) so we can call it from our PyType_FromSpec_tp_traverse wrapper.