summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorPetr Viktorin <encukou@gmail.com>2022-02-11 11:25:25 (GMT)
committerGitHub <noreply@github.com>2022-02-11 11:25:25 (GMT)
commit8b8673fe940c4ebc4512bff5af180b66def3d1ae (patch)
tree97ea957663ed8fe28fe0bc9a35b37ff134cb6e70 /Objects
parent1124ab6d1d15dc5058e03b63fd1d95e6f1009cc3 (diff)
downloadcpython-8b8673fe940c4ebc4512bff5af180b66def3d1ae.zip
cpython-8b8673fe940c4ebc4512bff5af180b66def3d1ae.tar.gz
cpython-8b8673fe940c4ebc4512bff5af180b66def3d1ae.tar.bz2
[3.10] bpo-46433: _PyType_GetModuleByDef: handle static types in MRO (GH-30696) (GH-31262)
(cherry picked from commit 0ef08530124c5ca13a9394f4ac18bee8e6c66409)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c16
1 files changed, 7 insertions, 9 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 10b69fe..deffeb2 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3707,22 +3707,20 @@ _PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def)
// to check i < PyTuple_GET_SIZE(mro) at the first loop iteration.
assert(PyTuple_GET_SIZE(mro) >= 1);
- Py_ssize_t i = 0;
- do {
+ Py_ssize_t n = PyTuple_GET_SIZE(mro);
+ for (Py_ssize_t i = 0; i < n; i++) {
PyObject *super = PyTuple_GET_ITEM(mro, i);
- // _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.
- assert(_PyType_HasFeature((PyTypeObject *)type, Py_TPFLAGS_HEAPTYPE));
+ if(!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
+ // Static types in the MRO need to be skipped
+ continue;
+ }
PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
PyObject *module = ht->ht_module;
if (module && _PyModule_GetDef(module) == def) {
return module;
}
- i++;
- } while (i < PyTuple_GET_SIZE(mro));
+ }
PyErr_Format(
PyExc_TypeError,