Skip to content

Commit

Permalink
[mypyc] Don't rely on _PyType_CalculateMetaclass on 3.13 (#17525)
Browse files Browse the repository at this point in the history
Copy the implementation from CPython (with minor changes), as it's no
longer exported.

Work on mypyc/mypyc#1056.
  • Loading branch information
JukkaL committed Jul 14, 2024
1 parent 42337a0 commit 6a0657e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
5 changes: 1 addition & 4 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,7 @@ static inline bool CPy_TypeCheck(PyObject *o, PyObject *type) {
return PyObject_TypeCheck(o, (PyTypeObject *)type);
}

static inline PyObject *CPy_CalculateMetaclass(PyObject *type, PyObject *o) {
return (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)type, o);
}

PyObject *CPy_CalculateMetaclass(PyObject *type, PyObject *o);
PyObject *CPy_GetCoro(PyObject *obj);
PyObject *CPyIter_Send(PyObject *iter, PyObject *val);
int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp);
Expand Down
48 changes: 47 additions & 1 deletion mypyc/lib-rt/misc_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,52 @@ static bool _CPy_IsSafeMetaClass(PyTypeObject *metaclass) {
return matches;
}

#if CPY_3_13_FEATURES

// Adapted from CPython 3.13.0b3
/* Determine the most derived metatype. */
PyObject *CPy_CalculateMetaclass(PyObject *metatype, PyObject *bases)
{
Py_ssize_t i, nbases;
PyTypeObject *winner;
PyObject *tmp;
PyTypeObject *tmptype;

/* Determine the proper metatype to deal with this,
and check for metatype conflicts while we're at it.
Note that if some other metatype wins to contract,
it's possible that its instances are not types. */

nbases = PyTuple_GET_SIZE(bases);
winner = (PyTypeObject *)metatype;
for (i = 0; i < nbases; i++) {
tmp = PyTuple_GET_ITEM(bases, i);
tmptype = Py_TYPE(tmp);
if (PyType_IsSubtype(winner, tmptype))
continue;
if (PyType_IsSubtype(tmptype, winner)) {
winner = tmptype;
continue;
}
/* else: */
PyErr_SetString(PyExc_TypeError,
"metaclass conflict: "
"the metaclass of a derived class "
"must be a (non-strict) subclass "
"of the metaclasses of all its bases");
return NULL;
}
return (PyObject *)winner;
}

#else

PyObject *CPy_CalculateMetaclass(PyObject *metatype, PyObject *bases) {
return (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)metatype, bases);
}

#endif

// Create a heap type based on a template non-heap type.
// This is super hacky and maybe we should suck it up and use PyType_FromSpec instead.
// We allow bases to be NULL to represent just inheriting from object.
Expand Down Expand Up @@ -163,7 +209,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
// Find the appropriate metaclass from our base classes. We
// care about this because Generic uses a metaclass prior to
// Python 3.7.
metaclass = _PyType_CalculateMetaclass(metaclass, bases);
metaclass = (PyTypeObject *)CPy_CalculateMetaclass((PyObject *)metaclass, bases);
if (!metaclass)
goto error;

Expand Down

0 comments on commit 6a0657e

Please sign in to comment.