blob: daff681e69689f4884bb2f9d95a94777d80d309e [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
Brett Cannoncd171c82013-07-04 17:43:24 -04007except ImportError as exc:
Brett Cannon938d44d2012-04-22 19:58:33 -04008 if exc.name != '_frozen_importlib':
9 raise
10 _frozen_importlib = None
Eric Snow32439d62015-05-02 19:15:18 -060011try:
12 import _frozen_importlib_external
13except ImportError as exc:
14 _frozen_importlib_external = _bootstrap_external
Brett Cannon2a922ed2009-03-09 03:35:50 +000015import abc
Brett Cannon2a922ed2009-03-09 03:35:50 +000016
17
Brett Cannon938d44d2012-04-22 19:58:33 -040018def _register(abstract_cls, *classes):
19 for cls in classes:
20 abstract_cls.register(cls)
21 if _frozen_importlib is not None:
Eric Snow32439d62015-05-02 19:15:18 -060022 try:
23 frozen_cls = getattr(_frozen_importlib, cls.__name__)
24 except AttributeError:
25 frozen_cls = getattr(_frozen_importlib_external, cls.__name__)
Brett Cannon938d44d2012-04-22 19:58:33 -040026 abstract_cls.register(frozen_cls)
27
28
Nick Coghlan8a9080f2012-08-02 21:26:03 +100029class Finder(metaclass=abc.ABCMeta):
30
Brett Cannonf4dc9202012-08-10 12:21:12 -040031 """Legacy abstract base class for import finders.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100032
Brett Cannonf4dc9202012-08-10 12:21:12 -040033 It may be subclassed for compatibility with legacy third party
34 reimplementations of the import system. Otherwise, finder
35 implementations should derive from the more specific MetaPathFinder
36 or PathEntryFinder ABCs.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100037 """
38
Brett Cannonf4dc9202012-08-10 12:21:12 -040039 @abc.abstractmethod
Nick Coghlan8a9080f2012-08-02 21:26:03 +100040 def find_module(self, fullname, path=None):
Brett Cannonf4dc9202012-08-10 12:21:12 -040041 """An abstract method that should find a module.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100042 The fullname is a str and the optional path is a str or None.
Brett Cannon100883f2013-04-09 16:59:39 -040043 Returns a Loader object or None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100044 """
Nick Coghlan8a9080f2012-08-02 21:26:03 +100045
Nick Coghlan8a9080f2012-08-02 21:26:03 +100046
47class MetaPathFinder(Finder):
48
49 """Abstract base class for import finders on sys.meta_path."""
50
Eric Snowb523f842013-11-22 09:05:39 -070051 # We don't define find_spec() here since that would break
52 # hasattr checks we do to support backward compatibility.
53
Nick Coghlan8a9080f2012-08-02 21:26:03 +100054 def find_module(self, fullname, path):
Eric Snowb523f842013-11-22 09:05:39 -070055 """Return a loader for the module.
56
57 If no module is found, return None. The fullname is a str and
58 the path is a list of strings or None.
59
Brett Cannon8d942292014-01-07 15:52:42 -050060 This method is deprecated in favor of finder.find_spec(). If find_spec()
61 exists then backwards-compatible functionality is provided for this
62 method.
Eric Snow1500d492014-01-06 20:49:04 -070063
Nick Coghlan8a9080f2012-08-02 21:26:03 +100064 """
Brett Cannon8d942292014-01-07 15:52:42 -050065 if not hasattr(self, 'find_spec'):
66 return None
67 found = self.find_spec(fullname, path)
68 return found.loader if found is not None else None
Nick Coghlan8a9080f2012-08-02 21:26:03 +100069
Brett Cannonf4dc9202012-08-10 12:21:12 -040070 def invalidate_caches(self):
71 """An optional method for clearing the finder's cache, if any.
72 This method is used by importlib.invalidate_caches().
73 """
Brett Cannonf4dc9202012-08-10 12:21:12 -040074
Nick Coghlan8a9080f2012-08-02 21:26:03 +100075_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
Nick Coghlanff794862012-08-02 21:45:24 +100076 machinery.PathFinder, machinery.WindowsRegistryFinder)
Nick Coghlan8a9080f2012-08-02 21:26:03 +100077
78
79class PathEntryFinder(Finder):
80
81 """Abstract base class for path entry finders used by PathFinder."""
82
Eric Snowb523f842013-11-22 09:05:39 -070083 # We don't define find_spec() here since that would break
84 # hasattr checks we do to support backward compatibility.
85
Nick Coghlan8a9080f2012-08-02 21:26:03 +100086 def find_loader(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -070087 """Return (loader, namespace portion) for the path entry.
88
89 The fullname is a str. The namespace portion is a sequence of
90 path entries contributing to part of a namespace package. The
91 sequence may be empty. If loader is not None, the portion will
92 be ignored.
93
94 The portion will be discarded if another path entry finder
95 locates the module as a normal module or package.
96
Brett Cannon8d942292014-01-07 15:52:42 -050097 This method is deprecated in favor of finder.find_spec(). If find_spec()
98 is provided than backwards-compatible functionality is provided.
Eric Snow1500d492014-01-06 20:49:04 -070099
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000100 """
Brett Cannon8d942292014-01-07 15:52:42 -0500101 if not hasattr(self, 'find_spec'):
102 return None, []
103 found = self.find_spec(fullname)
104 if found is not None:
105 if not found.submodule_search_locations:
106 portions = []
107 else:
108 portions = found.submodule_search_locations
109 return found.loader, portions
110 else:
111 return None, []
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000112
Eric Snow32439d62015-05-02 19:15:18 -0600113 find_module = _bootstrap_external._find_module_shim
Brett Cannonf4dc9202012-08-10 12:21:12 -0400114
115 def invalidate_caches(self):
116 """An optional method for clearing the finder's cache, if any.
117 This method is used by PathFinder.invalidate_caches().
118 """
Brett Cannonf4dc9202012-08-10 12:21:12 -0400119
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000120_register(PathEntryFinder, machinery.FileFinder)
121
122
Brett Cannon2a922ed2009-03-09 03:35:50 +0000123class Loader(metaclass=abc.ABCMeta):
124
Eric Snowb523f842013-11-22 09:05:39 -0700125 """Abstract base class for import loaders."""
Brett Cannon100883f2013-04-09 16:59:39 -0400126
Eric Snowb523f842013-11-22 09:05:39 -0700127 def create_module(self, spec):
128 """Return a module to initialize and into which to load.
Brett Cannon100883f2013-04-09 16:59:39 -0400129
Eric Snowb523f842013-11-22 09:05:39 -0700130 This method should raise ImportError if anything prevents it
131 from creating a new module. It may return None to indicate
132 that the spec should create the new module.
Eric Snowb523f842013-11-22 09:05:39 -0700133 """
Brett Cannon2a17bde2014-05-30 14:55:29 -0400134 # By default, defer to default semantics for the new module.
Eric Snowb523f842013-11-22 09:05:39 -0700135 return None
136
137 # We don't define exec_module() here since that would break
138 # hasattr checks we do to support backward compatibility.
139
Raymond Hettingercd92f372011-01-13 02:31:25 +0000140 def load_module(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700141 """Return the loaded module.
142
143 The module must be added to sys.modules and have import-related
144 attributes set properly. The fullname is a str.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000145
Brett Cannon100883f2013-04-09 16:59:39 -0400146 ImportError is raised on failure.
Eric Snowb523f842013-11-22 09:05:39 -0700147
Brett Cannon8d942292014-01-07 15:52:42 -0500148 This method is deprecated in favor of loader.exec_module(). If
149 exec_module() exists then it is used to provide a backwards-compatible
150 functionality for this method.
Eric Snow1500d492014-01-06 20:49:04 -0700151
Brett Cannon100883f2013-04-09 16:59:39 -0400152 """
Brett Cannon8d942292014-01-07 15:52:42 -0500153 if not hasattr(self, 'exec_module'):
154 raise ImportError
Eric Snow183a9412015-05-15 21:54:59 -0600155 return _bootstrap._load_module_shim(self, fullname)
Brett Cannon100883f2013-04-09 16:59:39 -0400156
Barry Warsawd7d21942012-07-29 16:36:17 -0400157 def module_repr(self, module):
Brett Cannon100883f2013-04-09 16:59:39 -0400158 """Return a module's repr.
159
Brett Cannonf1d7b112013-05-31 18:39:07 -0400160 Used by the module type when the method does not raise
161 NotImplementedError.
Barry Warsawd7d21942012-07-29 16:36:17 -0400162
Eric Snow1500d492014-01-06 20:49:04 -0700163 This method is deprecated.
164
Eric Snowb523f842013-11-22 09:05:39 -0700165 """
166 # The exception will cause ModuleType.__repr__ to ignore this method.
167 raise NotImplementedError
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400168
Brett Cannon2a922ed2009-03-09 03:35:50 +0000169
Brett Cannon2a922ed2009-03-09 03:35:50 +0000170class ResourceLoader(Loader):
171
Brett Cannon7aa21f72009-03-15 00:53:05 +0000172 """Abstract base class for loaders which can return data from their
173 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000174
175 This ABC represents one of the optional protocols specified by PEP 302.
176
177 """
178
179 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000180 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000181 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000182 the specified path. The path must be a str."""
Brett Cannon100883f2013-04-09 16:59:39 -0400183 raise IOError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000184
185
186class InspectLoader(Loader):
187
Brett Cannon7aa21f72009-03-15 00:53:05 +0000188 """Abstract base class for loaders which support inspection about the
189 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000190
191 This ABC represents one of the optional protocols specified by PEP 302.
192
193 """
194
Raymond Hettingercd92f372011-01-13 02:31:25 +0000195 def is_package(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700196 """Optional method which when implemented should return whether the
Brett Cannon100883f2013-04-09 16:59:39 -0400197 module is a package. The fullname is a str. Returns a bool.
198
Eric Snowb523f842013-11-22 09:05:39 -0700199 Raises ImportError if the module cannot be found.
Brett Cannon100883f2013-04-09 16:59:39 -0400200 """
201 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000202
Raymond Hettingercd92f372011-01-13 02:31:25 +0000203 def get_code(self, fullname):
Brett Cannon3b62ca82013-05-27 21:11:04 -0400204 """Method which returns the code object for the module.
Brett Cannon100883f2013-04-09 16:59:39 -0400205
Brett Cannon3b62ca82013-05-27 21:11:04 -0400206 The fullname is a str. Returns a types.CodeType if possible, else
207 returns None if a code object does not make sense
208 (e.g. built-in module). Raises ImportError if the module cannot be
209 found.
Brett Cannon100883f2013-04-09 16:59:39 -0400210 """
Brett Cannon3b62ca82013-05-27 21:11:04 -0400211 source = self.get_source(fullname)
212 if source is None:
213 return None
214 return self.source_to_code(source)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000215
216 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000217 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000218 """Abstract method which should return the source code for the
Brett Cannon100883f2013-04-09 16:59:39 -0400219 module. The fullname is a str. Returns a str.
220
221 Raises ImportError if the module cannot be found.
222 """
223 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000224
Brett Cannon6eaac132014-05-09 12:28:22 -0400225 @staticmethod
226 def source_to_code(data, path='<string>'):
Brett Cannon9ffe85e2013-05-26 16:45:10 -0400227 """Compile 'data' into a code object.
228
229 The 'data' argument can be anything that compile() can handle. The'path'
230 argument should be where the data was retrieved (when applicable)."""
231 return compile(data, path, 'exec', dont_inherit=True)
232
Eric Snow32439d62015-05-02 19:15:18 -0600233 exec_module = _bootstrap_external._LoaderBasics.exec_module
234 load_module = _bootstrap_external._LoaderBasics.load_module
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400235
Eric Snowb523f842013-11-22 09:05:39 -0700236_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
Brett Cannona113ac52009-03-15 01:41:33 +0000237
Brett Cannon2a922ed2009-03-09 03:35:50 +0000238
Brett Cannon69194272009-07-20 04:23:48 +0000239class ExecutionLoader(InspectLoader):
240
241 """Abstract base class for loaders that wish to support the execution of
242 modules as scripts.
243
244 This ABC represents one of the optional protocols specified in PEP 302.
245
246 """
247
248 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000249 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000250 """Abstract method which should return the value that __file__ is to be
Brett Cannon100883f2013-04-09 16:59:39 -0400251 set to.
252
253 Raises ImportError if the module cannot be found.
254 """
255 raise ImportError
Brett Cannon69194272009-07-20 04:23:48 +0000256
Brett Cannon3b62ca82013-05-27 21:11:04 -0400257 def get_code(self, fullname):
258 """Method to return the code object for fullname.
259
260 Should return None if not applicable (e.g. built-in module).
261 Raise ImportError if the module cannot be found.
262 """
263 source = self.get_source(fullname)
264 if source is None:
265 return None
266 try:
267 path = self.get_filename(fullname)
268 except ImportError:
269 return self.source_to_code(source)
270 else:
271 return self.source_to_code(source, path)
272
Eric Snow7e70fa52013-10-04 20:28:52 -0600273_register(ExecutionLoader, machinery.ExtensionFileLoader)
Eric Snow51794452013-10-03 12:08:55 -0600274
Brett Cannon69194272009-07-20 04:23:48 +0000275
Eric Snow32439d62015-05-02 19:15:18 -0600276class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader):
Brett Cannon938d44d2012-04-22 19:58:33 -0400277
278 """Abstract base class partially implementing the ResourceLoader and
279 ExecutionLoader ABCs."""
280
281_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200282 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400283
284
Eric Snow32439d62015-05-02 19:15:18 -0600285class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000286
Brett Cannonf23e3742010-06-27 23:57:46 +0000287 """Abstract base class for loading source code (and optionally any
288 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000289
Brett Cannonf23e3742010-06-27 23:57:46 +0000290 To support loading from source code, the abstractmethods inherited from
291 ResourceLoader and ExecutionLoader need to be implemented. To also support
292 loading from bytecode, the optional methods specified directly by this ABC
293 is required.
294
295 Inherited abstractmethods not implemented in this ABC:
296
297 * ResourceLoader.get_data
298 * ExecutionLoader.get_filename
299
300 """
301
Raymond Hettingercd92f372011-01-13 02:31:25 +0000302 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000303 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100304 if self.path_stats.__func__ is SourceLoader.path_stats:
Brett Cannon100883f2013-04-09 16:59:39 -0400305 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100306 return int(self.path_stats(path)['mtime'])
307
308 def path_stats(self, path):
309 """Return a metadata dict for the source pointed to by the path (str).
310 Possible keys:
311 - 'mtime' (mandatory) is the numeric timestamp of last source
312 code modification;
313 - 'size' (optional) is the size in bytes of the source code.
314 """
315 if self.path_mtime.__func__ is SourceLoader.path_mtime:
Brett Cannon100883f2013-04-09 16:59:39 -0400316 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100317 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000318
Raymond Hettingercd92f372011-01-13 02:31:25 +0000319 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000320 """Write the bytes to the path (if possible).
321
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000322 Accepts a str path and data as bytes.
323
Brett Cannon8d189072010-08-22 20:38:47 +0000324 Any needed intermediary directories are to be created. If for some
325 reason the file cannot be written because of permissions, fail
326 silently.
Brett Cannon8d189072010-08-22 20:38:47 +0000327 """
Brett Cannon8d189072010-08-22 20:38:47 +0000328
Brett Cannon938d44d2012-04-22 19:58:33 -0400329_register(SourceLoader, machinery.SourceFileLoader)