Skip to content

Commit d4aaa34

Browse files
authored
bpo-40137: _PyType_GetModuleByDef() doesn't check tp_flags (GH-25504)
_PyType_GetModuleByDef() no longer checks if types are heap types. _PyType_GetModuleByDef() must only be called on a heap type created by PyType_FromModuleAndSpec() or on its subclasses. type_ready_mro() ensures that a static type cannot inherit from a heap type.
1 parent 81fe014 commit d4aaa34

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

Objects/typeobject.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,24 +3590,23 @@ PyObject *
35903590
_PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def)
35913591
{
35923592
assert(PyType_Check(type));
3593-
assert(type->tp_mro);
3594-
int i;
3595-
for (i = 0; i < PyTuple_GET_SIZE(type->tp_mro); i++) {
3596-
PyObject *super = PyTuple_GET_ITEM(type->tp_mro, i);
3597-
if (!PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
3598-
/* Currently, there's no way for static types to inherit
3599-
* from heap types, but to allow that possibility,
3600-
* we `continue` rather than `break`.
3601-
* We'll just potentially loop a few more times before throwing
3602-
* the error.
3603-
*/
3604-
continue;
3605-
}
3593+
PyObject *mro = type->tp_mro;
3594+
assert(mro != NULL);
3595+
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(mro); i++) {
3596+
PyObject *super = PyTuple_GET_ITEM(mro, i);
3597+
// _PyType_GetModuleByDef() must only be called on a heap type created
3598+
// by PyType_FromModuleAndSpec() or on its subclasses.
3599+
// type_ready_mro() ensures that a static type cannot inherit from a
3600+
// heap type.
3601+
assert(_PyType_HasFeature((PyTypeObject *)type, Py_TPFLAGS_HEAPTYPE));
3602+
36063603
PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
3607-
if (ht->ht_module && PyModule_GetDef(ht->ht_module) == def) {
3608-
return ht->ht_module;
3604+
PyObject *module = ht->ht_module;
3605+
if (module && PyModule_GetDef(module) == def) {
3606+
return module;
36093607
}
36103608
}
3609+
36113610
PyErr_Format(
36123611
PyExc_TypeError,
36133612
"_PyType_GetModuleByDef: No superclass of '%s' has the given module",

0 commit comments

Comments
 (0)