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

Skip to content

Commit 770f2f5

Browse files
Add _PyModule_IsInitializing().
1 parent b072b9b commit 770f2f5

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

Include/moduleobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ PyAPI_FUNC(PyObject *) PyModule_GetFilenameObject(PyObject *);
3030
#ifndef Py_LIMITED_API
3131
PyAPI_FUNC(void) _PyModule_Clear(PyObject *);
3232
PyAPI_FUNC(void) _PyModule_ClearDict(PyObject *);
33+
PyAPI_FUNC(int) _PyModule_IsInitializing(PyObject *);
3334
#endif
3435
PyAPI_FUNC(struct PyModuleDef*) PyModule_GetDef(PyObject*);
3536
PyAPI_FUNC(void*) PyModule_GetState(PyObject*);

Objects/moduleobject.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,33 @@ module_repr(PyModuleObject *m)
699699
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
700700
}
701701

702+
int
703+
_PyModule_IsInitializing(PyObject *m)
704+
{
705+
if (PyModule_Check(m) && ((PyModuleObject *)m)->md_dict) {
706+
_Py_IDENTIFIER(__spec__);
707+
_Py_IDENTIFIER(_initializing);
708+
PyObject *value = NULL;
709+
PyObject *spec;
710+
int initializing = 0;
711+
spec = _PyDict_GetItemId(((PyModuleObject *)m)->md_dict, &PyId___spec__);
712+
if (spec != NULL && spec != Py_None) {
713+
value = _PyObject_GetAttrId(spec, &PyId__initializing);
714+
}
715+
if (value == NULL) {
716+
PyErr_Clear();
717+
}
718+
else {
719+
initializing = PyObject_IsTrue(value);
720+
Py_DECREF(value);
721+
if (initializing < 0)
722+
PyErr_Clear();
723+
return initializing > 0;
724+
}
725+
}
726+
return 0;
727+
}
728+
702729
static PyObject*
703730
module_getattro(PyModuleObject *m, PyObject *name)
704731
{
@@ -718,25 +745,8 @@ module_getattro(PyModuleObject *m, PyObject *name)
718745
_Py_IDENTIFIER(__name__);
719746
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
720747
if (mod_name && PyUnicode_Check(mod_name)) {
721-
_Py_IDENTIFIER(__spec__);
722-
_Py_IDENTIFIER(_initializing);
723-
PyObject *value = NULL;
724-
PyObject *spec;
725-
int initializing = 0;
726748
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) {
749+
if (_PyModule_IsInitializing((PyObject *)m)) {
740750
PyErr_Format(PyExc_AttributeError,
741751
"partially initialized "
742752
"module '%U' has no attribute '%U' "

Python/import.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,38 +1720,20 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
17201720

17211721
mod = PyImport_GetModule(abs_name);
17221722
if (mod != NULL && mod != Py_None) {
1723-
_Py_IDENTIFIER(__spec__);
1724-
_Py_IDENTIFIER(_initializing);
17251723
_Py_IDENTIFIER(_lock_unlock_module);
1726-
PyObject *value = NULL;
1727-
PyObject *spec;
1728-
int initializing = 0;
17291724

17301725
/* Optimization: only call _bootstrap._lock_unlock_module() if
17311726
__spec__._initializing is true.
17321727
NOTE: because of this, initializing must be set *before*
17331728
stuffing the new module in sys.modules.
17341729
*/
1735-
spec = _PyObject_GetAttrId(mod, &PyId___spec__);
1736-
if (spec != NULL) {
1737-
value = _PyObject_GetAttrId(spec, &PyId__initializing);
1738-
Py_DECREF(spec);
1739-
}
1740-
if (value == NULL)
1741-
PyErr_Clear();
1742-
else {
1743-
initializing = PyObject_IsTrue(value);
1730+
if (_PyModule_IsInitializing(mod)) {
1731+
PyObject *value = _PyObject_CallMethodIdObjArgs(interp->importlib,
1732+
&PyId__lock_unlock_module, abs_name,
1733+
NULL);
1734+
if (value == NULL)
1735+
goto error;
17441736
Py_DECREF(value);
1745-
if (initializing == -1)
1746-
PyErr_Clear();
1747-
if (initializing > 0) {
1748-
value = _PyObject_CallMethodIdObjArgs(interp->importlib,
1749-
&PyId__lock_unlock_module, abs_name,
1750-
NULL);
1751-
if (value == NULL)
1752-
goto error;
1753-
Py_DECREF(value);
1754-
}
17551737
}
17561738
}
17571739
else {

0 commit comments

Comments
 (0)