[Python-checkins] bpo-20490: Improve circular import error message (GH-15308)

Steve Dower webhook-mailer at python.org
Mon Sep 9 12:45:21 EDT 2019


https://github.com/python/cpython/commit/2d5594fac21a81a06f82c3605318dfa96e72398f
commit: 2d5594fac21a81a06f82c3605318dfa96e72398f
branch: 3.8
author: Steve Dower <steve.dower at python.org>
committer: GitHub <noreply at github.com>
date: 2019年09月09日T09:45:18-07:00
summary:
bpo-20490: Improve circular import error message (GH-15308)
files:
A Lib/test/test_import/data/circular_imports/from_cycle1.py
A Lib/test/test_import/data/circular_imports/from_cycle2.py
A Misc/NEWS.d/next/Core and Builtins/2019-08-15-12-48-36.bpo-20490.-hXeEn.rst
M Lib/test/test_import/__init__.py
M Python/ceval.c
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 50406d9aa1d9..7c24f0e50232 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -1326,6 +1326,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.
diff --git a/Lib/test/test_import/data/circular_imports/from_cycle1.py b/Lib/test/test_import/data/circular_imports/from_cycle1.py
new file mode 100644
index 000000000000..aacfd5f46fca
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/from_cycle1.py
@@ -0,0 +1,2 @@
+from .from_cycle2 import a
+b = 1
diff --git a/Lib/test/test_import/data/circular_imports/from_cycle2.py b/Lib/test/test_import/data/circular_imports/from_cycle2.py
new file mode 100644
index 000000000000..62a66e1cfd02
--- /dev/null
+++ b/Lib/test/test_import/data/circular_imports/from_cycle2.py
@@ -0,0 +1,2 @@
+from .from_cycle1 import b
+a = 1
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-08-15-12-48-36.bpo-20490.-hXeEn.rst b/Misc/NEWS.d/next/Core and Builtins/2019-08-15-12-48-36.bpo-20490.-hXeEn.rst
new file mode 100644
index 000000000000..dfee480e1aa7
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-08-15-12-48-36.bpo-20490.-hXeEn.rst	
@@ -0,0 +1,2 @@
+Improve import error message for partially initialized module on circular
+``from`` imports - by Anthony Sottile.
diff --git a/Python/ceval.c b/Python/ceval.c
index 546a4264d8ad..07ec3293adf1 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);
 }


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /