Commit 4b03b686 authored by Brett Cannon's avatar Brett Cannon

Turn _return_module() into _handle_fromlist().

parent 6858cabb
...@@ -1001,7 +1001,7 @@ def _gcd_import(name, package=None, level=0): ...@@ -1001,7 +1001,7 @@ def _gcd_import(name, package=None, level=0):
return _find_and_load(name, _gcd_import) return _find_and_load(name, _gcd_import)
def _return_module(module, name, fromlist, level, import_): def _handle_fromlist(module, fromlist, import_):
"""Figure out what __import__ should return. """Figure out what __import__ should return.
The import_ parameter is a callable which takes the name of module to The import_ parameter is a callable which takes the name of module to
...@@ -1010,29 +1010,18 @@ def _return_module(module, name, fromlist, level, import_): ...@@ -1010,29 +1010,18 @@ def _return_module(module, name, fromlist, level, import_):
""" """
# The hell that is fromlist ... # The hell that is fromlist ...
if not fromlist: # If a package was imported, try to import stuff from fromlist.
# Return up to the first dot in 'name'. This is complicated by the fact if hasattr(module, '__path__'):
# that 'name' may be relative. if '*' in fromlist and hasattr(module, '__all__'):
if level == 0: fromlist = list(fromlist)
return sys.modules[name.partition('.')[0]] fromlist.remove('*')
elif not name: fromlist.extend(module.__all__)
return module for x in (y for y in fromlist if not hasattr(module,y)):
else: try:
cut_off = len(name) - len(name.partition('.')[0]) import_('{0}.{1}'.format(module.__name__, x))
return sys.modules[module.__name__[:-cut_off]] except ImportError:
else: pass
# If a package was imported, try to import stuff from fromlist. return module
if hasattr(module, '__path__'):
if '*' in fromlist and hasattr(module, '__all__'):
fromlist = list(fromlist)
fromlist.remove('*')
fromlist.extend(module.__all__)
for x in (y for y in fromlist if not hasattr(module,y)):
try:
import_('{0}.{1}'.format(module.__name__, x))
except ImportError:
pass
return module
def _calc___package__(globals): def _calc___package__(globals):
...@@ -1066,7 +1055,18 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0): ...@@ -1066,7 +1055,18 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
else: else:
package = _calc___package__(globals) package = _calc___package__(globals)
module = _gcd_import(name, package, level) module = _gcd_import(name, package, level)
return _return_module(module, name, fromlist, level, _gcd_import) if not fromlist:
# 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]]
elif not name:
return module
else:
cut_off = len(name) - len(name.partition('.')[0])
return sys.modules[module.__name__[:-cut_off]]
else:
return _handle_fromlist(module, fromlist, _gcd_import)
def _setup(sys_module, imp_module): def _setup(sys_module, imp_module):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment