Skip to content

gh-114314: ctypes: remove stgdict and switch to heap types - #116458

Merged
encukou merged 60 commits into
python:mainfrom
encukou:ctypes-no-stgdict
Mar 20, 2024
Merged

encukou merged 60 commits into
python:mainfrom
encukou:ctypes-no-stgdict

Conversation

@encukou

@encukou encukou commented Mar 7, 2024 •

Copy link
Copy Markdown
Member

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 information
about the type.

This replaces stgdict by StgInfo, a C struct on the type, accessed by
PyObject_GetTypeData() (PEP-697).
All usage of StgDict (mainly variables named stgdict, dict, edict etc.) is
converted to StgInfo (named stginfo, 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 with
the next part of this PR: moving the initialization logic from tp_new to tp_init.

The StgInfo is set up in __init__ of each class, with a guard that prevents
calling __init__ more than once. Note that abstract classes (like Array or
Structure) are created using PyType_FromMetaclass and do not have
__init__ called.
Previously, this was done in __new__, which also wasn't called for abstract
classes.
Since __init__ can be called from Python code or skipped, there is a tested
guard to ensure StgInfo is initialized exactly once before it's used.

Thanks to @neonene and @erlend-aasland.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants