diff options
author | Ben Lewis <benjimin@users.noreply.github.com> | 2019-09-11 10:09:47 (GMT) |
---|---|---|
committer | Brett Cannon <54418+brettcannon@users.noreply.github.com> | 2019-09-11 10:09:47 (GMT) |
commit | 92420b3e679959a7d0ce875875601a4cee45231e (patch) | |
tree | 8e73ad151c044a4233f9b8cfa94127b477a2a33a /Lib/test/test_builtin.py | |
parent | 6472ece5a0fe82809d3aa0ffb281796fcd252d76 (diff) | |
download | cpython-92420b3e679959a7d0ce875875601a4cee45231e.zip cpython-92420b3e679959a7d0ce875875601a4cee45231e.tar.gz cpython-92420b3e679959a7d0ce875875601a4cee45231e.tar.bz2 |
bpo-37409: fix relative import with no parent (#14956)
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.
Diffstat (limited to 'Lib/test/test_builtin.py')
-rw-r--r-- | Lib/test/test_builtin.py | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 7b3c054..185d8f7 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -160,6 +160,10 @@ class BuiltinTest(unittest.TestCase): self.assertRaises(TypeError, __import__, 1, 2, 3, 4) self.assertRaises(ValueError, __import__, '') self.assertRaises(TypeError, __import__, 'sys', name='sys') + # Relative import outside of a package with no __package__ or __spec__ (bpo-37409). + self.assertRaises(ImportError, __import__, '', + {'__package__': None, '__spec__': None, '__name__': '__main__'}, + locals={}, fromlist=('foo',), level=1) # embedded null character self.assertRaises(ModuleNotFoundError, __import__, 'string\x00') |