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