Repository navigation
gh-160749: Correct documentation of tp_alloc and tp_free inheritance for heap types - #160821
Open
Yuvansh-thx wants to merge 2 commits into
Open
Yuvansh-thx wants to merge 2 commits into
Yuvansh-thx wants to merge 2 commits into
Conversation
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes gh-160749
Summary of Changes
Clarify the inheritance and default documentation for the tp_alloc and tp_free slots in Doc/c-api/typeobj.rst.
The documentation previously stated that heap subtypes do not inherit tp_alloc or tp_free, and that for heap subtypes, tp_alloc is always set to PyType_GenericAlloc() and tp_free defaults to a generic deallocator.
However, heap types also include types created through C API functions such as PyType_FromSpec and PyType_FromMetaclass. For these types, if Py_tp_alloc or Py_tp_free is omitted from the specification slots, the corresponding slot is inherited from the base type through inherit_slots in Objects/typeobject.c. If the base type defines a custom allocator or deallocator, the C API heap subtype can inherit that custom implementation.
The exception applies to subtypes created using Python’s class statement or the type() constructor, which are handled by type_new_alloc. In these cases, tp_alloc and tp_free are explicitly initialized to PyType_GenericAlloc and PyObject_GC_Del, respectively.
Changes Made
Testing
Documentation-only change. No code changes or additional tests are included.