-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-40217: Ensure Py_VISIT(Py_TYPE(self)) is always called for PyType_FromSpec types #19414
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
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I have a few minor remarks.
if (obj == NULL) | ||
return PyErr_NoMemory(); | ||
|
||
obj = obj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of that?
/* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP 7: please add { ... }. Same remark for other if.
@@ -2985,6 +3047,26 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) | |||
memcpy(PyHeapType_GET_MEMBERS(res), slot->pfunc, len); | |||
type->tp_members = PyHeapType_GET_MEMBERS(res); | |||
} | |||
else if (slot->slot == Py_tp_traverse) { | |||
|
|||
/* Types created by PyType_FromSpec own a strong reference to their |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put bpo-40217 somewhere in the comment.
@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.