Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
65366bc8
Commit
65366bc8
authored
Sep 09, 2019
by
Anthony Sottile
Committed by
Steve Dower
Sep 09, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-20490: Improve circular import error message (GH-15308)
parent
88b24f96
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
4 deletions
+27
-4
Lib/test/test_import/__init__.py
Lib/test/test_import/__init__.py
+10
-0
Lib/test/test_import/data/circular_imports/from_cycle1.py
Lib/test/test_import/data/circular_imports/from_cycle1.py
+2
-0
Lib/test/test_import/data/circular_imports/from_cycle2.py
Lib/test/test_import/data/circular_imports/from_cycle2.py
+2
-0
Misc/NEWS.d/next/Core and Builtins/2019-08-15-12-48-36.bpo-20490.-hXeEn.rst
...ore and Builtins/2019-08-15-12-48-36.bpo-20490.-hXeEn.rst
+2
-0
Python/ceval.c
Python/ceval.c
+11
-4
No files found.
Lib/test/test_import/__init__.py
View file @
65366bc8
...
...
@@ -1324,6 +1324,16 @@ class CircularImportTests(unittest.TestCase):
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.
...
...
Lib/test/test_import/data/circular_imports/from_cycle1.py
0 → 100644
View file @
65366bc8
from
.from_cycle2
import
a
b
=
1
Lib/test/test_import/data/circular_imports/from_cycle2.py
0 → 100644
View file @
65366bc8
from
.from_cycle1
import
b
a
=
1
Misc/NEWS.d/next/Core and Builtins/2019-08-15-12-48-36.bpo-20490.-hXeEn.rst
0 → 100644
View file @
65366bc8
Improve import error message for partially initialized module on circular
``from`` imports - by Anthony Sottile.
Python/ceval.c
View file @
65366bc8
...
...
@@ -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
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment