In Python c-api: how can I create a heap type that has a metaclass? I’m well aware of PyType_FromSpec
and derivatives and they can do this (from documentation):
Changed in version 3.12: The function now finds and uses a metaclass corresponding to the provided base classes. Previously, only type instances were returned.
The tp_new of the metaclass is ignored. which may result in incomplete initialization. Creating classes whose metaclass overrides tp_new is deprecated and in Python 3.14+ it will be no longer allowed.
I have two one issue with this: support for metaclasses was added in Python 3.12 and I want to support Python 3.8+. Second one: if class name is not statically defined, (creating with malloc and eg.: string concatenation), for python < 3.11, this will leak, as that function doesn’t copy the buffer in these versions and there is no good way of freeing that buffer (PyTypeObject->tp_name is a public field and can be replaced by anything).