blob: 558abd3d9a409e2c187f84994b4f55d8b65447a4 [file] [log] [blame]
Brett Cannon2a922ed2009-03-09 03:35:50 +00001"""Abstract base classes related to import."""
2from . import _bootstrap
3from . import machinery
Brett Cannon938d44d2012-04-22 19:58:33 -04004try:
5 import _frozen_importlib
Brett Cannoncd171c82013-07-04 17:43:24 -04006except ImportError as exc:
Brett Cannon938d44d2012-04-22 19:58:33 -04007 if exc.name != '_frozen_importlib':
8 raise
9 _frozen_importlib = None
Brett Cannon2a922ed2009-03-09 03:35:50 +000010import abc
Brett Cannon2a922ed2009-03-09 03:35:50 +000011
12
Brett Cannon938d44d2012-04-22 19:58:33 -040013def _register(abstract_cls, *classes):
14 for cls in classes:
15 abstract_cls.register(cls)
16 if _frozen_importlib is not None:
17 frozen_cls = getattr(_frozen_importlib, cls.__name__)
18 abstract_cls.register(frozen_cls)
19
20
Nick Coghlan8a9080f2012-08-02 21:26:03 +100021class Finder(metaclass=abc.ABCMeta):
22
Brett Cannonf4dc9202012-08-10 12:21:12 -040023 """Legacy abstract base class for import finders.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100024
Brett Cannonf4dc9202012-08-10 12:21:12 -040025 It may be subclassed for compatibility with legacy third party
26 reimplementations of the import system. Otherwise, finder
27 implementations should derive from the more specific MetaPathFinder
28 or PathEntryFinder ABCs.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100029 """
30
Brett Cannonf4dc9202012-08-10 12:21:12 -040031 @abc.abstractmethod
Nick Coghlan8a9080f2012-08-02 21:26:03 +100032 def find_module(self, fullname, path=None):
Brett Cannonf4dc9202012-08-10 12:21:12 -040033 """An abstract method that should find a module.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100034 The fullname is a str and the optional path is a str or None.
Brett Cannon100883f2013-04-09 16:59:39 -040035 Returns a Loader object or None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100036 """
Nick Coghlan8a9080f2012-08-02 21:26:03 +100037
Nick Coghlan8a9080f2012-08-02 21:26:03 +100038
39class MetaPathFinder(Finder):
40
41 """Abstract base class for import finders on sys.meta_path."""
42
Eric Snowb523f842013-11-22 09:05:39 -070043 # We don't define find_spec() here since that would break
44 # hasattr checks we do to support backward compatibility.
45
Nick Coghlan8a9080f2012-08-02 21:26:03 +100046 def find_module(self, fullname, path):
Eric Snowb523f842013-11-22 09:05:39 -070047 """Return a loader for the module.
48
49 If no module is found, return None. The fullname is a str and
50 the path is a list of strings or None.
51
Brett Cannon8d942292014-01-07 15:52:42 -050052 This method is deprecated in favor of finder.find_spec(). If find_spec()
53 exists then backwards-compatible functionality is provided for this
54 method.
Eric Snow1500d492014-01-06 20:49:04 -070055
Nick Coghlan8a9080f2012-08-02 21:26:03 +100056 """
Brett Cannon8d942292014-01-07 15:52:42 -050057 if not hasattr(self, 'find_spec'):
58 return None
59 found = self.find_spec(fullname, path)
60 return found.loader if found is not None else None
Nick Coghlan8a9080f2012-08-02 21:26:03 +100061
Brett Cannonf4dc9202012-08-10 12:21:12 -040062 def invalidate_caches(self):
63 """An optional method for clearing the finder's cache, if any.
64 This method is used by importlib.invalidate_caches().
65 """
Brett Cannonf4dc9202012-08-10 12:21:12 -040066
Nick Coghlan8a9080f2012-08-02 21:26:03 +100067_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
Nick Coghlanff794862012-08-02 21:45:24 +100068 machinery.PathFinder, machinery.WindowsRegistryFinder)
Nick Coghlan8a9080f2012-08-02 21:26:03 +100069
70
71class PathEntryFinder(Finder):
72
73 """Abstract base class for path entry finders used by PathFinder."""
74
Eric Snowb523f842013-11-22 09:05:39 -070075 # We don't define find_spec() here since that would break
76 # hasattr checks we do to support backward compatibility.
77
Nick Coghlan8a9080f2012-08-02 21:26:03 +100078 def find_loader(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -070079 """Return (loader, namespace portion) for the path entry.
80
81 The fullname is a str. The namespace portion is a sequence of
82 path entries contributing to part of a namespace package. The
83 sequence may be empty. If loader is not None, the portion will
84 be ignored.
85
86 The portion will be discarded if another path entry finder
87 locates the module as a normal module or package.
88
Brett Cannon8d942292014-01-07 15:52:42 -050089 This method is deprecated in favor of finder.find_spec(). If find_spec()
90 is provided than backwards-compatible functionality is provided.
Eric Snow1500d492014-01-06 20:49:04 -070091
Nick Coghlan8a9080f2012-08-02 21:26:03 +100092 """
Brett Cannon8d942292014-01-07 15:52:42 -050093 if not hasattr(self, 'find_spec'):
94 return None, []
95 found = self.find_spec(fullname)
96 if found is not None:
97 if not found.submodule_search_locations:
98 portions = []
99 else:
100 portions = found.submodule_search_locations
101 return found.loader, portions
102 else:
103 return None, []
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000104
Brett Cannonf410ce82012-08-10 17:41:23 -0400105 find_module = _bootstrap._find_module_shim
Brett Cannonf4dc9202012-08-10 12:21:12 -0400106
107 def invalidate_caches(self):
108 """An optional method for clearing the finder's cache, if any.
109 This method is used by PathFinder.invalidate_caches().
110 """
Brett Cannonf4dc9202012-08-10 12:21:12 -0400111
Nick Coghlan8a9080f2012-08-02 21:26:03 +1000112_register(PathEntryFinder, machinery.FileFinder)
113
114
Brett Cannon2a922ed2009-03-09 03:35:50 +0000115class Loader(metaclass=abc.ABCMeta):
116
Eric Snowb523f842013-11-22 09:05:39 -0700117 """Abstract base class for import loaders."""
Brett Cannon100883f2013-04-09 16:59:39 -0400118
Eric Snowb523f842013-11-22 09:05:39 -0700119 def create_module(self, spec):
120 """Return a module to initialize and into which to load.
Brett Cannon100883f2013-04-09 16:59:39 -0400121
Eric Snowb523f842013-11-22 09:05:39 -0700122 This method should raise ImportError if anything prevents it
123 from creating a new module. It may return None to indicate
124 that the spec should create the new module.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000125
Eric Snowb523f842013-11-22 09:05:39 -0700126 create_module() is optional.
127
128 """
129 # By default, defer to _SpecMethods.create() for the new module.
130 return None
131
132 # We don't define exec_module() here since that would break
133 # hasattr checks we do to support backward compatibility.
134
Raymond Hettingercd92f372011-01-13 02:31:25 +0000135 def load_module(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700136 """Return the loaded module.
137
138 The module must be added to sys.modules and have import-related
139 attributes set properly. The fullname is a str.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000140
Brett Cannon100883f2013-04-09 16:59:39 -0400141 ImportError is raised on failure.
Eric Snowb523f842013-11-22 09:05:39 -0700142
Brett Cannon8d942292014-01-07 15:52:42 -0500143 This method is deprecated in favor of loader.exec_module(). If
144 exec_module() exists then it is used to provide a backwards-compatible
145 functionality for this method.
Eric Snow1500d492014-01-06 20:49:04 -0700146
Brett Cannon100883f2013-04-09 16:59:39 -0400147 """
Brett Cannon8d942292014-01-07 15:52:42 -0500148 if not hasattr(self, 'exec_module'):
149 raise ImportError
150 return _bootstrap._load_module_shim(self, fullname)
Brett Cannon100883f2013-04-09 16:59:39 -0400151
Barry Warsawd7d21942012-07-29 16:36:17 -0400152 def module_repr(self, module):
Brett Cannon100883f2013-04-09 16:59:39 -0400153 """Return a module's repr.
154
Brett Cannonf1d7b112013-05-31 18:39:07 -0400155 Used by the module type when the method does not raise
156 NotImplementedError.
Barry Warsawd7d21942012-07-29 16:36:17 -0400157
Eric Snow1500d492014-01-06 20:49:04 -0700158 This method is deprecated.
159
Eric Snowb523f842013-11-22 09:05:39 -0700160 """
161 # The exception will cause ModuleType.__repr__ to ignore this method.
162 raise NotImplementedError
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400163
Brett Cannon2a922ed2009-03-09 03:35:50 +0000164
Brett Cannon2a922ed2009-03-09 03:35:50 +0000165class ResourceLoader(Loader):
166
Brett Cannon7aa21f72009-03-15 00:53:05 +0000167 """Abstract base class for loaders which can return data from their
168 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000169
170 This ABC represents one of the optional protocols specified by PEP 302.
171
172 """
173
174 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000175 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000176 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000177 the specified path. The path must be a str."""
Brett Cannon100883f2013-04-09 16:59:39 -0400178 raise IOError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000179
180
181class InspectLoader(Loader):
182
Brett Cannon7aa21f72009-03-15 00:53:05 +0000183 """Abstract base class for loaders which support inspection about the
184 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000185
186 This ABC represents one of the optional protocols specified by PEP 302.
187
188 """
189
Raymond Hettingercd92f372011-01-13 02:31:25 +0000190 def is_package(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700191 """Optional method which when implemented should return whether the
Brett Cannon100883f2013-04-09 16:59:39 -0400192 module is a package. The fullname is a str. Returns a bool.
193
Eric Snowb523f842013-11-22 09:05:39 -0700194 Raises ImportError if the module cannot be found.
Brett Cannon100883f2013-04-09 16:59:39 -0400195 """
196 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000197
Raymond Hettingercd92f372011-01-13 02:31:25 +0000198 def get_code(self, fullname):
Brett Cannon3b62ca82013-05-27 21:11:04 -0400199 """Method which returns the code object for the module.
Brett Cannon100883f2013-04-09 16:59:39 -0400200
Brett Cannon3b62ca82013-05-27 21:11:04 -0400201 The fullname is a str. Returns a types.CodeType if possible, else
202 returns None if a code object does not make sense
203 (e.g. built-in module). Raises ImportError if the module cannot be
204 found.
Brett Cannon100883f2013-04-09 16:59:39 -0400205 """
Brett Cannon3b62ca82013-05-27 21:11:04 -0400206 source = self.get_source(fullname)
207 if source is None:
208 return None
209 return self.source_to_code(source)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000210
211 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000212 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000213 """Abstract method which should return the source code for the
Brett Cannon100883f2013-04-09 16:59:39 -0400214 module. The fullname is a str. Returns a str.
215
216 Raises ImportError if the module cannot be found.
217 """
218 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000219
Brett Cannon9ffe85e2013-05-26 16:45:10 -0400220 def source_to_code(self, data, path='<string>'):
221 """Compile 'data' into a code object.
222
223 The 'data' argument can be anything that compile() can handle. The'path'
224 argument should be where the data was retrieved (when applicable)."""
225 return compile(data, path, 'exec', dont_inherit=True)
226
Eric Snowb523f842013-11-22 09:05:39 -0700227 exec_module = _bootstrap._LoaderBasics.exec_module
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400228 load_module = _bootstrap._LoaderBasics.load_module
229
Eric Snowb523f842013-11-22 09:05:39 -0700230_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
Brett Cannona113ac52009-03-15 01:41:33 +0000231
Brett Cannon2a922ed2009-03-09 03:35:50 +0000232
Brett Cannon69194272009-07-20 04:23:48 +0000233class ExecutionLoader(InspectLoader):
234
235 """Abstract base class for loaders that wish to support the execution of
236 modules as scripts.
237
238 This ABC represents one of the optional protocols specified in PEP 302.
239
240 """
241
242 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000243 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000244 """Abstract method which should return the value that __file__ is to be
Brett Cannon100883f2013-04-09 16:59:39 -0400245 set to.
246
247 Raises ImportError if the module cannot be found.
248 """
249 raise ImportError
Brett Cannon69194272009-07-20 04:23:48 +0000250
Brett Cannon3b62ca82013-05-27 21:11:04 -0400251 def get_code(self, fullname):
252 """Method to return the code object for fullname.
253
254 Should return None if not applicable (e.g. built-in module).
255 Raise ImportError if the module cannot be found.
256 """
257 source = self.get_source(fullname)
258 if source is None:
259 return None
260 try:
261 path = self.get_filename(fullname)
262 except ImportError:
263 return self.source_to_code(source)
264 else:
265 return self.source_to_code(source, path)
266
Eric Snow7e70fa52013-10-04 20:28:52 -0600267_register(ExecutionLoader, machinery.ExtensionFileLoader)
Eric Snow51794452013-10-03 12:08:55 -0600268
Brett Cannon69194272009-07-20 04:23:48 +0000269
Brett Cannon938d44d2012-04-22 19:58:33 -0400270class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):
271
272 """Abstract base class partially implementing the ResourceLoader and
273 ExecutionLoader ABCs."""
274
275_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200276 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400277
278
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000279class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000280
Brett Cannonf23e3742010-06-27 23:57:46 +0000281 """Abstract base class for loading source code (and optionally any
282 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000283
Brett Cannonf23e3742010-06-27 23:57:46 +0000284 To support loading from source code, the abstractmethods inherited from
285 ResourceLoader and ExecutionLoader need to be implemented. To also support
286 loading from bytecode, the optional methods specified directly by this ABC
287 is required.
288
289 Inherited abstractmethods not implemented in this ABC:
290
291 * ResourceLoader.get_data
292 * ExecutionLoader.get_filename
293
294 """
295
Raymond Hettingercd92f372011-01-13 02:31:25 +0000296 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000297 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100298 if self.path_stats.__func__ is SourceLoader.path_stats:
Brett Cannon100883f2013-04-09 16:59:39 -0400299 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100300 return int(self.path_stats(path)['mtime'])
301
302 def path_stats(self, path):
303 """Return a metadata dict for the source pointed to by the path (str).
304 Possible keys:
305 - 'mtime' (mandatory) is the numeric timestamp of last source
306 code modification;
307 - 'size' (optional) is the size in bytes of the source code.
308 """
309 if self.path_mtime.__func__ is SourceLoader.path_mtime:
Brett Cannon100883f2013-04-09 16:59:39 -0400310 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100311 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000312
Raymond Hettingercd92f372011-01-13 02:31:25 +0000313 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000314 """Write the bytes to the path (if possible).
315
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000316 Accepts a str path and data as bytes.
317
Brett Cannon8d189072010-08-22 20:38:47 +0000318 Any needed intermediary directories are to be created. If for some
319 reason the file cannot be written because of permissions, fail
320 silently.
Brett Cannon8d189072010-08-22 20:38:47 +0000321 """
Brett Cannon8d189072010-08-22 20:38:47 +0000322
Brett Cannon938d44d2012-04-22 19:58:33 -0400323_register(SourceLoader, machinery.SourceFileLoader)