cpython/Modules/_ctypes
Petr Viktorin dcaf33a41d
gh-114314: ctypes: remove stgdict and switch to heap types (GH-116458)
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.

Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
2024-03-20 17:33:08 +01:00
..
clinic gh-107603: Argument Clinic: Only include pycore_gc.h if needed (#108726) 2023-08-31 23:42:34 +02:00
_ctypes.c gh-114314: ctypes: remove stgdict and switch to heap types (GH-116458) 2024-03-20 17:33:08 +01:00
_ctypes_test.c gh-108494: Fix Argument Clinic LIMITED_CAPI_REGEX (#116610) 2024-03-11 22:42:18 +00:00
_ctypes_test.h Copy ctypes-0.9.9.4 sources from external into the trunk. 2006-03-08 19:35:11 +00:00
callbacks.c gh-114314: ctypes: remove stgdict and switch to heap types (GH-116458) 2024-03-20 17:33:08 +01:00
callproc.c gh-114314: ctypes: remove stgdict and switch to heap types (GH-116458) 2024-03-20 17:33:08 +01:00
cfield.c gh-114314: ctypes: remove stgdict and switch to heap types (GH-116458) 2024-03-20 17:33:08 +01:00
ctypes.h gh-114314: ctypes: remove stgdict and switch to heap types (GH-116458) 2024-03-20 17:33:08 +01:00
malloc_closure.c gh-108765: Python.h no longer includes <unistd.h> (#108783) 2023-09-02 16:50:18 +02:00
stgdict.c gh-114314: ctypes: remove stgdict and switch to heap types (GH-116458) 2024-03-20 17:33:08 +01:00