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. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 40 | Returns a Loader object or None. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 41 | """ |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 42 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 43 | |
| 44 | class MetaPathFinder(Finder): |
| 45 | |
| 46 | """Abstract base class for import finders on sys.meta_path.""" |
| 47 | |
| 48 | @abc.abstractmethod |
| 49 | def find_module(self, fullname, path): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 50 | """Abstract method which, when implemented, should find a module. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 51 | The fullname is a str and the path is a list of strings or None. |
| 52 | Returns a Loader object or None. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 53 | """ |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 54 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 55 | def invalidate_caches(self): |
| 56 | """An optional method for clearing the finder's cache, if any. |
| 57 | This method is used by importlib.invalidate_caches(). |
| 58 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 59 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 60 | _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, |
Nick Coghlan | ff79486 | 2012-08-02 21:45:24 +1000 | [diff] [blame] | 61 | machinery.PathFinder, machinery.WindowsRegistryFinder) |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | class PathEntryFinder(Finder): |
| 65 | |
| 66 | """Abstract base class for path entry finders used by PathFinder.""" |
| 67 | |
| 68 | @abc.abstractmethod |
| 69 | def find_loader(self, fullname): |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 70 | """Abstract method which, when implemented, returns a module loader or |
| 71 | a possible part of a namespace. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 72 | The fullname is a str. Returns a 2-tuple of (Loader, portion) where |
| 73 | portion is a sequence of file system locations contributing to part of |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 74 | a namespace package. The sequence may be empty and the loader may be |
| 75 | None. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 76 | """ |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 77 | return None, [] |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 78 | |
Brett Cannon | f410ce8 | 2012-08-10 17:41:23 -0400 | [diff] [blame] | 79 | find_module = _bootstrap._find_module_shim |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 80 | |
| 81 | def invalidate_caches(self): |
| 82 | """An optional method for clearing the finder's cache, if any. |
| 83 | This method is used by PathFinder.invalidate_caches(). |
| 84 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 85 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 86 | _register(PathEntryFinder, machinery.FileFinder) |
| 87 | |
| 88 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 89 | class Loader(metaclass=abc.ABCMeta): |
| 90 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 91 | """Abstract base class for import loaders. |
| 92 | |
| 93 | The optional method module_repr(module) may be defined to provide a |
| 94 | repr for a module when appropriate (see PEP 420). The __repr__() method on |
| 95 | the module type will use the method as appropriate. |
| 96 | |
| 97 | """ |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 98 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 99 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 100 | def load_module(self, fullname): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 101 | """Abstract method which when implemented should load a module. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 102 | The fullname is a str. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 103 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 104 | ImportError is raised on failure. |
| 105 | """ |
| 106 | raise ImportError |
| 107 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 108 | def module_repr(self, module): |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 109 | """Return a module's repr. |
| 110 | |
| 111 | Used by the module type when implemented without raising an exception. |
| 112 | """ |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 113 | raise NotImplementedError |
| 114 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 115 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 116 | class ResourceLoader(Loader): |
| 117 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 118 | """Abstract base class for loaders which can return data from their |
| 119 | back-end storage. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 120 | |
| 121 | This ABC represents one of the optional protocols specified by PEP 302. |
| 122 | |
| 123 | """ |
| 124 | |
| 125 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 126 | def get_data(self, path): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 127 | """Abstract method which when implemented should return the bytes for |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 128 | the specified path. The path must be a str.""" |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 129 | raise IOError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 130 | |
| 131 | |
| 132 | class InspectLoader(Loader): |
| 133 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 134 | """Abstract base class for loaders which support inspection about the |
| 135 | modules they can load. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 136 | |
| 137 | This ABC represents one of the optional protocols specified by PEP 302. |
| 138 | |
| 139 | """ |
| 140 | |
| 141 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 142 | def is_package(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 143 | """Abstract method which when implemented should return whether the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 144 | module is a package. The fullname is a str. Returns a bool. |
| 145 | |
| 146 | Raises ImportError is the module cannot be found. |
| 147 | """ |
| 148 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 149 | |
| 150 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 151 | def get_code(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 152 | """Abstract method which when implemented should return the code object |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 153 | for the module. The fullname is a str. Returns a types.CodeType. |
| 154 | |
| 155 | Raises ImportError if the module cannot be found. |
| 156 | """ |
| 157 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 158 | |
| 159 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 160 | def get_source(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 161 | """Abstract method which should return the source code for the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 162 | module. The fullname is a str. Returns a str. |
| 163 | |
| 164 | Raises ImportError if the module cannot be found. |
| 165 | """ |
| 166 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 167 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 168 | _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, |
| 169 | machinery.ExtensionFileLoader) |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 170 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 171 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 172 | class ExecutionLoader(InspectLoader): |
| 173 | |
| 174 | """Abstract base class for loaders that wish to support the execution of |
| 175 | modules as scripts. |
| 176 | |
| 177 | This ABC represents one of the optional protocols specified in PEP 302. |
| 178 | |
| 179 | """ |
| 180 | |
| 181 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 182 | def get_filename(self, fullname): |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 183 | """Abstract method which should return the value that __file__ is to be |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 184 | set to. |
| 185 | |
| 186 | Raises ImportError if the module cannot be found. |
| 187 | """ |
| 188 | raise ImportError |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 189 | |
| 190 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 191 | class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader): |
| 192 | |
| 193 | """Abstract base class partially implementing the ResourceLoader and |
| 194 | ExecutionLoader ABCs.""" |
| 195 | |
| 196 | _register(FileLoader, machinery.SourceFileLoader, |
Marc-Andre Lemburg | 4fe29c9 | 2012-04-25 02:31:37 +0200 | [diff] [blame] | 197 | machinery.SourcelessFileLoader) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 198 | |
| 199 | |
Brett Cannon | 0cf9e6a | 2010-06-28 04:57:24 +0000 | [diff] [blame] | 200 | class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader): |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 201 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 202 | """Abstract base class for loading source code (and optionally any |
| 203 | corresponding bytecode). |
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 | To support loading from source code, the abstractmethods inherited from |
| 206 | ResourceLoader and ExecutionLoader need to be implemented. To also support |
| 207 | loading from bytecode, the optional methods specified directly by this ABC |
| 208 | is required. |
| 209 | |
| 210 | Inherited abstractmethods not implemented in this ABC: |
| 211 | |
| 212 | * ResourceLoader.get_data |
| 213 | * ExecutionLoader.get_filename |
| 214 | |
| 215 | """ |
| 216 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 217 | def path_mtime(self, path): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 218 | """Return the (int) modification time for the path (str).""" |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 219 | if self.path_stats.__func__ is SourceLoader.path_stats: |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 220 | raise IOError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 221 | return int(self.path_stats(path)['mtime']) |
| 222 | |
| 223 | def path_stats(self, path): |
| 224 | """Return a metadata dict for the source pointed to by the path (str). |
| 225 | Possible keys: |
| 226 | - 'mtime' (mandatory) is the numeric timestamp of last source |
| 227 | code modification; |
| 228 | - 'size' (optional) is the size in bytes of the source code. |
| 229 | """ |
| 230 | if self.path_mtime.__func__ is SourceLoader.path_mtime: |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 231 | raise IOError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 232 | return {'mtime': self.path_mtime(path)} |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 233 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 234 | def set_data(self, path, data): |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 235 | """Write the bytes to the path (if possible). |
| 236 | |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 237 | Accepts a str path and data as bytes. |
| 238 | |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 239 | Any needed intermediary directories are to be created. If for some |
| 240 | reason the file cannot be written because of permissions, fail |
| 241 | silently. |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 242 | """ |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 243 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 244 | _register(SourceLoader, machinery.SourceFileLoader) |