blob: 11af22dab9ba7ee3955535e6a787dc12445bafb8 [file] [log] [blame]
Brett Cannon2a922ed2009-03-09 03:35:50 +00001"""Abstract base classes related to import."""
Eric Snow183a9412015-05-15 21:54:59 -06002from . import _bootstrap
Eric Snow32439d62015-05-02 19:15:18 -06003from . import _bootstrap_external
Brett Cannon2a922ed2009-03-09 03:35:50 +00004from . import machinery
Brett Cannon938d44d2012-04-22 19:58:33 -04005try:
6 import _frozen_importlib
Eric Snow32439d62015-05-02 19:15:18 -06007# import _frozen_importlib_external
Brett Cannoncd171c82013-07-04 17:43:24 -04008except ImportError as exc:
Brett Cannon938d44d2012-04-22 19:58:33 -04009 if exc.name != '_frozen_importlib':
10 raise
11 _frozen_importlib = None
Eric Snow32439d62015-05-02 19:15:18 -060012try:
13 import _frozen_importlib_external
14except ImportError as exc:
15 _frozen_importlib_external = _bootstrap_external
Brett Cannon2a922ed2009-03-09 03:35:50 +000016import abc
Brett Cannon2a922ed2009-03-09 03:35:50 +000017
18
Brett Cannon938d44d2012-04-22 19:58:33 -040019def _register(abstract_cls, *classes):
20 for cls in classes:
21 abstract_cls.register(cls)
22 if _frozen_importlib is not None:
Eric Snow32439d62015-05-02 19:15:18 -060023 try:
24 frozen_cls = getattr(_frozen_importlib, cls.__name__)
25 except AttributeError:
26 frozen_cls = getattr(_frozen_importlib_external, cls.__name__)
Brett Cannon938d44d2012-04-22 19:58:33 -040027 abstract_cls.register(frozen_cls)
28
29
Nick Coghlan8a9080f2012-08-02 21:26:03 +100030class Finder(metaclass=abc.ABCMeta):
31
Brett Cannonf4dc9202012-08-10 12:21:12 -040032 """Legacy abstract base class for import finders.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100033
Brett Cannonf4dc9202012-08-10 12:21:12 -040034 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 Coghlan8a9080f2012-08-02 21:26:03 +100038 """
39
Brett Cannonf4dc9202012-08-10 12:21:12 -040040 @abc.abstractmethod
Nick Coghlan8a9080f2012-08-02 21:26:03 +100041 def find_module(self, fullname, path=None):
Brett Cannonf4dc9202012-08-10 12:21:12 -040042 """An abstract method that should find a module.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100043 The fullname is a str and the optional path is a str or None.
Brett Cannon100883f2013-04-09 16:59:39 -040044 Returns a Loader object or None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100045 """
Nick Coghlan8a9080f2012-08-02 21:26:03 +100046
Nick Coghlan8a9080f2012-08-02 21:26:03 +100047
48class MetaPathFinder(Finder):
49
50 """Abstract base class for import finders on sys.meta_path."""
51
Eric Snowb523f842013-11-22 09:05:39 -070052 # We don't define find_spec() here since that would break
53 # hasattr checks we do to support backward compatibility.
54
Nick Coghlan8a9080f2012-08-02 21:26:03 +100055 def find_module(self, fullname, path):
Eric Snowb523f842013-11-22 09:05:39 -070056 """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 Cannon8d942292014-01-07 15:52:42 -050061 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 Snow1500d492014-01-06 20:49:04 -070064
Nick Coghlan8a9080f2012-08-02 21:26:03 +100065 """
Brett Cannon8d942292014-01-07 15:52:42 -050066 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 Coghlan8a9080f2012-08-02 21:26:03 +100070
Brett Cannonf4dc9202012-08-10 12:21:12 -040071 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 Cannonf4dc9202012-08-10 12:21:12 -040075
Nick Coghlan8a9080f2012-08-02 21:26:03 +100076_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
Nick Coghlanff794862012-08-02 21:45:24 +100077 machinery.PathFinder, machinery.WindowsRegistryFinder)
Nick Coghlan8a9080f2012-08-02 21:26:03 +100078
79
80class PathEntryFinder(Finder):
81
82 """Abstract base class for path entry finders used by PathFinder."""
83
Eric Snowb523f842013-11-22 09:05:39 -070084 # We don't define find_spec() here since that would break
85 # hasattr checks we do to support backward compatibility.
86
Nick Coghlan8a9080f2012-08-02 21:26:03 +100087 def find_loader(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -070088 """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 Cannon8d942292014-01-07 15:52:42 -050098 This method is deprecated in favor of finder.find_spec(). If find_spec()
99 is provided than backwards-compatible functionality is provided.
Eric Snow1500d492014-01-06 20:49:04 -0700100
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000101 """
Brett Cannon8d942292014-01-07 15:52:42 -0500102 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 Coghlan8a9080f2012-08-02 21:26:03 +1000113
Eric Snow32439d62015-05-02 19:15:18 -0600114 find_module = _bootstrap_external._find_module_shim
Brett Cannonf4dc9202012-08-10 12:21:12 -0400115
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 Cannonf4dc9202012-08-10 12:21:12 -0400120
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000121_register(PathEntryFinder, machinery.FileFinder)
122
123
Brett Cannon2a922ed2009-03-09 03:35:50 +0000124class Loader(metaclass=abc.ABCMeta):
125
Eric Snowb523f842013-11-22 09:05:39 -0700126 """Abstract base class for import loaders."""
Brett Cannon100883f2013-04-09 16:59:39 -0400127
Eric Snowb523f842013-11-22 09:05:39 -0700128 def create_module(self, spec):
129 """Return a module to initialize and into which to load.
Brett Cannon100883f2013-04-09 16:59:39 -0400130
Eric Snowb523f842013-11-22 09:05:39 -0700131 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 Snowb523f842013-11-22 09:05:39 -0700134 """
Brett Cannon2a17bde2014-05-30 14:55:29 -0400135 # By default, defer to default semantics for the new module.
Eric Snowb523f842013-11-22 09:05:39 -0700136 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 Hettingercd92f372011-01-13 02:31:25 +0000141 def load_module(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700142 """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 Cannon2a922ed2009-03-09 03:35:50 +0000146
Brett Cannon100883f2013-04-09 16:59:39 -0400147 ImportError is raised on failure.
Eric Snowb523f842013-11-22 09:05:39 -0700148
Brett Cannon8d942292014-01-07 15:52:42 -0500149 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 Snow1500d492014-01-06 20:49:04 -0700152
Brett Cannon100883f2013-04-09 16:59:39 -0400153 """
Brett Cannon8d942292014-01-07 15:52:42 -0500154 if not hasattr(self, 'exec_module'):
155 raise ImportError
Eric Snow183a9412015-05-15 21:54:59 -0600156 return _bootstrap._load_module_shim(self, fullname)
Brett Cannon100883f2013-04-09 16:59:39 -0400157
Barry Warsawd7d21942012-07-29 16:36:17 -0400158 def module_repr(self, module):
Brett Cannon100883f2013-04-09 16:59:39 -0400159 """Return a module's repr.
160
Brett Cannonf1d7b112013-05-31 18:39:07 -0400161 Used by the module type when the method does not raise
162 NotImplementedError.
Barry Warsawd7d21942012-07-29 16:36:17 -0400163
Eric Snow1500d492014-01-06 20:49:04 -0700164 This method is deprecated.
165
Eric Snowb523f842013-11-22 09:05:39 -0700166 """
167 # The exception will cause ModuleType.__repr__ to ignore this method.
168 raise NotImplementedError
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400169
Brett Cannon2a922ed2009-03-09 03:35:50 +0000170
Brett Cannon2a922ed2009-03-09 03:35:50 +0000171class ResourceLoader(Loader):
172
Brett Cannon7aa21f72009-03-15 00:53:05 +0000173 """Abstract base class for loaders which can return data from their
174 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000175
176 This ABC represents one of the optional protocols specified by PEP 302.
177
178 """
179
180 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000181 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000182 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000183 the specified path. The path must be a str."""
Brett Cannon100883f2013-04-09 16:59:39 -0400184 raise IOError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000185
186
187class InspectLoader(Loader):
188
Brett Cannon7aa21f72009-03-15 00:53:05 +0000189 """Abstract base class for loaders which support inspection about the
190 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000191
192 This ABC represents one of the optional protocols specified by PEP 302.
193
194 """
195
Raymond Hettingercd92f372011-01-13 02:31:25 +0000196 def is_package(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700197 """Optional method which when implemented should return whether the
Brett Cannon100883f2013-04-09 16:59:39 -0400198 module is a package. The fullname is a str. Returns a bool.
199
Eric Snowb523f842013-11-22 09:05:39 -0700200 Raises ImportError if the module cannot be found.
Brett Cannon100883f2013-04-09 16:59:39 -0400201 """
202 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000203
Raymond Hettingercd92f372011-01-13 02:31:25 +0000204 def get_code(self, fullname):
Brett Cannon3b62ca82013-05-27 21:11:04 -0400205 """Method which returns the code object for the module.
Brett Cannon100883f2013-04-09 16:59:39 -0400206
Brett Cannon3b62ca82013-05-27 21:11:04 -0400207 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 Cannon100883f2013-04-09 16:59:39 -0400211 """
Brett Cannon3b62ca82013-05-27 21:11:04 -0400212 source = self.get_source(fullname)
213 if source is None:
214 return None
215 return self.source_to_code(source)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000216
217 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000218 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000219 """Abstract method which should return the source code for the
Brett Cannon100883f2013-04-09 16:59:39 -0400220 module. The fullname is a str. Returns a str.
221
222 Raises ImportError if the module cannot be found.
223 """
224 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000225
Brett Cannon6eaac132014-05-09 12:28:22 -0400226 @staticmethod
227 def source_to_code(data, path='<string>'):
Brett Cannon9ffe85e2013-05-26 16:45:10 -0400228 """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 Snow32439d62015-05-02 19:15:18 -0600234 exec_module = _bootstrap_external._LoaderBasics.exec_module
235 load_module = _bootstrap_external._LoaderBasics.load_module
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400236
Eric Snowb523f842013-11-22 09:05:39 -0700237_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
Brett Cannona113ac52009-03-15 01:41:33 +0000238
Brett Cannon2a922ed2009-03-09 03:35:50 +0000239
Brett Cannon69194272009-07-20 04:23:48 +0000240class 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 Hettingercd92f372011-01-13 02:31:25 +0000250 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000251 """Abstract method which should return the value that __file__ is to be
Brett Cannon100883f2013-04-09 16:59:39 -0400252 set to.
253
254 Raises ImportError if the module cannot be found.
255 """
256 raise ImportError
Brett Cannon69194272009-07-20 04:23:48 +0000257
Brett Cannon3b62ca82013-05-27 21:11:04 -0400258 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 Snow7e70fa52013-10-04 20:28:52 -0600274_register(ExecutionLoader, machinery.ExtensionFileLoader)
Eric Snow51794452013-10-03 12:08:55 -0600275
Brett Cannon69194272009-07-20 04:23:48 +0000276
Eric Snow32439d62015-05-02 19:15:18 -0600277class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader):
Brett Cannon938d44d2012-04-22 19:58:33 -0400278
279 """Abstract base class partially implementing the ResourceLoader and
280 ExecutionLoader ABCs."""
281
282_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200283 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400284
285
Eric Snow32439d62015-05-02 19:15:18 -0600286class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000287
Brett Cannonf23e3742010-06-27 23:57:46 +0000288 """Abstract base class for loading source code (and optionally any
289 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000290
Brett Cannonf23e3742010-06-27 23:57:46 +0000291 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 Hettingercd92f372011-01-13 02:31:25 +0000303 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000304 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100305 if self.path_stats.__func__ is SourceLoader.path_stats:
Brett Cannon100883f2013-04-09 16:59:39 -0400306 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100307 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 Cannon100883f2013-04-09 16:59:39 -0400317 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100318 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000319
Raymond Hettingercd92f372011-01-13 02:31:25 +0000320 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000321 """Write the bytes to the path (if possible).
322
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000323 Accepts a str path and data as bytes.
324
Brett Cannon8d189072010-08-22 20:38:47 +0000325 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 Cannon8d189072010-08-22 20:38:47 +0000328 """
Brett Cannon8d189072010-08-22 20:38:47 +0000329
Brett Cannon938d44d2012-04-22 19:58:33 -0400330_register(SourceLoader, machinery.SourceFileLoader)