Author jhaberman
Recipients Alexander.Belopolsky, Arfrever, Robin.Schreiber, amaury.forgeotdarc, belopolsky, jcea, jhaberman, lekma, loewis, petr.viktorin, pitrou
Date 2021-08-02.15:45:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1627919116.9.0.982721021255.issue15870@roundup.psfhosted.org>
In-reply-to
Content
> You can also call (PyObject_Call*) the metaclass with (name, bases, namespace);

But won't that just call my metaclass's tp_new?  I'm trying to do this from my metaclass's tp_new, so I can customize the class creation process. Then Python code can use my metaclass to construct classes normally.

> I wouldn't recommend [setting ob_type] after PyType_Ready is called.

Why not?  What bad things will happen?  It seems to be working so far.

Setting ob_type directly actually solves another problem that I had been having with the limited API.  I want to implement tp_getattro on the metaclass, but I want to first delegate to PyType.tp_getattro to return any entry that may be present in the type's tp_dict.  With the full API I could call self->ob_type->tp_base->tp_getattro() do to the equivalent of super(), but with the limited API I can't access type->tp_getattro (and PyType_GetSlot() can't be used on non-heap types).

I find that this does what I want:

  PyTypeObject *saved_type = self->ob_type;
  self->ob_type = &PyType_Type;
  PyObject *ret = PyObject_GetAttr(self, name);
  self->ob_type = saved_type;

Previously I had tried:

   PyObject *super = PyObject_CallFunction((PyObject *)&PySuper_Type, "OO",
                                           self->ob_type, self);
   PyObject *ret = PyObject_GetAttr(super, name);
   Py_DECREF(super);

But for some reason this didn't work.
History
Date User Action Args
2021-08-02 15:45:16jhabermansetrecipients: + jhaberman, loewis, jcea, amaury.forgeotdarc, belopolsky, pitrou, Arfrever, petr.viktorin, lekma, Alexander.Belopolsky, Robin.Schreiber
2021-08-02 15:45:16jhabermansetmessageid: <1627919116.9.0.982721021255.issue15870@roundup.psfhosted.org>
2021-08-02 15:45:16jhabermanlinkissue15870 messages
2021-08-02 15:45:16jhabermancreate