@@ -752,15 +752,15 @@ class PathFinder:
752752 """Meta path finder for sys.(path|path_hooks|path_importer_cache)."""
753753
754754 @classmethod
755- def _path_hooks (cls , path , hooks = None ):
755+ def _path_hooks (cls , path ):
756756 """Search sequence of hooks for a finder for 'path'.
757757
758758 If 'hooks' is false then use sys.path_hooks.
759759
760760 """
761- if hooks is None :
762- hooks = sys .path_hooks
763- for hook in hooks :
761+ if not sys . path_hooks :
762+ _warnings . warn ( ' sys.path_hooks is empty' , ImportWarning )
763+ for hook in sys . path_hooks :
764764 try :
765765 return hook (path )
766766 except ImportError :
@@ -770,14 +770,11 @@ def _path_hooks(cls, path, hooks=None):
770770 path = path )
771771
772772 @classmethod
773- def _path_importer_cache (cls , path , default = None ):
773+ def _path_importer_cache (cls , path ):
774774 """Get the finder for the path from sys.path_importer_cache.
775775
776776 If the path is not in the cache, find the appropriate finder and cache
777- it. If None is cached, get the default finder and cache that
778- (if applicable).
779-
780- Because of NullImporter, some finder should be returned. The only
777+ it. Because of NullImporter, some finder should be returned. The only
781778 explicit fail case is if None is cached but the path cannot be used for
782779 the default hook, for which ImportError is raised.
783780
@@ -790,9 +787,13 @@ def _path_importer_cache(cls, path, default=None):
790787 finder = cls ._path_hooks (path )
791788 sys .path_importer_cache [path ] = finder
792789 else :
793- if finder is None and default :
794- # Raises ImportError on failure.
795- finder = default (path )
790+ if finder is None :
791+ msg = ("'None' in sys.path_importer_cache[{!r}], so retrying "
792+ "finder search; in future versions of Python 'None' "
793+ "will represent no finder" .format (path ))
794+ _warnings .warn (msg , ImportWarning )
795+ del sys .path_importer_cache [path ]
796+ finder = cls ._path_hooks (path )
796797 sys .path_importer_cache [path ] = finder
797798 return finder
798799
@@ -931,29 +932,6 @@ def path_hook_for_FileFinder(path):
931932
932933# Import itself ###############################################################
933934
934- _DEFAULT_PATH_HOOK = None # Set in _setup()
935-
936- class _DefaultPathFinder (PathFinder ):
937-
938- """Subclass of PathFinder that implements implicit semantics for
939- __import__."""
940-
941- @classmethod
942- def _path_hooks (cls , path ):
943- """Search sys.path_hooks as well as implicit path hooks."""
944- try :
945- return super ()._path_hooks (path )
946- except ImportError :
947- implicit_hooks = [_DEFAULT_PATH_HOOK , _imp .NullImporter ]
948- return super ()._path_hooks (path , implicit_hooks )
949-
950- @classmethod
951- def _path_importer_cache (cls , path ):
952- """Use the default path hook when None is stored in
953- sys.path_importer_cache."""
954- return super ()._path_importer_cache (path , _DEFAULT_PATH_HOOK )
955-
956-
957935class _ImportLockContext :
958936
959937 """Context manager for the import lock."""
@@ -1008,7 +986,7 @@ def _sanity_check(name, package, level):
1008986 raise ValueError ("Empty module name" )
1009987
1010988
1011- _IMPLICIT_META_PATH = [BuiltinImporter , FrozenImporter , _DefaultPathFinder ]
989+ _IMPLICIT_META_PATH = [BuiltinImporter , FrozenImporter , PathFinder ]
1012990
1013991_ERR_MSG = 'No module named {!r}'
1014992
@@ -1203,12 +1181,6 @@ def _setup(sys_module, _imp_module):
12031181 if builtin_os == 'nt' :
12041182 SOURCE_SUFFIXES .append ('.pyw' )
12051183
1206- supported_loaders = [(ExtensionFileLoader , _suffix_list (3 ), False ),
1207- (SourceFileLoader , _suffix_list (1 ), True ),
1208- (SourcelessFileLoader , _suffix_list (2 ), True )]
1209- setattr (self_module , '_DEFAULT_PATH_HOOK' ,
1210- FileFinder .path_hook (* supported_loaders ))
1211-
12121184
12131185def _install (sys_module , _imp_module ):
12141186 """Install importlib as the implementation of import.
@@ -1218,6 +1190,8 @@ def _install(sys_module, _imp_module):
12181190
12191191 """
12201192 _setup (sys_module , _imp_module )
1221- orig_import = builtins .__import__
1222- builtins .__import__ = __import__
1223- builtins .__original_import__ = orig_import
1193+ supported_loaders = [(ExtensionFileLoader , _suffix_list (3 ), False ),
1194+ (SourceFileLoader , _suffix_list (1 ), True ),
1195+ (SourcelessFileLoader , _suffix_list (2 ), True )]
1196+ sys .path_hooks .extend ([FileFinder .path_hook (* supported_loaders ),
1197+ _imp .NullImporter ])
0 commit comments