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 | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 4 | try: |
| 5 | import _frozen_importlib |
| 6 | except ImportError as exc: |
| 7 | if exc.name != '_frozen_importlib': |
| 8 | raise |
| 9 | _frozen_importlib = None |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 10 | import abc |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 11 | import imp |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 12 | import marshal |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 13 | import sys |
| 14 | import tokenize |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 15 | import warnings |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 16 | |
| 17 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 18 | def _register(abstract_cls, *classes): |
| 19 | for cls in classes: |
| 20 | abstract_cls.register(cls) |
| 21 | if _frozen_importlib is not None: |
| 22 | frozen_cls = getattr(_frozen_importlib, cls.__name__) |
| 23 | abstract_cls.register(frozen_cls) |
| 24 | |
| 25 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 26 | class Finder(metaclass=abc.ABCMeta): |
| 27 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 28 | """Legacy abstract base class for import finders. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 29 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 30 | It may be subclassed for compatibility with legacy third party |
| 31 | reimplementations of the import system. Otherwise, finder |
| 32 | implementations should derive from the more specific MetaPathFinder |
| 33 | or PathEntryFinder ABCs. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 34 | """ |
| 35 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 36 | @abc.abstractmethod |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 37 | def find_module(self, fullname, path=None): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 38 | """An abstract method that should find a module. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 39 | The fullname is a str and the optional path is a str or None. |
| 40 | Returns a Loader object. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 41 | """ |
| 42 | raise NotImplementedError |
| 43 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 44 | |
| 45 | class MetaPathFinder(Finder): |
| 46 | |
| 47 | """Abstract base class for import finders on sys.meta_path.""" |
| 48 | |
| 49 | @abc.abstractmethod |
| 50 | def find_module(self, fullname, path): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 51 | """Abstract method which, when implemented, should find a module. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 52 | The fullname is a str and the path is a str or None. |
| 53 | Returns a Loader object. |
| 54 | """ |
| 55 | raise NotImplementedError |
| 56 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 57 | def invalidate_caches(self): |
| 58 | """An optional method for clearing the finder's cache, if any. |
| 59 | This method is used by importlib.invalidate_caches(). |
| 60 | """ |
| 61 | return NotImplemented |
| 62 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 63 | _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, |
Nick Coghlan | ff79486 | 2012-08-02 21:45:24 +1000 | [diff] [blame] | 64 | machinery.PathFinder, machinery.WindowsRegistryFinder) |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | class PathEntryFinder(Finder): |
| 68 | |
| 69 | """Abstract base class for path entry finders used by PathFinder.""" |
| 70 | |
| 71 | @abc.abstractmethod |
| 72 | def find_loader(self, fullname): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 73 | """Abstract method which, when implemented, returns a module loader. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 74 | The fullname is a str. Returns a 2-tuple of (Loader, portion) where |
| 75 | portion is a sequence of file system locations contributing to part of |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 76 | a namespace package. The sequence may be empty and the loader may be |
| 77 | None. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 78 | """ |
| 79 | raise NotImplementedError |
| 80 | |
Brett Cannon | f410ce8 | 2012-08-10 17:41:23 -0400 | [diff] [blame] | 81 | find_module = _bootstrap._find_module_shim |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 82 | |
| 83 | def invalidate_caches(self): |
| 84 | """An optional method for clearing the finder's cache, if any. |
| 85 | This method is used by PathFinder.invalidate_caches(). |
| 86 | """ |
| 87 | return NotImplemented |
| 88 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 89 | _register(PathEntryFinder, machinery.FileFinder) |
| 90 | |
| 91 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 92 | class Loader(metaclass=abc.ABCMeta): |
| 93 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 94 | """Abstract base class for import loaders.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 95 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 96 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 97 | def load_module(self, fullname): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 98 | """Abstract method which when implemented should load a module. |
| 99 | The fullname is a str.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 100 | raise NotImplementedError |
| 101 | |
Barry Warsaw | 38f75cb | 2012-07-31 16:39:43 -0400 | [diff] [blame] | 102 | @abc.abstractmethod |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 103 | def module_repr(self, module): |
| 104 | """Abstract method which when implemented calculates and returns the |
| 105 | given module's repr.""" |
| 106 | raise NotImplementedError |
| 107 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 108 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 109 | class ResourceLoader(Loader): |
| 110 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 111 | """Abstract base class for loaders which can return data from their |
| 112 | back-end storage. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 113 | |
| 114 | This ABC represents one of the optional protocols specified by PEP 302. |
| 115 | |
| 116 | """ |
| 117 | |
| 118 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 119 | def get_data(self, path): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 120 | """Abstract method which when implemented should return the bytes for |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 121 | the specified path. The path must be a str.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 122 | raise NotImplementedError |
| 123 | |
| 124 | |
| 125 | class InspectLoader(Loader): |
| 126 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 127 | """Abstract base class for loaders which support inspection about the |
| 128 | modules they can load. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 129 | |
| 130 | This ABC represents one of the optional protocols specified by PEP 302. |
| 131 | |
| 132 | """ |
| 133 | |
| 134 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 135 | def is_package(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 136 | """Abstract method which when implemented should return whether the |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 137 | module is a package. The fullname is a str. Returns a bool.""" |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 138 | raise NotImplementedError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 139 | |
| 140 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 141 | def get_code(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 142 | """Abstract method which when implemented should return the code object |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 143 | for the module. The fullname is a str. Returns a types.CodeType.""" |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 144 | raise NotImplementedError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 145 | |
| 146 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 147 | def get_source(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 148 | """Abstract method which should return the source code for the |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 149 | module. The fullname is a str. Returns a str.""" |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 150 | raise NotImplementedError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 151 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 152 | _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, |
| 153 | machinery.ExtensionFileLoader) |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 154 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 155 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 156 | class ExecutionLoader(InspectLoader): |
| 157 | |
| 158 | """Abstract base class for loaders that wish to support the execution of |
| 159 | modules as scripts. |
| 160 | |
| 161 | This ABC represents one of the optional protocols specified in PEP 302. |
| 162 | |
| 163 | """ |
| 164 | |
| 165 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 166 | def get_filename(self, fullname): |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 167 | """Abstract method which should return the value that __file__ is to be |
| 168 | set to.""" |
| 169 | raise NotImplementedError |
| 170 | |
| 171 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 172 | class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader): |
| 173 | |
| 174 | """Abstract base class partially implementing the ResourceLoader and |
| 175 | ExecutionLoader ABCs.""" |
| 176 | |
| 177 | _register(FileLoader, machinery.SourceFileLoader, |
Marc-Andre Lemburg | 4fe29c9 | 2012-04-25 02:31:37 +0200 | [diff] [blame] | 178 | machinery.SourcelessFileLoader) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 179 | |
| 180 | |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 181 | class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader): |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 182 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 183 | """Abstract base class for loading source code (and optionally any |
| 184 | corresponding bytecode). |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 185 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 186 | To support loading from source code, the abstractmethods inherited from |
| 187 | ResourceLoader and ExecutionLoader need to be implemented. To also support |
| 188 | loading from bytecode, the optional methods specified directly by this ABC |
| 189 | is required. |
| 190 | |
| 191 | Inherited abstractmethods not implemented in this ABC: |
| 192 | |
| 193 | * ResourceLoader.get_data |
| 194 | * ExecutionLoader.get_filename |
| 195 | |
| 196 | """ |
| 197 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 198 | def path_mtime(self, path): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 199 | """Return the (int) modification time for the path (str).""" |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 200 | if self.path_stats.__func__ is SourceLoader.path_stats: |
| 201 | raise NotImplementedError |
| 202 | return int(self.path_stats(path)['mtime']) |
| 203 | |
| 204 | def path_stats(self, path): |
| 205 | """Return a metadata dict for the source pointed to by the path (str). |
| 206 | Possible keys: |
| 207 | - 'mtime' (mandatory) is the numeric timestamp of last source |
| 208 | code modification; |
| 209 | - 'size' (optional) is the size in bytes of the source code. |
| 210 | """ |
| 211 | if self.path_mtime.__func__ is SourceLoader.path_mtime: |
| 212 | raise NotImplementedError |
| 213 | return {'mtime': self.path_mtime(path)} |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 214 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 215 | def set_data(self, path, data): |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 216 | """Write the bytes to the path (if possible). |
| 217 | |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 218 | Accepts a str path and data as bytes. |
| 219 | |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 220 | Any needed intermediary directories are to be created. If for some |
| 221 | reason the file cannot be written because of permissions, fail |
| 222 | silently. |
| 223 | |
| 224 | """ |
| 225 | raise NotImplementedError |
| 226 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 227 | _register(SourceLoader, machinery.SourceFileLoader) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 228 | |
| 229 | class PyLoader(SourceLoader): |
| 230 | |
| 231 | """Implement the deprecated PyLoader ABC in terms of SourceLoader. |
| 232 | |
| 233 | This class has been deprecated! It is slated for removal in Python 3.4. |
| 234 | If compatibility with Python 3.1 is not needed then implement the |
| 235 | SourceLoader ABC instead of this class. If Python 3.1 compatibility is |
| 236 | needed, then use the following idiom to have a single class that is |
| 237 | compatible with Python 3.1 onwards:: |
| 238 | |
| 239 | try: |
| 240 | from importlib.abc import SourceLoader |
| 241 | except ImportError: |
| 242 | from importlib.abc import PyLoader as SourceLoader |
| 243 | |
| 244 | |
| 245 | class CustomLoader(SourceLoader): |
| 246 | def get_filename(self, fullname): |
| 247 | # Implement ... |
| 248 | |
| 249 | def source_path(self, fullname): |
| 250 | '''Implement source_path in terms of get_filename.''' |
| 251 | try: |
| 252 | return self.get_filename(fullname) |
| 253 | except ImportError: |
| 254 | return None |
| 255 | |
| 256 | def is_package(self, fullname): |
| 257 | filename = os.path.basename(self.get_filename(fullname)) |
| 258 | return os.path.splitext(filename)[0] == '__init__' |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 259 | |
| 260 | """ |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 261 | |
| 262 | @abc.abstractmethod |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 263 | def is_package(self, fullname): |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 264 | raise NotImplementedError |
| 265 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 266 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 267 | def source_path(self, fullname): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 268 | """Abstract method. Accepts a str module name and returns the path to |
| 269 | the source code for the module.""" |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 270 | raise NotImplementedError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 271 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 272 | def get_filename(self, fullname): |
| 273 | """Implement get_filename in terms of source_path. |
| 274 | |
| 275 | As get_filename should only return a source file path there is no |
| 276 | chance of the path not existing but loading still being possible, so |
| 277 | ImportError should propagate instead of being turned into returning |
| 278 | None. |
| 279 | |
| 280 | """ |
| 281 | warnings.warn("importlib.abc.PyLoader is deprecated and is " |
| 282 | "slated for removal in Python 3.4; " |
| 283 | "use SourceLoader instead. " |
| 284 | "See the importlib documentation on how to be " |
| 285 | "compatible with Python 3.1 onwards.", |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 286 | DeprecationWarning) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 287 | path = self.source_path(fullname) |
| 288 | if path is None: |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 289 | raise ImportError(name=fullname) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 290 | else: |
| 291 | return path |
| 292 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 293 | |
| 294 | class PyPycLoader(PyLoader): |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 295 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 296 | """Abstract base class to assist in loading source and bytecode by |
| 297 | requiring only back-end storage methods to be implemented. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 298 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 299 | This class has been deprecated! Removal is slated for Python 3.4. Implement |
| 300 | the SourceLoader ABC instead. If Python 3.1 compatibility is needed, see |
| 301 | PyLoader. |
| 302 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 303 | The methods get_code, get_source, and load_module are implemented for the |
| 304 | user. |
| 305 | |
| 306 | """ |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 307 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 308 | def get_filename(self, fullname): |
| 309 | """Return the source or bytecode file path.""" |
| 310 | path = self.source_path(fullname) |
| 311 | if path is not None: |
| 312 | return path |
| 313 | path = self.bytecode_path(fullname) |
| 314 | if path is not None: |
| 315 | return path |
| 316 | raise ImportError("no source or bytecode path available for " |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 317 | "{0!r}".format(fullname), name=fullname) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 318 | |
| 319 | def get_code(self, fullname): |
| 320 | """Get a code object from source or bytecode.""" |
| 321 | warnings.warn("importlib.abc.PyPycLoader is deprecated and slated for " |
| 322 | "removal in Python 3.4; use SourceLoader instead. " |
| 323 | "If Python 3.1 compatibility is required, see the " |
| 324 | "latest documentation for PyLoader.", |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 325 | DeprecationWarning) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 326 | source_timestamp = self.source_mtime(fullname) |
| 327 | # Try to use bytecode if it is available. |
| 328 | bytecode_path = self.bytecode_path(fullname) |
| 329 | if bytecode_path: |
| 330 | data = self.get_data(bytecode_path) |
| 331 | try: |
| 332 | magic = data[:4] |
| 333 | if len(magic) < 4: |
Philip Jenvey | 4b42ff6 | 2012-02-24 21:48:17 -0800 | [diff] [blame] | 334 | raise ImportError( |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 335 | "bad magic number in {}".format(fullname), |
| 336 | name=fullname, path=bytecode_path) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 337 | raw_timestamp = data[4:8] |
| 338 | if len(raw_timestamp) < 4: |
| 339 | raise EOFError("bad timestamp in {}".format(fullname)) |
Brett Cannon | c264e3e | 2012-01-25 18:58:03 -0500 | [diff] [blame] | 340 | pyc_timestamp = _bootstrap._r_long(raw_timestamp) |
Brett Cannon | 1e33156 | 2012-07-02 14:35:34 -0400 | [diff] [blame] | 341 | raw_source_size = data[8:12] |
| 342 | if len(raw_source_size) != 4: |
| 343 | raise EOFError("bad file size in {}".format(fullname)) |
| 344 | # Source size is unused as the ABC does not provide a way to |
| 345 | # get the size of the source ahead of reading it. |
| 346 | bytecode = data[12:] |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 347 | # Verify that the magic number is valid. |
| 348 | if imp.get_magic() != magic: |
Philip Jenvey | 4b42ff6 | 2012-02-24 21:48:17 -0800 | [diff] [blame] | 349 | raise ImportError( |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 350 | "bad magic number in {}".format(fullname), |
| 351 | name=fullname, path=bytecode_path) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 352 | # Verify that the bytecode is not stale (only matters when |
| 353 | # there is source to fall back on. |
| 354 | if source_timestamp: |
| 355 | if pyc_timestamp < source_timestamp: |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 356 | raise ImportError("bytecode is stale", name=fullname, |
| 357 | path=bytecode_path) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 358 | except (ImportError, EOFError): |
| 359 | # If source is available give it a shot. |
| 360 | if source_timestamp is not None: |
| 361 | pass |
| 362 | else: |
| 363 | raise |
| 364 | else: |
| 365 | # Bytecode seems fine, so try to use it. |
| 366 | return marshal.loads(bytecode) |
| 367 | elif source_timestamp is None: |
| 368 | raise ImportError("no source or bytecode available to create code " |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 369 | "object for {0!r}".format(fullname), |
| 370 | name=fullname) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 371 | # Use the source. |
| 372 | source_path = self.source_path(fullname) |
| 373 | if source_path is None: |
| 374 | message = "a source path must exist to load {0}".format(fullname) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 375 | raise ImportError(message, name=fullname) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 376 | source = self.get_data(source_path) |
| 377 | code_object = compile(source, source_path, 'exec', dont_inherit=True) |
| 378 | # Generate bytecode and write it out. |
| 379 | if not sys.dont_write_bytecode: |
| 380 | data = bytearray(imp.get_magic()) |
Brett Cannon | c264e3e | 2012-01-25 18:58:03 -0500 | [diff] [blame] | 381 | data.extend(_bootstrap._w_long(source_timestamp)) |
Brett Cannon | 1e33156 | 2012-07-02 14:35:34 -0400 | [diff] [blame] | 382 | data.extend(_bootstrap._w_long(len(source) & 0xFFFFFFFF)) |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 383 | data.extend(marshal.dumps(code_object)) |
| 384 | self.write_bytecode(fullname, data) |
| 385 | return code_object |
| 386 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 387 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 388 | def source_mtime(self, fullname): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 389 | """Abstract method. Accepts a str filename and returns an int |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 390 | modification time for the source of the module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 391 | raise NotImplementedError |
| 392 | |
| 393 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 394 | def bytecode_path(self, fullname): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 395 | """Abstract method. Accepts a str filename and returns the str pathname |
| 396 | to the bytecode for the module.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 397 | raise NotImplementedError |
| 398 | |
| 399 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 400 | def write_bytecode(self, fullname, bytecode): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 401 | """Abstract method. Accepts a str filename and bytes object |
| 402 | representing the bytecode for the module. Returns a boolean |
| 403 | representing whether the bytecode was written or not.""" |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 404 | raise NotImplementedError |