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) |