mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
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.
This commit is contained in:
parent
8c5e920ae3
commit
938d44d59c
17 changed files with 3246 additions and 3076 deletions
|
@ -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):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue