Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(166815)

Unified Diff: Python/ceval.c

Issue 23447: Import fails when doing a circular import involving an `import *`
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_import/__init__.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
index 8d2cdc2..a3b73bc 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5110,8 +5110,10 @@ import_all_from(PyObject *locals, PyObject *v)
{
_Py_IDENTIFIER(__all__);
_Py_IDENTIFIER(__dict__);
+ _Py_IDENTIFIER(__name__);
PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
PyObject *dict, *name, *value;
+ PyObject *fullmodname, *pkgname;
int skip_leading_underscores = 0;
int pos, err;
@@ -5152,8 +5154,32 @@ import_all_from(PyObject *locals, PyObject *v)
continue;
}
value = PyObject_GetAttr(v, name);
- if (value == NULL)
+ if (value == NULL && PyErr_ExceptionMatches(PyExc_AttributeError))
+ {
+ /* Issue #23477: if a circular import happens within a wildcard
+ import, some partially loaded modules may not have been saved
+ to the package's __dict__ yet, so we'll try getting them from
+ sys.modules before giving up.
+ */
+ PyErr_Clear();
+ pkgname = _PyObject_GetAttrId(v, &PyId___name__);
+ if (pkgname != NULL && PyUnicode_Check(pkgname))
+ {
+ fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
+ if (fullmodname != NULL)
+ {
+ value = PyDict_GetItem(PyImport_GetModuleDict(),
+ fullmodname);
+ Py_XINCREF(value);
+ Py_DECREF(fullmodname);
+ }
+ Py_DECREF(pkgname);
+ }
+ }
+ if (value == NULL) {
+ PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
err = -1;
+ }
else if (PyDict_CheckExact(locals))
err = PyDict_SetItem(locals, name, value);
else
« no previous file with comments | « Lib/test/test_import/__init__.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+