summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/abc.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-08-02 11:26:03 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-08-02 11:26:03 (GMT)
commit8a9080feffc757360d0d73e4173189586098ffb5 (patch)
tree9199a6a30f436fcc310ad16ff64f145f00822e2c /Lib/importlib/abc.py
parenta90f311d0592f6ab56068441413a1925bd7393f4 (diff)
downloadcpython-8a9080feffc757360d0d73e4173189586098ffb5.zip
cpython-8a9080feffc757360d0d73e4173189586098ffb5.tar.gz
cpython-8a9080feffc757360d0d73e4173189586098ffb5.tar.bz2
Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Diffstat (limited to 'Lib/importlib/abc.py')
-rw-r--r--Lib/importlib/abc.py70
1 files changed, 49 insertions, 21 deletions
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
index 7fcf2de..5e71758 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -23,48 +23,76 @@ def _register(abstract_cls, *classes):
abstract_cls.register(frozen_cls)
-class Loader(metaclass=abc.ABCMeta):
+class Finder(metaclass=abc.ABCMeta):
- """Abstract base class for import loaders."""
+ """Common abstract base class for import finders.
- @abc.abstractmethod
- def load_module(self, fullname):
- """Abstract method which when implemented should load a module.
- The fullname is a str."""
+ Finder implementations should derive from the more specific
+ MetaPathFinder or PathEntryFinder ABCs rather than directly from Finder.
+ """
+
+ def find_module(self, fullname, path=None):
+ """An optional legacy 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):
+
+ """Abstract base class for import finders on sys.meta_path."""
+
@abc.abstractmethod
- def module_repr(self, module):
- """Abstract method which when implemented calculates and returns the
- given module's repr."""
+ def find_module(self, fullname, path):
+ """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
+_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
+ machinery.PathFinder)
-class Finder(metaclass=abc.ABCMeta):
- """Abstract base class for import finders."""
+class PathEntryFinder(Finder):
+
+ """Abstract base class for path entry finders used by PathFinder."""
@abc.abstractmethod
def find_loader(self, fullname):
"""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. When present,
- `find_loader()` is preferred over `find_module()`.
+ a namespace package. The sequence may be empty.
"""
raise NotImplementedError
+_register(PathEntryFinder, machinery.FileFinder)
+
+
+class Loader(metaclass=abc.ABCMeta):
+
+ """Abstract base class for import loaders."""
+
@abc.abstractmethod
- def find_module(self, fullname, path=None):
- """Abstract method which when implemented should find a module.
- The fullname is a str and the optional path is a str or None.
- Returns a Loader object. This method is only called if
- `find_loader()` is not present.
- """
+ def load_module(self, fullname):
+ """Abstract method which when implemented should load a module.
+ The fullname is a str."""
raise NotImplementedError
-_register(Finder, machinery.BuiltinImporter, machinery.FrozenImporter,
- machinery.PathFinder, machinery.FileFinder)
+ @abc.abstractmethod
+ def module_repr(self, module):
+ """Abstract method which when implemented calculates and returns the
+ given module's repr."""
+ raise NotImplementedError
class ResourceLoader(Loader):