Repository navigation
gh-114314: ctypes: remove stgdict and switch to heap types - #116458
Merged
Merged
Conversation
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.
Before this change, ctypes classes used a custom dict subclass,
StgDict,as their
tp_dict. This acts like a regular dict but also includes extra informationabout the type.
This replaces stgdict by
StgInfo, a C struct on the type, accessed byPyObject_GetTypeData()(PEP-697).All usage of
StgDict(mainly variables namedstgdict,dict,edictetc.) isconverted to
StgInfo(namedstginfo,info,einfo, etc.).Where the dict is actually used for class attributes (as a regular PyDict), it's now
called
attrdict.This change -- not overriding
tp_dict-- is made to make me comfortable withthe next part of this PR: moving the initialization logic from
tp_newtotp_init.The
StgInfois set up in__init__of each class, with a guard that preventscalling
__init__more than once. Note that abstract classes (likeArrayorStructure) are created usingPyType_FromMetaclassand do not have__init__called.Previously, this was done in
__new__, which also wasn't called for abstractclasses.
Since
__init__can be called from Python code or skipped, there is a testedguard to ensure
StgInfois initialized exactly once before it's used.Thanks to @neonene and @erlend-aasland.