diff options
author | Brett Cannon <brett@python.org> | 2012-04-22 23:58:33 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-04-22 23:58:33 (GMT) |
commit | 938d44d59c9cc142d35f51a908cabf781b482f26 (patch) | |
tree | 93d2bd601f966f742ef2f6caca7b374e84f6a0b2 /Lib/importlib/abc.py | |
parent | 8c5e920ae3e98ebc5b37a105cf86e4c1e9649f57 (diff) | |
download | cpython-938d44d59c9cc142d35f51a908cabf781b482f26.zip cpython-938d44d59c9cc142d35f51a908cabf781b482f26.tar.gz cpython-938d44d59c9cc142d35f51a908cabf781b482f26.tar.bz2 |
Issue #14605: Expose importlib.abc.FileLoader and
importlib.machinery.(FileFinder, SourceFileLoader,
_SourcelessFileLoader, ExtensionFileLoader).
This exposes all of importlib's mechanisms that will become public on
the sys module.
Diffstat (limited to 'Lib/importlib/abc.py')
-rw-r--r-- | Lib/importlib/abc.py | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py index 43e4866..baa09fd 100644 --- a/Lib/importlib/abc.py +++ b/Lib/importlib/abc.py @@ -1,6 +1,12 @@ """Abstract base classes related to import.""" from . import _bootstrap from . import machinery +try: + import _frozen_importlib +except ImportError as exc: + if exc.name != '_frozen_importlib': + raise + _frozen_importlib = None import abc import imp import marshal @@ -9,6 +15,14 @@ import tokenize import warnings +def _register(abstract_cls, *classes): + for cls in classes: + abstract_cls.register(cls) + if _frozen_importlib is not None: + frozen_cls = getattr(_frozen_importlib, cls.__name__) + abstract_cls.register(frozen_cls) + + class Loader(metaclass=abc.ABCMeta): """Abstract base class for import loaders.""" @@ -32,9 +46,8 @@ class Finder(metaclass=abc.ABCMeta): """ raise NotImplementedError -Finder.register(machinery.BuiltinImporter) -Finder.register(machinery.FrozenImporter) -Finder.register(machinery.PathFinder) +_register(Finder, machinery.BuiltinImporter, machinery.FrozenImporter, + machinery.PathFinder, machinery.FileFinder) class ResourceLoader(Loader): @@ -80,8 +93,8 @@ class InspectLoader(Loader): module. The fullname is a str. Returns a str.""" raise NotImplementedError -InspectLoader.register(machinery.BuiltinImporter) -InspectLoader.register(machinery.FrozenImporter) +_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, + machinery.ExtensionFileLoader) class ExecutionLoader(InspectLoader): @@ -100,6 +113,15 @@ class ExecutionLoader(InspectLoader): raise NotImplementedError +class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader): + + """Abstract base class partially implementing the ResourceLoader and + ExecutionLoader ABCs.""" + +_register(FileLoader, machinery.SourceFileLoader, + machinery._SourcelessFileLoader) + + class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader): """Abstract base class for loading source code (and optionally any @@ -146,6 +168,7 @@ class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader): """ raise NotImplementedError +_register(SourceLoader, machinery.SourceFileLoader) class PyLoader(SourceLoader): |