Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8684e59

Browse files
bpo-33237: Improve AttributeError message for partially initialized module.
1 parent e22072f commit 8684e59

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

Objects/moduleobject.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,36 @@ module_getattro(PyModuleObject *m, PyObject *name)
718718
_Py_IDENTIFIER(__name__);
719719
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
720720
if (mod_name && PyUnicode_Check(mod_name)) {
721-
PyErr_Format(PyExc_AttributeError,
722-
"module '%U' has no attribute '%U'", mod_name, name);
721+
_Py_IDENTIFIER(__spec__);
722+
_Py_IDENTIFIER(_initializing);
723+
PyObject *value = NULL;
724+
PyObject *spec;
725+
int initializing = 0;
726+
Py_INCREF(mod_name);
727+
spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__);
728+
if (spec != NULL && spec != Py_None) {
729+
value = _PyObject_GetAttrId(spec, &PyId__initializing);
730+
}
731+
if (value == NULL)
732+
PyErr_Clear();
733+
else {
734+
initializing = PyObject_IsTrue(value);
735+
Py_DECREF(value);
736+
if (initializing < 0)
737+
PyErr_Clear();
738+
}
739+
if (initializing > 0) {
740+
PyErr_Format(PyExc_AttributeError,
741+
"partially initialized "
742+
"module '%U' has no attribute '%U'",
743+
mod_name, name);
744+
}
745+
else {
746+
PyErr_Format(PyExc_AttributeError,
747+
"module '%U' has no attribute '%U'",
748+
mod_name, name);
749+
}
750+
Py_DECREF(mod_name);
723751
return NULL;
724752
}
725753
}

0 commit comments

Comments
 (0)