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

Skip to content

Commit 2d5594f

Browse files
authored
bpo-20490: Improve circular import error message (GH-15308)
1 parent 78d15fa commit 2d5594f

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

Lib/test/test_import/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,16 @@ def test_crossreference2(self):
13261326
self.assertIn('partially initialized module', errmsg)
13271327
self.assertIn('circular import', errmsg)
13281328

1329+
def test_circular_from_import(self):
1330+
with self.assertRaises(ImportError) as cm:
1331+
import test.test_import.data.circular_imports.from_cycle1
1332+
self.assertIn(
1333+
"cannot import name 'b' from partially initialized module "
1334+
"'test.test_import.data.circular_imports.from_cycle1' "
1335+
"(most likely due to a circular import)",
1336+
str(cm.exception),
1337+
)
1338+
13291339

13301340
if __name__ == '__main__':
13311341
# Test needs to be a package, so we can do relative imports.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .from_cycle2 import a
2+
b = 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .from_cycle1 import b
2+
a = 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve import error message for partially initialized module on circular
2+
``from`` imports - by Anthony Sottile.

Python/ceval.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5236,10 +5236,17 @@ import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
52365236
PyErr_SetImportError(errmsg, pkgname, NULL);
52375237
}
52385238
else {
5239-
errmsg = PyUnicode_FromFormat(
5240-
"cannot import name %R from %R (%S)",
5241-
name, pkgname_or_unknown, pkgpath
5242-
);
5239+
_Py_IDENTIFIER(__spec__);
5240+
PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
5241+
Py_XINCREF(spec);
5242+
const char *fmt =
5243+
_PyModuleSpec_IsInitializing(spec) ?
5244+
"cannot import name %R from partially initialized module %R "
5245+
"(most likely due to a circular import) (%S)" :
5246+
"cannot import name %R from %R (%S)";
5247+
Py_XDECREF(spec);
5248+
5249+
errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
52435250
/* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
52445251
PyErr_SetImportError(errmsg, pkgname, pkgpath);
52455252
}

0 commit comments

Comments
 (0)