Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 1 | """Abstract base classes related to import.""" |
Eric Snow | 183a941 | 2015-05-15 21:54:59 -0600 | [diff] [blame] | 2 | from . import _bootstrap |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 3 | from . import _bootstrap_external |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 4 | from . import machinery |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 5 | try: |
| 6 | import _frozen_importlib |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 7 | # import _frozen_importlib_external |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 8 | except ImportError as exc: |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 9 | if exc.name != '_frozen_importlib': |
| 10 | raise |
| 11 | _frozen_importlib = None |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 12 | try: |
| 13 | import _frozen_importlib_external |
| 14 | except ImportError as exc: |
| 15 | _frozen_importlib_external = _bootstrap_external |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 16 | import abc |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 17 | |
| 18 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 19 | def _register(abstract_cls, *classes): |
| 20 | for cls in classes: |
| 21 | abstract_cls.register(cls) |
| 22 | if _frozen_importlib is not None: |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 23 | try: |
| 24 | frozen_cls = getattr(_frozen_importlib, cls.__name__) |
| 25 | except AttributeError: |
| 26 | frozen_cls = getattr(_frozen_importlib_external, cls.__name__) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 27 | abstract_cls.register(frozen_cls) |
| 28 | |
| 29 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 30 | class Finder(metaclass=abc.ABCMeta): |
| 31 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 32 | """Legacy abstract base class for import finders. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 33 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 34 | It may be subclassed for compatibility with legacy third party |
| 35 | reimplementations of the import system. Otherwise, finder |
| 36 | implementations should derive from the more specific MetaPathFinder |
| 37 | or PathEntryFinder ABCs. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 38 | """ |
| 39 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 40 | @abc.abstractmethod |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 41 | def find_module(self, fullname, path=None): |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 42 | """An abstract method that should find a module. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 43 | 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] | 44 | Returns a Loader object or None. |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 45 | """ |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 46 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 47 | |
| 48 | class MetaPathFinder(Finder): |
| 49 | |
| 50 | """Abstract base class for import finders on sys.meta_path.""" |
| 51 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 52 | # We don't define find_spec() here since that would break |
| 53 | # hasattr checks we do to support backward compatibility. |
| 54 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 55 | def find_module(self, fullname, path): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 56 | """Return a loader for the module. |
| 57 | |
| 58 | If no module is found, return None. The fullname is a str and |
| 59 | the path is a list of strings or None. |
| 60 | |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 61 | This method is deprecated in favor of finder.find_spec(). If find_spec() |
| 62 | exists then backwards-compatible functionality is provided for this |
| 63 | method. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 64 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 65 | """ |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 66 | if not hasattr(self, 'find_spec'): |
| 67 | return None |
| 68 | found = self.find_spec(fullname, path) |
| 69 | return found.loader if found is not None else None |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 70 | |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 71 | def invalidate_caches(self): |
| 72 | """An optional method for clearing the finder's cache, if any. |
| 73 | This method is used by importlib.invalidate_caches(). |
| 74 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 75 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 76 | _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, |
Nick Coghlan | ff79486 | 2012-08-02 21:45:24 +1000 | [diff] [blame] | 77 | machinery.PathFinder, machinery.WindowsRegistryFinder) |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | class PathEntryFinder(Finder): |
| 81 | |
| 82 | """Abstract base class for path entry finders used by PathFinder.""" |
| 83 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 84 | # We don't define find_spec() here since that would break |
| 85 | # hasattr checks we do to support backward compatibility. |
| 86 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 87 | def find_loader(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 88 | """Return (loader, namespace portion) for the path entry. |
| 89 | |
| 90 | The fullname is a str. The namespace portion is a sequence of |
| 91 | path entries contributing to part of a namespace package. The |
| 92 | sequence may be empty. If loader is not None, the portion will |
| 93 | be ignored. |
| 94 | |
| 95 | The portion will be discarded if another path entry finder |
| 96 | locates the module as a normal module or package. |
| 97 | |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 98 | This method is deprecated in favor of finder.find_spec(). If find_spec() |
| 99 | is provided than backwards-compatible functionality is provided. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 100 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 101 | """ |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 102 | if not hasattr(self, 'find_spec'): |
| 103 | return None, [] |
| 104 | found = self.find_spec(fullname) |
| 105 | if found is not None: |
| 106 | if not found.submodule_search_locations: |
| 107 | portions = [] |
| 108 | else: |
| 109 | portions = found.submodule_search_locations |
| 110 | return found.loader, portions |
| 111 | else: |
| 112 | return None, [] |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 113 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 114 | find_module = _bootstrap_external._find_module_shim |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 115 | |
| 116 | def invalidate_caches(self): |
| 117 | """An optional method for clearing the finder's cache, if any. |
| 118 | This method is used by PathFinder.invalidate_caches(). |
| 119 | """ |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 120 | |
Nick Coghlan | 8a9080f | 2012-08-02 21:26:03 +1000 | [diff] [blame] | 121 | _register(PathEntryFinder, machinery.FileFinder) |
| 122 | |
| 123 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 124 | class Loader(metaclass=abc.ABCMeta): |
| 125 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 126 | """Abstract base class for import loaders.""" |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 127 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 128 | def create_module(self, spec): |
| 129 | """Return a module to initialize and into which to load. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 130 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 131 | This method should raise ImportError if anything prevents it |
| 132 | from creating a new module. It may return None to indicate |
| 133 | that the spec should create the new module. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 134 | """ |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 135 | # By default, defer to default semantics for the new module. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 136 | return None |
| 137 | |
| 138 | # We don't define exec_module() here since that would break |
| 139 | # hasattr checks we do to support backward compatibility. |
| 140 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 141 | def load_module(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 142 | """Return the loaded module. |
| 143 | |
| 144 | The module must be added to sys.modules and have import-related |
| 145 | attributes set properly. The fullname is a str. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 146 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 147 | ImportError is raised on failure. |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 148 | |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 149 | This method is deprecated in favor of loader.exec_module(). If |
| 150 | exec_module() exists then it is used to provide a backwards-compatible |
| 151 | functionality for this method. |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 152 | |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 153 | """ |
Brett Cannon | 8d94229 | 2014-01-07 15:52:42 -0500 | [diff] [blame] | 154 | if not hasattr(self, 'exec_module'): |
| 155 | raise ImportError |
Eric Snow | 183a941 | 2015-05-15 21:54:59 -0600 | [diff] [blame] | 156 | return _bootstrap._load_module_shim(self, fullname) |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 157 | |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 158 | def module_repr(self, module): |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 159 | """Return a module's repr. |
| 160 | |
Brett Cannon | f1d7b11 | 2013-05-31 18:39:07 -0400 | [diff] [blame] | 161 | Used by the module type when the method does not raise |
| 162 | NotImplementedError. |
Barry Warsaw | d7d2194 | 2012-07-29 16:36:17 -0400 | [diff] [blame] | 163 | |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 164 | This method is deprecated. |
| 165 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 166 | """ |
| 167 | # The exception will cause ModuleType.__repr__ to ignore this method. |
| 168 | raise NotImplementedError |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 169 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 170 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 171 | class ResourceLoader(Loader): |
| 172 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 173 | """Abstract base class for loaders which can return data from their |
| 174 | back-end storage. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 175 | |
| 176 | This ABC represents one of the optional protocols specified by PEP 302. |
| 177 | |
| 178 | """ |
| 179 | |
| 180 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 181 | def get_data(self, path): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 182 | """Abstract method which when implemented should return the bytes for |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 183 | the specified path. The path must be a str.""" |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 184 | raise IOError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | class InspectLoader(Loader): |
| 188 | |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 189 | """Abstract base class for loaders which support inspection about the |
| 190 | modules they can load. |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 191 | |
| 192 | This ABC represents one of the optional protocols specified by PEP 302. |
| 193 | |
| 194 | """ |
| 195 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 196 | def is_package(self, fullname): |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 197 | """Optional method which when implemented should return whether the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 198 | module is a package. The fullname is a str. Returns a bool. |
| 199 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 200 | Raises ImportError if the module cannot be found. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 201 | """ |
| 202 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 203 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 204 | def get_code(self, fullname): |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 205 | """Method which returns the code object for the module. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 206 | |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 207 | The fullname is a str. Returns a types.CodeType if possible, else |
| 208 | returns None if a code object does not make sense |
| 209 | (e.g. built-in module). Raises ImportError if the module cannot be |
| 210 | found. |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 211 | """ |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 212 | source = self.get_source(fullname) |
| 213 | if source is None: |
| 214 | return None |
| 215 | return self.source_to_code(source) |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 216 | |
| 217 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 218 | def get_source(self, fullname): |
Brett Cannon | 7aa21f7 | 2009-03-15 00:53:05 +0000 | [diff] [blame] | 219 | """Abstract method which should return the source code for the |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 220 | module. The fullname is a str. Returns a str. |
| 221 | |
| 222 | Raises ImportError if the module cannot be found. |
| 223 | """ |
| 224 | raise ImportError |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 225 | |
Brett Cannon | 6eaac13 | 2014-05-09 12:28:22 -0400 | [diff] [blame] | 226 | @staticmethod |
| 227 | def source_to_code(data, path='<string>'): |
Brett Cannon | 9ffe85e | 2013-05-26 16:45:10 -0400 | [diff] [blame] | 228 | """Compile 'data' into a code object. |
| 229 | |
| 230 | The 'data' argument can be anything that compile() can handle. The'path' |
| 231 | argument should be where the data was retrieved (when applicable).""" |
| 232 | return compile(data, path, 'exec', dont_inherit=True) |
| 233 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 234 | exec_module = _bootstrap_external._LoaderBasics.exec_module |
| 235 | load_module = _bootstrap_external._LoaderBasics.load_module |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 236 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 237 | _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter) |
Brett Cannon | a113ac5 | 2009-03-15 01:41:33 +0000 | [diff] [blame] | 238 | |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 239 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 240 | class ExecutionLoader(InspectLoader): |
| 241 | |
| 242 | """Abstract base class for loaders that wish to support the execution of |
| 243 | modules as scripts. |
| 244 | |
| 245 | This ABC represents one of the optional protocols specified in PEP 302. |
| 246 | |
| 247 | """ |
| 248 | |
| 249 | @abc.abstractmethod |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 250 | def get_filename(self, fullname): |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 251 | """Abstract method which should return the value that __file__ is to be |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 252 | set to. |
| 253 | |
| 254 | Raises ImportError if the module cannot be found. |
| 255 | """ |
| 256 | raise ImportError |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 257 | |
Brett Cannon | 3b62ca8 | 2013-05-27 21:11:04 -0400 | [diff] [blame] | 258 | def get_code(self, fullname): |
| 259 | """Method to return the code object for fullname. |
| 260 | |
| 261 | Should return None if not applicable (e.g. built-in module). |
| 262 | Raise ImportError if the module cannot be found. |
| 263 | """ |
| 264 | source = self.get_source(fullname) |
| 265 | if source is None: |
| 266 | return None |
| 267 | try: |
| 268 | path = self.get_filename(fullname) |
| 269 | except ImportError: |
| 270 | return self.source_to_code(source) |
| 271 | else: |
| 272 | return self.source_to_code(source, path) |
| 273 | |
Eric Snow | 7e70fa5 | 2013-10-04 20:28:52 -0600 | [diff] [blame] | 274 | _register(ExecutionLoader, machinery.ExtensionFileLoader) |
Eric Snow | 5179445 | 2013-10-03 12:08:55 -0600 | [diff] [blame] | 275 | |
Brett Cannon | 6919427 | 2009-07-20 04:23:48 +0000 | [diff] [blame] | 276 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 277 | class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader): |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 278 | |
| 279 | """Abstract base class partially implementing the ResourceLoader and |
| 280 | ExecutionLoader ABCs.""" |
| 281 | |
| 282 | _register(FileLoader, machinery.SourceFileLoader, |
Marc-Andre Lemburg | 4fe29c9 | 2012-04-25 02:31:37 +0200 | [diff] [blame] | 283 | machinery.SourcelessFileLoader) |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 284 | |
| 285 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 286 | class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader): |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 287 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 288 | """Abstract base class for loading source code (and optionally any |
| 289 | corresponding bytecode). |
Brett Cannon | 2a922ed | 2009-03-09 03:35:50 +0000 | [diff] [blame] | 290 | |
Brett Cannon | f23e374 | 2010-06-27 23:57:46 +0000 | [diff] [blame] | 291 | To support loading from source code, the abstractmethods inherited from |
| 292 | ResourceLoader and ExecutionLoader need to be implemented. To also support |
| 293 | loading from bytecode, the optional methods specified directly by this ABC |
| 294 | is required. |
| 295 | |
| 296 | Inherited abstractmethods not implemented in this ABC: |
| 297 | |
| 298 | * ResourceLoader.get_data |
| 299 | * ExecutionLoader.get_filename |
| 300 | |
| 301 | """ |
| 302 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 303 | def path_mtime(self, path): |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 304 | """Return the (int) modification time for the path (str).""" |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 305 | if self.path_stats.__func__ is SourceLoader.path_stats: |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 306 | raise IOError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 307 | return int(self.path_stats(path)['mtime']) |
| 308 | |
| 309 | def path_stats(self, path): |
| 310 | """Return a metadata dict for the source pointed to by the path (str). |
| 311 | Possible keys: |
| 312 | - 'mtime' (mandatory) is the numeric timestamp of last source |
| 313 | code modification; |
| 314 | - 'size' (optional) is the size in bytes of the source code. |
| 315 | """ |
| 316 | if self.path_mtime.__func__ is SourceLoader.path_mtime: |
Brett Cannon | 100883f | 2013-04-09 16:59:39 -0400 | [diff] [blame] | 317 | raise IOError |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 318 | return {'mtime': self.path_mtime(path)} |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 319 | |
Raymond Hettinger | cd92f37 | 2011-01-13 02:31:25 +0000 | [diff] [blame] | 320 | def set_data(self, path, data): |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 321 | """Write the bytes to the path (if possible). |
| 322 | |
Raymond Hettinger | d958ea7 | 2011-01-13 19:08:04 +0000 | [diff] [blame] | 323 | Accepts a str path and data as bytes. |
| 324 | |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 325 | Any needed intermediary directories are to be created. If for some |
| 326 | reason the file cannot be written because of permissions, fail |
| 327 | silently. |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 328 | """ |
Brett Cannon | 8d18907 | 2010-08-22 20:38:47 +0000 | [diff] [blame] | 329 | |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 330 | _register(SourceLoader, machinery.SourceFileLoader) |