blob: d2f45093a4555d21ec6cdb1fcd5df2b1afdda3d1 [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
Matthias Bussonnier1d4601c2017-02-15 18:00:32 -080016import warnings
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.
Matthias Bussonnier1d4601c2017-02-15 18:00:32 -080038
39 Deprecated since Python 3.3
Nick Coghlan8a9080f2012-08-02 21:26:03 +100040 """
41
Brett Cannonf4dc9202012-08-10 12:21:12 -040042 @abc.abstractmethod
Nick Coghlan8a9080f2012-08-02 21:26:03 +100043 def find_module(self, fullname, path=None):
Brett Cannonf4dc9202012-08-10 12:21:12 -040044 """An abstract method that should find a module.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100045 The fullname is a str and the optional path is a str or None.
Brett Cannon100883f2013-04-09 16:59:39 -040046 Returns a Loader object or None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100047 """
Nick Coghlan8a9080f2012-08-02 21:26:03 +100048
Nick Coghlan8a9080f2012-08-02 21:26:03 +100049
50class MetaPathFinder(Finder):
51
52 """Abstract base class for import finders on sys.meta_path."""
53
Eric Snowb523f842013-11-22 09:05:39 -070054 # We don't define find_spec() here since that would break
55 # hasattr checks we do to support backward compatibility.
56
Nick Coghlan8a9080f2012-08-02 21:26:03 +100057 def find_module(self, fullname, path):
Eric Snowb523f842013-11-22 09:05:39 -070058 """Return a loader for the module.
59
60 If no module is found, return None. The fullname is a str and
61 the path is a list of strings or None.
62
Matthias Bussonnier1d4601c2017-02-15 18:00:32 -080063 This method is deprecated since Python 3.4 in favor of
64 finder.find_spec(). If find_spec() exists then backwards-compatible
65 functionality is provided for this method.
Eric Snow1500d492014-01-06 20:49:04 -070066
Nick Coghlan8a9080f2012-08-02 21:26:03 +100067 """
Matthias Bussonnier1d4601c2017-02-15 18:00:32 -080068 warnings.warn("MetaPathFinder.find_module() is deprecated since Python "
69 "3.4 in favor of MetaPathFinder.find_spec()"
70 "(available since 3.4)",
71 DeprecationWarning,
72 stacklevel=2)
Brett Cannon8d942292014-01-07 15:52:42 -050073 if not hasattr(self, 'find_spec'):
74 return None
75 found = self.find_spec(fullname, path)
76 return found.loader if found is not None else None
Nick Coghlan8a9080f2012-08-02 21:26:03 +100077
Brett Cannonf4dc9202012-08-10 12:21:12 -040078 def invalidate_caches(self):
79 """An optional method for clearing the finder's cache, if any.
80 This method is used by importlib.invalidate_caches().
81 """
Brett Cannonf4dc9202012-08-10 12:21:12 -040082
Nick Coghlan8a9080f2012-08-02 21:26:03 +100083_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
Nick Coghlanff794862012-08-02 21:45:24 +100084 machinery.PathFinder, machinery.WindowsRegistryFinder)
Nick Coghlan8a9080f2012-08-02 21:26:03 +100085
86
87class PathEntryFinder(Finder):
88
89 """Abstract base class for path entry finders used by PathFinder."""
90
Eric Snowb523f842013-11-22 09:05:39 -070091 # We don't define find_spec() here since that would break
92 # hasattr checks we do to support backward compatibility.
93
Nick Coghlan8a9080f2012-08-02 21:26:03 +100094 def find_loader(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -070095 """Return (loader, namespace portion) for the path entry.
96
97 The fullname is a str. The namespace portion is a sequence of
98 path entries contributing to part of a namespace package. The
99 sequence may be empty. If loader is not None, the portion will
100 be ignored.
101
102 The portion will be discarded if another path entry finder
103 locates the module as a normal module or package.
104
Matthias Bussonnier1d4601c2017-02-15 18:00:32 -0800105 This method is deprecated since Python 3.4 in favor of
106 finder.find_spec(). If find_spec() is provided than backwards-compatible
107 functionality is provided.
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000108 """
Matthias Bussonnier1d4601c2017-02-15 18:00:32 -0800109 warnings.warn("PathEntryFinder.find_loader() is deprecated since Python "
110 "3.4 in favor of PathEntryFinder.find_spec() "
111 "(available since 3.4)",
112 DeprecationWarning,
113 stacklevel=2)
Brett Cannon8d942292014-01-07 15:52:42 -0500114 if not hasattr(self, 'find_spec'):
115 return None, []
116 found = self.find_spec(fullname)
117 if found is not None:
118 if not found.submodule_search_locations:
119 portions = []
120 else:
121 portions = found.submodule_search_locations
122 return found.loader, portions
123 else:
124 return None, []
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000125
Eric Snow32439d62015-05-02 19:15:18 -0600126 find_module = _bootstrap_external._find_module_shim
Brett Cannonf4dc9202012-08-10 12:21:12 -0400127
128 def invalidate_caches(self):
129 """An optional method for clearing the finder's cache, if any.
130 This method is used by PathFinder.invalidate_caches().
131 """
Brett Cannonf4dc9202012-08-10 12:21:12 -0400132
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000133_register(PathEntryFinder, machinery.FileFinder)
134
135
Brett Cannon2a922ed2009-03-09 03:35:50 +0000136class Loader(metaclass=abc.ABCMeta):
137
Eric Snowb523f842013-11-22 09:05:39 -0700138 """Abstract base class for import loaders."""
Brett Cannon100883f2013-04-09 16:59:39 -0400139
Eric Snowb523f842013-11-22 09:05:39 -0700140 def create_module(self, spec):
141 """Return a module to initialize and into which to load.
Brett Cannon100883f2013-04-09 16:59:39 -0400142
Eric Snowb523f842013-11-22 09:05:39 -0700143 This method should raise ImportError if anything prevents it
144 from creating a new module. It may return None to indicate
145 that the spec should create the new module.
Eric Snowb523f842013-11-22 09:05:39 -0700146 """
Brett Cannon2a17bde2014-05-30 14:55:29 -0400147 # By default, defer to default semantics for the new module.
Eric Snowb523f842013-11-22 09:05:39 -0700148 return None
149
150 # We don't define exec_module() here since that would break
151 # hasattr checks we do to support backward compatibility.
152
Raymond Hettingercd92f372011-01-13 02:31:25 +0000153 def load_module(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700154 """Return the loaded module.
155
156 The module must be added to sys.modules and have import-related
157 attributes set properly. The fullname is a str.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000158
Brett Cannon100883f2013-04-09 16:59:39 -0400159 ImportError is raised on failure.
Eric Snowb523f842013-11-22 09:05:39 -0700160
Brett Cannon8d942292014-01-07 15:52:42 -0500161 This method is deprecated in favor of loader.exec_module(). If
162 exec_module() exists then it is used to provide a backwards-compatible
163 functionality for this method.
Eric Snow1500d492014-01-06 20:49:04 -0700164
Brett Cannon100883f2013-04-09 16:59:39 -0400165 """
Brett Cannon8d942292014-01-07 15:52:42 -0500166 if not hasattr(self, 'exec_module'):
167 raise ImportError
Eric Snow183a9412015-05-15 21:54:59 -0600168 return _bootstrap._load_module_shim(self, fullname)
Brett Cannon100883f2013-04-09 16:59:39 -0400169
Barry Warsawd7d21942012-07-29 16:36:17 -0400170 def module_repr(self, module):
Brett Cannon100883f2013-04-09 16:59:39 -0400171 """Return a module's repr.
172
Brett Cannonf1d7b112013-05-31 18:39:07 -0400173 Used by the module type when the method does not raise
174 NotImplementedError.
Barry Warsawd7d21942012-07-29 16:36:17 -0400175
Eric Snow1500d492014-01-06 20:49:04 -0700176 This method is deprecated.
177
Eric Snowb523f842013-11-22 09:05:39 -0700178 """
179 # The exception will cause ModuleType.__repr__ to ignore this method.
180 raise NotImplementedError
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400181
Brett Cannon2a922ed2009-03-09 03:35:50 +0000182
Brett Cannon2a922ed2009-03-09 03:35:50 +0000183class ResourceLoader(Loader):
184
Brett Cannon7aa21f72009-03-15 00:53:05 +0000185 """Abstract base class for loaders which can return data from their
186 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000187
188 This ABC represents one of the optional protocols specified by PEP 302.
189
190 """
191
192 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000193 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000194 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000195 the specified path. The path must be a str."""
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300196 raise OSError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000197
198
199class InspectLoader(Loader):
200
Brett Cannon7aa21f72009-03-15 00:53:05 +0000201 """Abstract base class for loaders which support inspection about the
202 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000203
204 This ABC represents one of the optional protocols specified by PEP 302.
205
206 """
207
Raymond Hettingercd92f372011-01-13 02:31:25 +0000208 def is_package(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700209 """Optional method which when implemented should return whether the
Brett Cannon100883f2013-04-09 16:59:39 -0400210 module is a package. The fullname is a str. Returns a bool.
211
Eric Snowb523f842013-11-22 09:05:39 -0700212 Raises ImportError if the module cannot be found.
Brett Cannon100883f2013-04-09 16:59:39 -0400213 """
214 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000215
Raymond Hettingercd92f372011-01-13 02:31:25 +0000216 def get_code(self, fullname):
Brett Cannon3b62ca82013-05-27 21:11:04 -0400217 """Method which returns the code object for the module.
Brett Cannon100883f2013-04-09 16:59:39 -0400218
Brett Cannon3b62ca82013-05-27 21:11:04 -0400219 The fullname is a str. Returns a types.CodeType if possible, else
220 returns None if a code object does not make sense
221 (e.g. built-in module). Raises ImportError if the module cannot be
222 found.
Brett Cannon100883f2013-04-09 16:59:39 -0400223 """
Brett Cannon3b62ca82013-05-27 21:11:04 -0400224 source = self.get_source(fullname)
225 if source is None:
226 return None
227 return self.source_to_code(source)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000228
229 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000230 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000231 """Abstract method which should return the source code for the
Brett Cannon100883f2013-04-09 16:59:39 -0400232 module. The fullname is a str. Returns a str.
233
234 Raises ImportError if the module cannot be found.
235 """
236 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000237
Brett Cannon6eaac132014-05-09 12:28:22 -0400238 @staticmethod
239 def source_to_code(data, path='<string>'):
Brett Cannon9ffe85e2013-05-26 16:45:10 -0400240 """Compile 'data' into a code object.
241
242 The 'data' argument can be anything that compile() can handle. The'path'
243 argument should be where the data was retrieved (when applicable)."""
244 return compile(data, path, 'exec', dont_inherit=True)
245
Eric Snow32439d62015-05-02 19:15:18 -0600246 exec_module = _bootstrap_external._LoaderBasics.exec_module
247 load_module = _bootstrap_external._LoaderBasics.load_module
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400248
Eric Snowb523f842013-11-22 09:05:39 -0700249_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
Brett Cannona113ac52009-03-15 01:41:33 +0000250
Brett Cannon2a922ed2009-03-09 03:35:50 +0000251
Brett Cannon69194272009-07-20 04:23:48 +0000252class ExecutionLoader(InspectLoader):
253
254 """Abstract base class for loaders that wish to support the execution of
255 modules as scripts.
256
257 This ABC represents one of the optional protocols specified in PEP 302.
258
259 """
260
261 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000262 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000263 """Abstract method which should return the value that __file__ is to be
Brett Cannon100883f2013-04-09 16:59:39 -0400264 set to.
265
266 Raises ImportError if the module cannot be found.
267 """
268 raise ImportError
Brett Cannon69194272009-07-20 04:23:48 +0000269
Brett Cannon3b62ca82013-05-27 21:11:04 -0400270 def get_code(self, fullname):
271 """Method to return the code object for fullname.
272
273 Should return None if not applicable (e.g. built-in module).
274 Raise ImportError if the module cannot be found.
275 """
276 source = self.get_source(fullname)
277 if source is None:
278 return None
279 try:
280 path = self.get_filename(fullname)
281 except ImportError:
282 return self.source_to_code(source)
283 else:
284 return self.source_to_code(source, path)
285
Eric Snow7e70fa52013-10-04 20:28:52 -0600286_register(ExecutionLoader, machinery.ExtensionFileLoader)
Eric Snow51794452013-10-03 12:08:55 -0600287
Brett Cannon69194272009-07-20 04:23:48 +0000288
Eric Snow32439d62015-05-02 19:15:18 -0600289class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader):
Brett Cannon938d44d2012-04-22 19:58:33 -0400290
291 """Abstract base class partially implementing the ResourceLoader and
292 ExecutionLoader ABCs."""
293
294_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200295 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400296
297
Eric Snow32439d62015-05-02 19:15:18 -0600298class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000299
Brett Cannonf23e3742010-06-27 23:57:46 +0000300 """Abstract base class for loading source code (and optionally any
301 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000302
Brett Cannonf23e3742010-06-27 23:57:46 +0000303 To support loading from source code, the abstractmethods inherited from
304 ResourceLoader and ExecutionLoader need to be implemented. To also support
305 loading from bytecode, the optional methods specified directly by this ABC
306 is required.
307
308 Inherited abstractmethods not implemented in this ABC:
309
310 * ResourceLoader.get_data
311 * ExecutionLoader.get_filename
312
313 """
314
Raymond Hettingercd92f372011-01-13 02:31:25 +0000315 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000316 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100317 if self.path_stats.__func__ is SourceLoader.path_stats:
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300318 raise OSError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100319 return int(self.path_stats(path)['mtime'])
320
321 def path_stats(self, path):
322 """Return a metadata dict for the source pointed to by the path (str).
323 Possible keys:
324 - 'mtime' (mandatory) is the numeric timestamp of last source
325 code modification;
326 - 'size' (optional) is the size in bytes of the source code.
327 """
328 if self.path_mtime.__func__ is SourceLoader.path_mtime:
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300329 raise OSError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100330 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000331
Raymond Hettingercd92f372011-01-13 02:31:25 +0000332 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000333 """Write the bytes to the path (if possible).
334
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000335 Accepts a str path and data as bytes.
336
Brett Cannon8d189072010-08-22 20:38:47 +0000337 Any needed intermediary directories are to be created. If for some
338 reason the file cannot be written because of permissions, fail
339 silently.
Brett Cannon8d189072010-08-22 20:38:47 +0000340 """
Brett Cannon8d189072010-08-22 20:38:47 +0000341
Brett Cannon938d44d2012-04-22 19:58:33 -0400342_register(SourceLoader, machinery.SourceFileLoader)
Brett Cannon4ac51502017-12-15 16:29:35 -0800343
344
Barry Warsaw5ec0fee2018-01-15 15:07:11 -0800345class ResourceReader(metaclass=abc.ABCMeta):
Brett Cannon4ac51502017-12-15 16:29:35 -0800346
Brett Cannonbca42182018-01-12 15:08:59 -0800347 """Abstract base class to provide resource-reading support.
348
349 Loaders that support resource reading are expected to implement
350 the ``get_resource_reader(fullname)`` method and have it either return None
351 or an object compatible with this ABC.
352 """
Brett Cannon4ac51502017-12-15 16:29:35 -0800353
354 @abc.abstractmethod
355 def open_resource(self, resource):
356 """Return an opened, file-like object for binary reading.
357
358 The 'resource' argument is expected to represent only a file name
359 and thus not contain any subdirectory components.
360
361 If the resource cannot be found, FileNotFoundError is raised.
362 """
363 raise FileNotFoundError
364
365 @abc.abstractmethod
366 def resource_path(self, resource):
367 """Return the file system path to the specified resource.
368
369 The 'resource' argument is expected to represent only a file name
370 and thus not contain any subdirectory components.
371
372 If the resource does not exist on the file system, raise
373 FileNotFoundError.
374 """
375 raise FileNotFoundError
376
377 @abc.abstractmethod
378 def is_resource(self, name):
379 """Return True if the named 'name' is consider a resource."""
380 raise FileNotFoundError
381
382 @abc.abstractmethod
383 def contents(self):
384 """Return an iterator of strings over the contents of the package."""
385 return iter([])
Barry Warsaw5ec0fee2018-01-15 15:07:11 -0800386
387
388_register(ResourceReader, machinery.SourceFileLoader)