summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/abc.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-08-10 16:21:12 (GMT)
committerBrett Cannon <brett@python.org>2012-08-10 16:21:12 (GMT)
commitf4dc9204cc406ab41c2d643c1a64375a6a2954e5 (patch)
treec0cf92198b707bac6a68b801305ad86b24eb814f /Lib/importlib/abc.py
parent2d6266d5f148549979df024459e29b73307b86c4 (diff)
downloadcpython-f4dc9204cc406ab41c2d643c1a64375a6a2954e5.zip
cpython-f4dc9204cc406ab41c2d643c1a64375a6a2954e5.tar.gz
cpython-f4dc9204cc406ab41c2d643c1a64375a6a2954e5.tar.bz2
Issue #15502: Finish bringing importlib.abc in line with the current
state of the import system. Also make importlib.invalidate_caches() work with sys.meta_path instead of sys.path_importer_cache to completely separate the path-based import system from the overall import system. Patch by Eric Snow.
Diffstat (limited to 'Lib/importlib/abc.py')
-rw-r--r--Lib/importlib/abc.py42
1 files changed, 28 insertions, 14 deletions
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
index e8a0541..cd687e8 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -25,26 +25,22 @@ def _register(abstract_cls, *classes):
class Finder(metaclass=abc.ABCMeta):
- """Common abstract base class for import finders.
+ """Legacy abstract base class for import finders.
- Finder implementations should derive from the more specific
- MetaPathFinder or PathEntryFinder ABCs rather than directly from Finder.
+ It may be subclassed for compatibility with legacy third party
+ reimplementations of the import system. Otherwise, finder
+ implementations should derive from the more specific MetaPathFinder
+ or PathEntryFinder ABCs.
"""
+ @abc.abstractmethod
def find_module(self, fullname, path=None):
- """An optional legacy method that should find a module.
+ """An abstract method that should find a module.
The fullname is a str and the optional path is a str or None.
Returns a Loader object.
-
- The path finder will use this method only if find_loader() does
- not exist. It may optionally be implemented for compatibility
- with legacy third party reimplementations of the import system.
"""
raise NotImplementedError
- # invalidate_caches() is a completely optional method, so no default
- # implementation is provided. See the docs for details.
-
class MetaPathFinder(Finder):
@@ -52,12 +48,18 @@ class MetaPathFinder(Finder):
@abc.abstractmethod
def find_module(self, fullname, path):
- """Abstract method which when implemented should find a module.
+ """Abstract method which, when implemented, should find a module.
The fullname is a str and the path is a str or None.
Returns a Loader object.
"""
raise NotImplementedError
+ def invalidate_caches(self):
+ """An optional method for clearing the finder's cache, if any.
+ This method is used by importlib.invalidate_caches().
+ """
+ return NotImplemented
+
_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
machinery.PathFinder, machinery.WindowsRegistryFinder)
@@ -68,13 +70,25 @@ class PathEntryFinder(Finder):
@abc.abstractmethod
def find_loader(self, fullname):
- """Abstract method which when implemented returns a module loader.
+ """Abstract method which, when implemented, returns a module loader.
The fullname is a str. Returns a 2-tuple of (Loader, portion) where
portion is a sequence of file system locations contributing to part of
- a namespace package. The sequence may be empty.
+ a namespace package. The sequence may be empty and the loader may be
+ None.
"""
raise NotImplementedError
+ def find_module(self, fullname):
+ """Compatibility function which is the equivalent of
+ self.find_loader(fullname)[0]."""
+ return self.find_loader(fullname)[0]
+
+ def invalidate_caches(self):
+ """An optional method for clearing the finder's cache, if any.
+ This method is used by PathFinder.invalidate_caches().
+ """
+ return NotImplemented
+
_register(PathEntryFinder, machinery.FileFinder)