summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/abc.py
blob: 2ecb8218ba4f8d51c4fe06fd3509bdc1c0765afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Abstract base classes related to import."""
from . import _bootstrap
from . import machinery
import abc
import types


class Loader(metaclass=abc.ABCMeta):

    """Abstract base class for import loaders.

    See PEP 302 for details.

    """

    def load_module(self, fullname:str) -> types.ModuleType:
        raise NotImplementedError

Loader.register(machinery.BuiltinImporter)
Loader.register(machinery.FrozenImporter)


class Finder(metaclass=abc.ABCMeta):

    """Abstract base class for import finders.

    See PEP 302 for details.

    """

    @abc.abstractmethod
    def find_module(self, fullname:str, path:[str]=None) -> Loader:
        raise NotImplementedError

Finder.register(machinery.BuiltinImporter)
Finder.register(machinery.FrozenImporter)
Finder.register(machinery.PathFinder)


class Importer(Finder, Loader):

    """Abstract base class for importers."""



class ResourceLoader(Loader):

    """Abstract base class for loaders which can return data from the back-end
    storage.

    This ABC represents one of the optional protocols specified by PEP 302.

    """

    @abc.abstractmethod
    def get_data(self, path:str) -> bytes:
        raise NotImplementedError


class InspectLoader(Loader):

    """Abstract base class for loaders which supports introspection.

    This ABC represents one of the optional protocols specified by PEP 302.

    """

    @abc.abstractmethod
    def is_package(self, fullname:str) -> bool:
        return NotImplementedError

    @abc.abstractmethod
    def get_code(self, fullname:str) -> types.CodeType:
        return NotImplementedError

    @abc.abstractmethod
    def get_source(self, fullname:str) -> str:
        return NotImplementedError


class PyLoader(_bootstrap.PyLoader, InspectLoader):

    """Abstract base class that implements the core parts needed to load Python
    source code."""

    # load_module and get_code are implemented.

    @abc.abstractmethod
    def source_path(self, fullname:str) -> object:
        raise NotImplementedError


class PyPycLoader(_bootstrap.PyPycLoader, PyLoader):

    """Abstract base class that implements the core parts needed to load Python
    source and bytecode."""

    # Implements load_module and get_code.

    @abc.abstractmethod
    def source_mtime(self, fullname:str) -> int:
        raise NotImplementedError

    @abc.abstractmethod
    def bytecode_path(self, fullname:str) -> object:
        raise NotImplementedError

    @abc.abstractmethod
    def write_bytecode(self, fullname:str, bytecode:bytes):
        raise NotImplementedError