summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@python.org>2019-09-09 16:45:18 (GMT)
committerGitHub <noreply@github.com>2019-09-09 16:45:18 (GMT)
commit2d5594fac21a81a06f82c3605318dfa96e72398f (patch)
treea517131bd3e842634fac53510874875bb104b3cf /Python
parent78d15faf6c522619098e94be3e7f6d88a9e61123 (diff)
downloadcpython-2d5594fac21a81a06f82c3605318dfa96e72398f.zip
cpython-2d5594fac21a81a06f82c3605318dfa96e72398f.tar.gz
cpython-2d5594fac21a81a06f82c3605318dfa96e72398f.tar.bz2
bpo-20490: Improve circular import error message (GH-15308)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 546a426..07ec329 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5236,10 +5236,17 @@ import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
PyErr_SetImportError(errmsg, pkgname, NULL);
}
else {
- errmsg = PyUnicode_FromFormat(
- "cannot import name %R from %R (%S)",
- name, pkgname_or_unknown, pkgpath
- );
+ _Py_IDENTIFIER(__spec__);
+ PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
+ Py_XINCREF(spec);
+ const char *fmt =
+ _PyModuleSpec_IsInitializing(spec) ?
+ "cannot import name %R from partially initialized module %R "
+ "(most likely due to a circular import) (%S)" :
+ "cannot import name %R from %R (%S)";
+ Py_XDECREF(spec);
+
+ errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
/* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
PyErr_SetImportError(errmsg, pkgname, pkgpath);
}