annotate Lib/importlib/abc.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 7d658b49b3e1
children f82ebf9b3a52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 """Abstract base classes related to import."""
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2 from . import _bootstrap
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3 from . import machinery
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
4 from . import util
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5 import abc
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
6 import imp
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
7 import io
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
8 import marshal
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
9 import os.path
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
10 import sys
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
11 import tokenize
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12 import types
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
13 import warnings
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
14
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16 class Loader(metaclass=abc.ABCMeta):
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
17
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
18 """Abstract base class for import loaders."""
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
19
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
20 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
21 def load_module(self, fullname):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
22 """Abstract method which when implemented should load a module.
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
23 The fullname is a str."""
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
24 raise NotImplementedError
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
25
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
26
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
27 class Finder(metaclass=abc.ABCMeta):
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
28
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
29 """Abstract base class for import finders."""
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
30
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
31 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
32 def find_module(self, fullname, path=None):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
33 """Abstract method which when implemented should find a module.
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
34 The fullname is a str and the optional path is a str or None.
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
35 Returns a Loader object.
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
36 """
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
37 raise NotImplementedError
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
38
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
39 Finder.register(machinery.BuiltinImporter)
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
40 Finder.register(machinery.FrozenImporter)
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
41 Finder.register(machinery.PathFinder)
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
42
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
43
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
44 class ResourceLoader(Loader):
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
45
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
46 """Abstract base class for loaders which can return data from their
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
47 back-end storage.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
48
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
49 This ABC represents one of the optional protocols specified by PEP 302.
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
50
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
51 """
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
52
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
53 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
54 def get_data(self, path):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
55 """Abstract method which when implemented should return the bytes for
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
56 the specified path. The path must be a str."""
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
57 raise NotImplementedError
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
58
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
60 class InspectLoader(Loader):
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
61
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
62 """Abstract base class for loaders which support inspection about the
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
63 modules they can load.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
64
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
65 This ABC represents one of the optional protocols specified by PEP 302.
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
66
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
67 """
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
68
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
69 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
70 def is_package(self, fullname):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
71 """Abstract method which when implemented should return whether the
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
72 module is a package. The fullname is a str. Returns a bool."""
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
73 raise NotImplementedError
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
74
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
75 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
76 def get_code(self, fullname):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
77 """Abstract method which when implemented should return the code object
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
78 for the module. The fullname is a str. Returns a types.CodeType."""
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
79 raise NotImplementedError
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
80
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
81 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
82 def get_source(self, fullname):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
83 """Abstract method which should return the source code for the
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
84 module. The fullname is a str. Returns a str."""
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
85 raise NotImplementedError
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
86
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
87 InspectLoader.register(machinery.BuiltinImporter)
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
88 InspectLoader.register(machinery.FrozenImporter)
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
89
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
90
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
91 class ExecutionLoader(InspectLoader):
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
92
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
93 """Abstract base class for loaders that wish to support the execution of
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
94 modules as scripts.
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
95
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
96 This ABC represents one of the optional protocols specified in PEP 302.
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
97
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
98 """
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
99
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
100 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
101 def get_filename(self, fullname):
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
102 """Abstract method which should return the value that __file__ is to be
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
103 set to."""
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
104 raise NotImplementedError
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
105
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
106
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 62581
diff changeset
107 class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
108
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
109 """Abstract base class for loading source code (and optionally any
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
110 corresponding bytecode).
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
111
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
112 To support loading from source code, the abstractmethods inherited from
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
113 ResourceLoader and ExecutionLoader need to be implemented. To also support
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
114 loading from bytecode, the optional methods specified directly by this ABC
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
115 is required.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
116
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
117 Inherited abstractmethods not implemented in this ABC:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
118
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
119 * ResourceLoader.get_data
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
120 * ExecutionLoader.get_filename
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
121
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
122 """
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
123
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
124 def path_mtime(self, path):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
125 """Return the (int) modification time for the path (str)."""
64236
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
126 raise NotImplementedError
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
127
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
128 def set_data(self, path, data):
64236
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
129 """Write the bytes to the path (if possible).
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
130
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
131 Accepts a str path and data as bytes.
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
132
64236
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
133 Any needed intermediary directories are to be created. If for some
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
134 reason the file cannot be written because of permissions, fail
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
135 silently.
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
136
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
137 """
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
138 raise NotImplementedError
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
139
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
140
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
141 class PyLoader(SourceLoader):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
142
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
143 """Implement the deprecated PyLoader ABC in terms of SourceLoader.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
144
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
145 This class has been deprecated! It is slated for removal in Python 3.4.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
146 If compatibility with Python 3.1 is not needed then implement the
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
147 SourceLoader ABC instead of this class. If Python 3.1 compatibility is
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
148 needed, then use the following idiom to have a single class that is
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
149 compatible with Python 3.1 onwards::
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
150
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
151 try:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
152 from importlib.abc import SourceLoader
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
153 except ImportError:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
154 from importlib.abc import PyLoader as SourceLoader
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
155
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
156
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
157 class CustomLoader(SourceLoader):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
158 def get_filename(self, fullname):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
159 # Implement ...
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
160
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
161 def source_path(self, fullname):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
162 '''Implement source_path in terms of get_filename.'''
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
163 try:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
164 return self.get_filename(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
165 except ImportError:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
166 return None
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
167
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
168 def is_package(self, fullname):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
169 filename = os.path.basename(self.get_filename(fullname))
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
170 return os.path.splitext(filename)[0] == '__init__'
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
171
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
172 """
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
173
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
174 @abc.abstractmethod
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
175 def is_package(self, fullname):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
176 raise NotImplementedError
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
177
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
178 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
179 def source_path(self, fullname):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
180 """Abstract method. Accepts a str module name and returns the path to
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
181 the source code for the module."""
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
182 raise NotImplementedError
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
183
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
184 def get_filename(self, fullname):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
185 """Implement get_filename in terms of source_path.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
186
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
187 As get_filename should only return a source file path there is no
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
188 chance of the path not existing but loading still being possible, so
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
189 ImportError should propagate instead of being turned into returning
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
190 None.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
191
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
192 """
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
193 warnings.warn("importlib.abc.PyLoader is deprecated and is "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
194 "slated for removal in Python 3.4; "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
195 "use SourceLoader instead. "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
196 "See the importlib documentation on how to be "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
197 "compatible with Python 3.1 onwards.",
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
198 PendingDeprecationWarning)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
199 path = self.source_path(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
200 if path is None:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
201 raise ImportError
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
202 else:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
203 return path
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
204
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
205
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
206 class PyPycLoader(PyLoader):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
207
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
208 """Abstract base class to assist in loading source and bytecode by
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
209 requiring only back-end storage methods to be implemented.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
210
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
211 This class has been deprecated! Removal is slated for Python 3.4. Implement
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
212 the SourceLoader ABC instead. If Python 3.1 compatibility is needed, see
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
213 PyLoader.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
214
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
215 The methods get_code, get_source, and load_module are implemented for the
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
216 user.
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
217
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
218 """
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
219
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
220 def get_filename(self, fullname):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
221 """Return the source or bytecode file path."""
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
222 path = self.source_path(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
223 if path is not None:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
224 return path
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
225 path = self.bytecode_path(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
226 if path is not None:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
227 return path
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
228 raise ImportError("no source or bytecode path available for "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
229 "{0!r}".format(fullname))
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
230
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
231 def get_code(self, fullname):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
232 """Get a code object from source or bytecode."""
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
233 warnings.warn("importlib.abc.PyPycLoader is deprecated and slated for "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
234 "removal in Python 3.4; use SourceLoader instead. "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
235 "If Python 3.1 compatibility is required, see the "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
236 "latest documentation for PyLoader.",
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
237 PendingDeprecationWarning)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
238 source_timestamp = self.source_mtime(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
239 # Try to use bytecode if it is available.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
240 bytecode_path = self.bytecode_path(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
241 if bytecode_path:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
242 data = self.get_data(bytecode_path)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
243 try:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
244 magic = data[:4]
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
245 if len(magic) < 4:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
246 raise ImportError("bad magic number in {}".format(fullname))
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
247 raw_timestamp = data[4:8]
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
248 if len(raw_timestamp) < 4:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
249 raise EOFError("bad timestamp in {}".format(fullname))
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
250 pyc_timestamp = marshal._r_long(raw_timestamp)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
251 bytecode = data[8:]
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
252 # Verify that the magic number is valid.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
253 if imp.get_magic() != magic:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
254 raise ImportError("bad magic number in {}".format(fullname))
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
255 # Verify that the bytecode is not stale (only matters when
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
256 # there is source to fall back on.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
257 if source_timestamp:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
258 if pyc_timestamp < source_timestamp:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
259 raise ImportError("bytecode is stale")
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
260 except (ImportError, EOFError):
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
261 # If source is available give it a shot.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
262 if source_timestamp is not None:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
263 pass
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
264 else:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
265 raise
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
266 else:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
267 # Bytecode seems fine, so try to use it.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
268 return marshal.loads(bytecode)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
269 elif source_timestamp is None:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
270 raise ImportError("no source or bytecode available to create code "
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
271 "object for {0!r}".format(fullname))
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
272 # Use the source.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
273 source_path = self.source_path(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
274 if source_path is None:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
275 message = "a source path must exist to load {0}".format(fullname)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
276 raise ImportError(message)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
277 source = self.get_data(source_path)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
278 code_object = compile(source, source_path, 'exec', dont_inherit=True)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
279 # Generate bytecode and write it out.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
280 if not sys.dont_write_bytecode:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
281 data = bytearray(imp.get_magic())
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
282 data.extend(marshal._w_long(source_timestamp))
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
283 data.extend(marshal.dumps(code_object))
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
284 self.write_bytecode(fullname, data)
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
285 return code_object
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
286
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
287 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
288 def source_mtime(self, fullname):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
289 """Abstract method. Accepts a str filename and returns an int
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
290 modification time for the source of the module."""
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
291 raise NotImplementedError
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
292
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
293 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
294 def bytecode_path(self, fullname):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
295 """Abstract method. Accepts a str filename and returns the str pathname
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
296 to the bytecode for the module."""
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
297 raise NotImplementedError
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
298
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
299 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
300 def write_bytecode(self, fullname, bytecode):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
301 """Abstract method. Accepts a str filename and bytes object
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
302 representing the bytecode for the module. Returns a boolean
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
303 representing whether the bytecode was written or not."""
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
304 raise NotImplementedError