Skip to content

bpo-20490: Improve circular import error messaging #15308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,16 @@ def test_crossreference2(self):
self.assertIn('partially initialized module', errmsg)
self.assertIn('circular import', errmsg)

def test_circular_from_import(self):
with self.assertRaises(ImportError) as cm:
import test.test_import.data.circular_imports.from_cycle1
self.assertIn(
"cannot import name 'b' from partially initialized module "
"'test.test_import.data.circular_imports.from_cycle1' "
"(most likely due to a circular import)",
str(cm.exception),
)


if __name__ == '__main__':
# Test needs to be a package, so we can do relative imports.
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_import/data/circular_imports/from_cycle1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .from_cycle2 import a
b = 1
2 changes: 2 additions & 0 deletions Lib/test/test_import/data/circular_imports/from_cycle2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .from_cycle1 import b
a = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve import error message for partially initialized module on circular
``from`` imports - by Anthony Sottile.
15 changes: 11 additions & 4 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5238,10 +5238,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);
}
Expand Down