[issue15870] PyType_FromSpec should take metaclass as an argument

Sebastian Berg report at bugs.python.org
Fri Sep 17 18:54:10 EDT 2021


Sebastian Berg <sebastian at sipsolutions.net> added the comment:

I am still fighting with this (and the issues surrounding it) for NumPy.  The main point is that my new DTypes in NumPy are metaclasses that extend the (heap)type struct.
That just feels right and matches the structure perfectly, but seems to get awkward when you want (users) to dynamically create new MetaClass instances.


It also works fine: I could allow creating new instances from Python (although I don't need it now) and allow users to create static types (for this I have to fix the size of the additional fields for a stable ABI, but that is fine, I can allocate a new opaque slot).

But cython or heaptypes (stable API) seem not accessible without annoying hacks...


For this concrete issue, would it be acceptable to use the base classes to correctly find the metaclass and use its alloc?
I personally don't see why a new signature would be needed even if the metaclass was to be passed (I do not need this), it seems like you could pass a `Py_tp_meta` slot and not add a new function?


I have attached a potential patch (it is a bit large because it needs to move the `bases` discovery code to before the `res` allocation).


Doing this would allow to provide a `FromSpec` function which internally calls `PyType_FromSpec`.
That may not make things much neater for cython, but it feels like this is really just a bug fix?  Unless you want to consider any extension of the type struct an unofficial hack to begin with :).


(Python metaclasses won't get their `__new__` called, but that is already the case, presumably a metaclass author will provide their own `InitFromSpec` function that must be called immediately after type creation, or just create the type completely.  I can do the second even now probably, but this tiny change would make it a lot cleaner.)


Trying more desperate angles, I was even wondering today if I should instead petition for Python to "donate" a `tp_metaslot` type slot...  A single `void *` unused by `PyType_Type` but available to metatypes to use as they wish.

While a bit strange, that might even simplify some ABI concerns or cython compatibility ;).

----------
nosy: +seberg
Added file: https://bugs.python.org/file50285/typeobject.patch

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue15870>
_______________________________________


More information about the Python-bugs-list mailing list