_abc.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Subset of importlib.abc used to reduce importlib.util imports."""
  2. from . import _bootstrap
  3. import abc
  4. class Loader(metaclass=abc.ABCMeta):
  5. """Abstract base class for import loaders."""
  6. def create_module(self, spec):
  7. """Return a module to initialize and into which to load.
  8. This method should raise ImportError if anything prevents it
  9. from creating a new module. It may return None to indicate
  10. that the spec should create the new module.
  11. """
  12. # By default, defer to default semantics for the new module.
  13. return None
  14. # We don't define exec_module() here since that would break
  15. # hasattr checks we do to support backward compatibility.
  16. def load_module(self, fullname):
  17. """Return the loaded module.
  18. The module must be added to sys.modules and have import-related
  19. attributes set properly. The fullname is a str.
  20. ImportError is raised on failure.
  21. This method is deprecated in favor of loader.exec_module(). If
  22. exec_module() exists then it is used to provide a backwards-compatible
  23. functionality for this method.
  24. """
  25. if not hasattr(self, 'exec_module'):
  26. raise ImportError
  27. # Warning implemented in _load_module_shim().
  28. return _bootstrap._load_module_shim(self, fullname)