On 9 Jun 2020, at 21:12, Sebastian Berg <sebastian@sipsolutions.net> wrote:
On Tue, 2020-06-09 at 20:48 +0200, Petr Viktorin wrote:
On 2020-06-09 18:32, Ronald Oussoren wrote:
On 9 Jun 2020, at 14:50, Petr Viktorin <encukou@gmail.com <mailto:encukou@gmail.com>> wrote:
[…]
I don't think this is about metaclasses per se: you can create a metaclass in the limited API. Rather, it's about attaching state to class objects—at the C level, i.e. allocating some space rather than putting something in the __dict__.
That’s not just a problem for classes, the same is true for subclassing other types for which the instance struct is private (e.g. all builtin types in the limited API).
For classes, the problem is even worse because they're implemented as variable-length objects ("containers", PyVarObject) to hold slots.
For normal objects, you can play tricks with the ob_size of the superclass, which tells you where additional data can be located in the instance. But for classes (and lists, tuples, ...), it's not clear what the memory layout for additional storage should be, let alone how to access/define it.
More indirection could help. One way to do this is to have an API to retrieve a pointer to the data for a specific class. That is, subclasses define a struct with the additional data for that subclass, an API returns a pointer to that data for a given object.
Is is possible to define a class that uses a metaclass using PyType_FromSpec or one of its variants? AFAIK it is not possible to set the ob_type field of the to be created type using these APIs.
Ah, I see what you mean. No, it's currently not possible to set the metaclass using PyType_FromSpec. I don't see a reason why adding a Py_ob_type slot would be impossible, but I haven't really given it much thought.
Ah, right, I think that was actually one of main issues... For me this was a bit more tedious. I am currently not too scared of using the limited API for the metaclass (in the end, it probably is a true static, although I am not sure that helps with embedding/sub- interpreters). But it would be much better to use (only) the limited API for dynamically creating the metaclasses instances.
This is one technical limitation that stops me from moving to the new API in PyObjC.
Ronald