summaryrefslogtreecommitdiffstats
path: root/Objects/typeobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/typeobject.c')
-rw-r--r--Objects/typeobject.c52
1 files changed, 45 insertions, 7 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 808e11f..07e0a5a 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4825,11 +4825,24 @@ PyType_GetModuleState(PyTypeObject *type)
/* Get the module of the first superclass where the module has the
* given PyModuleDef.
*/
-PyObject *
-PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
+static inline PyObject *
+get_module_by_def(PyTypeObject *type, PyModuleDef *def)
{
assert(PyType_Check(type));
+ if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
+ // type_ready_mro() ensures that no heap type is
+ // contained in a static type MRO.
+ return NULL;
+ }
+ else {
+ PyHeapTypeObject *ht = (PyHeapTypeObject*)type;
+ PyObject *module = ht->ht_module;
+ if (module && _PyModule_GetDef(module) == def) {
+ return module;
+ }
+ }
+
PyObject *res = NULL;
BEGIN_TYPE_LOCK()
@@ -4837,12 +4850,14 @@ PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
// The type must be ready
assert(mro != NULL);
assert(PyTuple_Check(mro));
- // mro_invoke() ensures that the type MRO cannot be empty, so we don't have
- // to check i < PyTuple_GET_SIZE(mro) at the first loop iteration.
+ // mro_invoke() ensures that the type MRO cannot be empty.
assert(PyTuple_GET_SIZE(mro) >= 1);
+ // Also, the first item in the MRO is the type itself, which
+ // we already checked above. We skip it in the loop.
+ assert(PyTuple_GET_ITEM(mro, 0) == (PyObject *)type);
Py_ssize_t n = PyTuple_GET_SIZE(mro);
- for (Py_ssize_t i = 0; i < n; i++) {
+ for (Py_ssize_t i = 1; i < n; i++) {
PyObject *super = PyTuple_GET_ITEM(mro, i);
if(!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
// Static types in the MRO need to be skipped
@@ -4857,14 +4872,37 @@ PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
}
}
END_TYPE_LOCK()
+ return res;
+}
- if (res == NULL) {
+PyObject *
+PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
+{
+ PyObject *module = get_module_by_def(type, def);
+ if (module == NULL) {
PyErr_Format(
PyExc_TypeError,
"PyType_GetModuleByDef: No superclass of '%s' has the given module",
type->tp_name);
}
- return res;
+ return module;
+}
+
+PyObject *
+_PyType_GetModuleByDef2(PyTypeObject *left, PyTypeObject *right,
+ PyModuleDef *def)
+{
+ PyObject *module = get_module_by_def(left, def);
+ if (module == NULL) {
+ module = get_module_by_def(right, def);
+ if (module == NULL) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "PyType_GetModuleByDef: No superclass of '%s' nor '%s' has "
+ "the given module", left->tp_name, right->tp_name);
+ }
+ }
+ return module;
}
void *