@@ -1001,7 +1001,7 @@ def _gcd_import(name, package=None, level=0):
10011001 return _find_and_load (name , _gcd_import )
10021002
10031003
1004- def _return_module (module , name , fromlist , level , import_ ):
1004+ def _handle_fromlist (module , fromlist , import_ ):
10051005 """Figure out what __import__ should return.
10061006
10071007 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_):
10101010
10111011 """
10121012 # The hell that is fromlist ...
1013- if not fromlist :
1014- # Return up to the first dot in 'name'. This is complicated by the fact
1015- # that 'name' may be relative.
1016- if level == 0 :
1017- return sys .modules [name .partition ('.' )[0 ]]
1018- elif not name :
1019- return module
1020- else :
1021- cut_off = len (name ) - len (name .partition ('.' )[0 ])
1022- return sys .modules [module .__name__ [:- cut_off ]]
1023- else :
1024- # If a package was imported, try to import stuff from fromlist.
1025- if hasattr (module , '__path__' ):
1026- if '*' in fromlist and hasattr (module , '__all__' ):
1027- fromlist = list (fromlist )
1028- fromlist .remove ('*' )
1029- fromlist .extend (module .__all__ )
1030- for x in (y for y in fromlist if not hasattr (module ,y )):
1031- try :
1032- import_ ('{0}.{1}' .format (module .__name__ , x ))
1033- except ImportError :
1034- pass
1035- return module
1013+ # If a package was imported, try to import stuff from fromlist.
1014+ if hasattr (module , '__path__' ):
1015+ if '*' in fromlist and hasattr (module , '__all__' ):
1016+ fromlist = list (fromlist )
1017+ fromlist .remove ('*' )
1018+ fromlist .extend (module .__all__ )
1019+ for x in (y for y in fromlist if not hasattr (module ,y )):
1020+ try :
1021+ import_ ('{0}.{1}' .format (module .__name__ , x ))
1022+ except ImportError :
1023+ pass
1024+ return module
10361025
10371026
10381027def _calc___package__ (globals ):
@@ -1066,7 +1055,18 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
10661055 else :
10671056 package = _calc___package__ (globals )
10681057 module = _gcd_import (name , package , level )
1069- return _return_module (module , name , fromlist , level , _gcd_import )
1058+ if not fromlist :
1059+ # Return up to the first dot in 'name'. This is complicated by the fact
1060+ # that 'name' may be relative.
1061+ if level == 0 :
1062+ return sys .modules [name .partition ('.' )[0 ]]
1063+ elif not name :
1064+ return module
1065+ else :
1066+ cut_off = len (name ) - len (name .partition ('.' )[0 ])
1067+ return sys .modules [module .__name__ [:- cut_off ]]
1068+ else :
1069+ return _handle_fromlist (module , fromlist , _gcd_import )
10701070
10711071
10721072def _setup (sys_module , imp_module ):
0 commit comments