• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Subset of importlib.abc used to reduce importlib.util imports."""
2from . import _bootstrap
3import abc
4import warnings
5
6
7class Loader(metaclass=abc.ABCMeta):
8
9    """Abstract base class for import loaders."""
10
11    def create_module(self, spec):
12        """Return a module to initialize and into which to load.
13
14        This method should raise ImportError if anything prevents it
15        from creating a new module.  It may return None to indicate
16        that the spec should create the new module.
17        """
18        # By default, defer to default semantics for the new module.
19        return None
20
21    # We don't define exec_module() here since that would break
22    # hasattr checks we do to support backward compatibility.
23
24    def load_module(self, fullname):
25        """Return the loaded module.
26
27        The module must be added to sys.modules and have import-related
28        attributes set properly.  The fullname is a str.
29
30        ImportError is raised on failure.
31
32        This method is deprecated in favor of loader.exec_module(). If
33        exec_module() exists then it is used to provide a backwards-compatible
34        functionality for this method.
35
36        """
37        if not hasattr(self, 'exec_module'):
38            raise ImportError
39        # Warning implemented in _load_module_shim().
40        return _bootstrap._load_module_shim(self, fullname)
41
42    def module_repr(self, module):
43        """Return a module's repr.
44
45        Used by the module type when the method does not raise
46        NotImplementedError.
47
48        This method is deprecated.
49
50        """
51        warnings.warn("importlib.abc.Loader.module_repr() is deprecated and "
52                      "slated for removal in Python 3.12", DeprecationWarning)
53        # The exception will cause ModuleType.__repr__ to ignore this method.
54        raise NotImplementedError
55