diff options
author | Anthony Sottile <asottile@umich.edu> | 2019-09-09 15:17:50 (GMT) |
---|---|---|
committer | Steve Dower <steve.dower@python.org> | 2019-09-09 15:17:50 (GMT) |
commit | 65366bc8bdc4716ebc361e622590b45a6e5aef07 (patch) | |
tree | 520bd77224c7ecb24bd2851f15cf91ecc3b9d924 /Python | |
parent | 88b24f96aedbe546d7d3248089d94e874edc0e11 (diff) | |
download | cpython-65366bc8bdc4716ebc361e622590b45a6e5aef07.zip cpython-65366bc8bdc4716ebc361e622590b45a6e5aef07.tar.gz cpython-65366bc8bdc4716ebc361e622590b45a6e5aef07.tar.bz2 |
bpo-20490: Improve circular import error message (GH-15308)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index f9e03b3..d280d79 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -5233,10 +5233,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); } |