Commit f7e5a8c7 authored by Brett Cannon's avatar Brett Cannon

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

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