Skip to content

Commit 0a6693a

Browse files
[3.8] bpo-37409: fix relative import with no parent (GH-14956) (GH-15913)
Relative imports use resolve_name to get the absolute target name, which first seeks the current module's absolute package name from the globals: If __package__ (and __spec__.parent) are missing then import uses __name__, truncating the last segment if the module is a submodule rather than a package __init__.py (which it guesses from whether __path__ is defined). The __name__ attempt should fail if there is no parent package (top level modules), if __name__ is '__main__' (-m entry points), or both (scripts). That is, if both __name__ has no subcomponents and the module does not seem to be a package __init__ module then import should fail.. (cherry picked from commit 92420b3) Co-authored-by: Ben Lewis <benjimin@users.noreply.github.com>
1 parent 0ba5dbd commit 0a6693a

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

Lib/test/test_builtin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ def test_import(self):
160160
self.assertRaises(TypeError, __import__, 1, 2, 3, 4)
161161
self.assertRaises(ValueError, __import__, '')
162162
self.assertRaises(TypeError, __import__, 'sys', name='sys')
163+
# Relative import outside of a package with no __package__ or __spec__ (bpo-37409).
164+
self.assertRaises(ImportError, __import__, '',
165+
{'__package__': None, '__spec__': None, '__name__': '__main__'},
166+
locals={}, fromlist=('foo',), level=1)
163167
# embedded null character
164168
self.assertRaises(ModuleNotFoundError, __import__, 'string\x00')
165169

Lib/test/test_import/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,11 @@ def check_relative():
774774
ns = dict(__package__=object())
775775
self.assertRaises(TypeError, check_relative)
776776

777+
def test_parentless_import_shadowed_by_global(self):
778+
# Test as if this were done from the REPL where this error most commonly occurs (bpo-37409).
779+
script_helper.assert_python_failure('-W', 'ignore', '-c',
780+
"foo = 1; from . import foo")
781+
777782
def test_absolute_import_without_future(self):
778783
# If explicit relative import syntax is used, then do not try
779784
# to perform an absolute import in the face of failure.

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ Alain Leufroy
969969
Mark Levinson
970970
Mark Levitt
971971
Ivan Levkivskyi
972+
Ben Lewis
972973
William Lewis
973974
Akira Li
974975
Robert Li
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensure explicit relative imports from interactive sessions and scripts (having no parent package) always raise ImportError, rather than treating the current module as the package.
2+
Patch by Ben Lewis.

Python/import.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,22 +1601,20 @@ resolve_name(PyObject *name, PyObject *globals, int level)
16011601
if (dot == -2) {
16021602
goto error;
16031603
}
1604-
1605-
if (dot >= 0) {
1606-
PyObject *substr = PyUnicode_Substring(package, 0, dot);
1607-
if (substr == NULL) {
1608-
goto error;
1609-
}
1610-
Py_SETREF(package, substr);
1604+
else if (dot == -1) {
1605+
goto no_parent_error;
16111606
}
1607+
PyObject *substr = PyUnicode_Substring(package, 0, dot);
1608+
if (substr == NULL) {
1609+
goto error;
1610+
}
1611+
Py_SETREF(package, substr);
16121612
}
16131613
}
16141614

16151615
last_dot = PyUnicode_GET_LENGTH(package);
16161616
if (last_dot == 0) {
1617-
PyErr_SetString(PyExc_ImportError,
1618-
"attempted relative import with no known parent package");
1619-
goto error;
1617+
goto no_parent_error;
16201618
}
16211619

16221620
for (level_up = 1; level_up < level; level_up += 1) {
@@ -1642,6 +1640,11 @@ resolve_name(PyObject *name, PyObject *globals, int level)
16421640
Py_DECREF(base);
16431641
return abs_name;
16441642

1643+
no_parent_error:
1644+
PyErr_SetString(PyExc_ImportError,
1645+
"attempted relative import "
1646+
"with no known parent package");
1647+
16451648
error:
16461649
Py_XDECREF(package);
16471650
return NULL;

0 commit comments

Comments
 (0)