Skip to content

Commit f93234b

Browse files
bpo-30024: Circular imports involving absolute imports with binding (python#1264)
a submodule to a name are now supported.
1 parent dbdea62 commit f93234b

File tree

6 files changed

+17
-2
lines changed

6 files changed

+17
-2
lines changed

Doc/whatsnew/3.7.rst

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ Other Language Changes
8585
* :exc:`ImportError` now displays module name and module ``__file__`` path when
8686
``from ... import ...`` fails. (Contributed by Matthias Bussonnier in :issue:`29546`.)
8787

88+
* Circular imports involving absolute imports with binding a submodule to
89+
a name are now supported.
90+
(Contributed by Serhiy Storchaka in :issue:`30024`.)
91+
8892

8993
New Modules
9094
===========

Lib/test/test_import/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,12 @@ def test_rebinding(self):
11681168
from test.test_import.data.circular_imports.subpkg import util
11691169
self.assertIs(util.util, rebinding.util)
11701170

1171+
def test_binding(self):
1172+
try:
1173+
import test.test_import.data.circular_imports.binding
1174+
except ImportError:
1175+
self.fail('circular import with binding a submodule to a name failed')
1176+
11711177

11721178
if __name__ == '__main__':
11731179
# Test needs to be a package, so we can do relative imports.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import test.test_import.data.circular_imports.binding2 as binding2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import test.test_import.data.circular_imports.binding as binding

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-30024: Circular imports involving absolute imports with binding
14+
a submodule to a name are now supported.
15+
1316
- bpo-12414: sys.getsizeof() on a code object now returns the sizes
1417
which includes the code struct and sizes of objects which it references.
1518
Patch by Dong-hee Na.

Python/compile.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,7 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
25462546
merely needs to bind the result to a name.
25472547
25482548
If there is a dot in name, we need to split it and emit a
2549-
LOAD_ATTR for each name.
2549+
IMPORT_FROM for each name.
25502550
*/
25512551
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
25522552
PyUnicode_GET_LENGTH(name), 1);
@@ -2566,7 +2566,7 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
25662566
PyUnicode_GET_LENGTH(name));
25672567
if (!attr)
25682568
return 0;
2569-
ADDOP_O(c, LOAD_ATTR, attr, names);
2569+
ADDOP_O(c, IMPORT_FROM, attr, names);
25702570
Py_DECREF(attr);
25712571
pos = dot + 1;
25722572
}

0 commit comments

Comments
 (0)