8000 Check __mro__ instead of __bases__ · python/cpython@9c8de19 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c8de19

Browse files
committed
Check __mro__ instead of __bases__
1 parent 710dd51 commit 9c8de19

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Objects/typeobject.c

-5Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4618,9 +4618,14 @@ check_basicsize_includes_size_and_offsets(PyTypeObject* type)
46184618
}
46194619

46204620
static int
4621-
check_immutable_bases(const char *type_name, PyObject *bases)
4621+
check_immutable_bases(const char *type_name, PyObject *bases, int skip_first)
46224622
{
4623-
for (Py_ssize_t i=0; i<PyTuple_GET_SIZE(bases); i++) {
4623+
Py_ssize_t i = 0;
4624+
if (skip_first) {
4625+
// When testing the MRO, skip the type itself
4626+
i = 1;
4627+
}
4628+
for (; i<PyTuple_GET_SIZE(bases); i++) {
46244629
PyTypeObject *b = (PyTypeObject*)PyTuple_GET_ITEM(bases, i);
46254630
if (!b) {
46264631
return -1;
@@ -4793,7 +4798,7 @@ _PyType_FromMetaclass_impl(
47934798
* and only heap types can be mutable.)
47944799
*/
47954800
if (spec->flags & Py_TPFLAGS_IMMUTABLETYPE) {
4796-
if (check_immutable_bases(spec->name, bases) < 0) {
4801+
if (check_immutable_bases(spec->name, bases, 0) < 0) {
47974802
goto finally;
47984803
}
47994804
}
@@ -11139,8 +11144,17 @@ add_operators(PyTypeObject *type, PyTypeObject *def)
1113911144
int
1114011145
PyType_Freeze(PyTypeObject *type)
1114111146
{
11142-
PyObject *bases = type->tp_bases;
11143-
if (check_immutable_bases(type->tp_name, bases) < 0) {
11147+
// gh-121654: Check the __mro__ instead of __bases__
11148+
PyObject *mro = type_get_mro(type, NULL);
11149+
if (!PyTuple_Check(mro)) {
11150+
Py_DECREF(mro);
11151+
PyErr_SetString(PyExc_TypeError, "unable to get the type MRO");
11152+
return -1;
11153+
}
11154+
11155+
int check = check_immutable_bases(type->tp_name, mro, 1);
11156+
Py_DECREF(mro);
11157+
if (check < 0) {
1114411158
return -1;
1114511159
}
1114611160

0 commit comments

Comments
 (0)
0