You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve guidelines for GC protocol implementation for heap types #138292
Now, tp_alloc automatically calls PyObject_GC_Track so users won't be able to pre-initialize fields. If people need to first initialize fields, maybe we should recommend constructing them first
I don't think this will actually be an issue in practice. tp_alloc will zero the memory, so tp_clear and tp_traverse can be safely called. If the constructor calls into Python code, the object shouldn't be used anyway, per the note in gc.get_referrers:
Care must be taken when using objects returned by get_referrers() because some of them could still be under construction and hence in a temporarily invalid state. Avoid using get_referrers() for any purpose other than debugging.
In light of #116946 (comment) and #138329 (comment), immutable empty heap types do not need to implement the GC protocol because they will never contribute to a cycle. This may be documented as well.
Activity
ZeroIntensity commentedon Aug 31, 2025
I don't think this will actually be an issue in practice.
tp_allocwill zero the memory, sotp_clearandtp_traversecan be safely called. If the constructor calls into Python code, the object shouldn't be used anyway, per the note ingc.get_referrers:picnixz commentedon Aug 31, 2025
ZeroIntensity commentedon Aug 31, 2025
picnixz commentedon Sep 1, 2025
In light of #116946 (comment) and #138329 (comment), immutable empty heap types do not need to implement the GC protocol because they will never contribute to a cycle. This may be documented as well.