]> granicus.if.org Git - python/commit
bpo-37409: fix relative import with no parent (#14956)
authorBen Lewis <benjimin@users.noreply.github.com>
Wed, 11 Sep 2019 10:09:47 +0000 (20:09 +1000)
committerBrett Cannon <54418+brettcannon@users.noreply.github.com>
Wed, 11 Sep 2019 10:09:47 +0000 (11:09 +0100)
commit92420b3e679959a7d0ce875875601a4cee45231e
tree8e73ad151c044a4233f9b8cfa94127b477a2a33a
parent6472ece5a0fe82809d3aa0ffb281796fcd252d76
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.
Lib/test/test_builtin.py
Lib/test/test_import/__init__.py
Misc/ACKS
Misc/NEWS.d/next/Core and Builtins/2019-08-06-23-39-05.bpo-37409.1qwzn2.rst [new file with mode: 0644]
Python/import.c