mirror of
https://github.com/python/cpython.git
synced 2025-08-27 04:05:34 +00:00
Issue #14582: Import returns the module returned by a loader instead
of sys.modules when possible. This is being done for two reasons. One is to gain a little bit of performance by skipping an unnecessary dict lookup in sys.modules. But the other (and main) reason is to be a little bit more clear in how things should work from the perspective of import's interactions with loaders. Otherwise loaders can easily forget to return the module even though PEP 302 explicitly states they are expected to return the module they loaded.
This commit is contained in:
parent
27fc52877c
commit
881535b726
5 changed files with 497 additions and 503 deletions
|
@ -974,12 +974,12 @@ def _find_and_load(name, import_):
|
|||
loader = _find_module(name, path)
|
||||
if loader is None:
|
||||
raise ImportError(_ERR_MSG.format(name), name=name)
|
||||
elif name not in sys.modules:
|
||||
# The parent import may have already imported this module.
|
||||
loader.load_module(name)
|
||||
elif name in sys.modules:
|
||||
# The parent module already imported this module.
|
||||
module = sys.modules[name]
|
||||
else:
|
||||
module = loader.load_module(name)
|
||||
verbose_message('import {!r} # {!r}', name, loader)
|
||||
# Backwards-compatibility; be nicer to skip the dict lookup.
|
||||
module = sys.modules[name]
|
||||
if parent:
|
||||
# Set the module as an attribute on its parent.
|
||||
parent_module = sys.modules[parent]
|
||||
|
@ -1078,7 +1078,11 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
|
|||
# Return up to the first dot in 'name'. This is complicated by the fact
|
||||
# that 'name' may be relative.
|
||||
if level == 0:
|
||||
return sys.modules[name.partition('.')[0]]
|
||||
index = name.find('.')
|
||||
if index == -1:
|
||||
return module
|
||||
else:
|
||||
return sys.modules[name[:index]]
|
||||
elif not name:
|
||||
return module
|
||||
else:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue