Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of

importlib.machinery. Also make the methods either class or staticmethods.
This commit is contained in:
Brett Cannon 2009-02-05 02:52:18 +00:00
parent be2f57c39a
commit f7e5a8c724
1 changed files with 16 additions and 18 deletions

View File

@ -589,33 +589,29 @@ class PyFileImporter(FileImporter):
super(PyFileImporter, self).__init__(path_entry)
class SysPathFinder:
class PathFinder:
"""Meta path finder for sys.(path|path_hooks|path_importer_cache)."""
def _default_hook(self, path):
"""Use the default hook on 'path'.
If the path will not work for the default hook then raise ImportError.
"""
# TODO(brett.cannon) Implement
raise ImportError
_default_hook = staticmethod(chaining_fs_path_hook(ExtensionFileImporter,
PyFileImporter))
# The list of implicit hooks cannot be a class attribute because of
# bootstrapping issues for accessing imp.
def _implicit_hooks(self, path):
@classmethod
def _implicit_hooks(cls):
"""Return a list of the implicit path hooks."""
return [self._default_hook, imp.NullImporter]
def _path_hooks(self, path):
@classmethod
def _path_hooks(cls, path):
"""Search sys.path_hooks for a finder for 'path'.
Guaranteed to return a finder for the path as NullImporter is the
default importer for any path that does not have an explicit finder.
"""
for hook in sys.path_hooks + self._implicit_hooks():
for hook in sys.path_hooks + cls._implicit_hooks():
try:
return hook(path)
except ImportError:
@ -625,7 +621,8 @@ class SysPathFinder:
raise SystemError("no hook could find an importer for "
"{0}".format(path))
def _path_importer_cache(self, path):
@classmethod
def _path_importer_cache(cls, path):
"""Get the finder for the path from sys.path_importer_cache.
If the path is not in the cache, find the appropriate finder and cache
@ -638,24 +635,25 @@ class SysPathFinder:
"""
try:
finder = sys.path_importer_cache(path);
finder = sys.path_importer_cache[path]
except KeyError:
finder = self._path_hooks(path)
finder = cls._path_hooks(path)
sys.path_importer_cache[path] = finder
else:
if finder is None:
# Raises ImportError on failure.
finder = self._default_hook(path)
finder = cls._default_hook(path)
sys.path_importer_cache[path] = finder
return finder
def find_module(self, fullname, path=None):
@classmethod
def find_module(cls, fullname, path=None):
"""Find the module on sys.path or 'path'."""
if not path:
path = sys.path
for entry in path:
try:
finder = self._path_importer_cache(entry)
finder = cls._path_importer_cache(entry)
except ImportError:
continue
loader = finder.find_module(fullname)