blob: 287848847bd269a2cf1b240c029a7e23adedd987 [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.
Eric Snowb523f842013-11-22 09:05:39 -0700125 """
Brett Cannon2a17bde2014-05-30 14:55:29 -0400126 # By default, defer to default semantics for the new module.
Eric Snowb523f842013-11-22 09:05:39 -0700127 return None
128
129 # We don't define exec_module() here since that would break
130 # hasattr checks we do to support backward compatibility.
131
Raymond Hettingercd92f372011-01-13 02:31:25 +0000132 def load_module(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700133 """Return the loaded module.
134
135 The module must be added to sys.modules and have import-related
136 attributes set properly. The fullname is a str.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000137
Brett Cannon100883f2013-04-09 16:59:39 -0400138 ImportError is raised on failure.
Eric Snowb523f842013-11-22 09:05:39 -0700139
Brett Cannon8d942292014-01-07 15:52:42 -0500140 This method is deprecated in favor of loader.exec_module(). If
141 exec_module() exists then it is used to provide a backwards-compatible
142 functionality for this method.
Eric Snow1500d492014-01-06 20:49:04 -0700143
Brett Cannon100883f2013-04-09 16:59:39 -0400144 """
Brett Cannon8d942292014-01-07 15:52:42 -0500145 if not hasattr(self, 'exec_module'):
146 raise ImportError
147 return _bootstrap._load_module_shim(self, fullname)
Brett Cannon100883f2013-04-09 16:59:39 -0400148
Barry Warsawd7d21942012-07-29 16:36:17 -0400149 def module_repr(self, module):
Brett Cannon100883f2013-04-09 16:59:39 -0400150 """Return a module's repr.
151
Brett Cannonf1d7b112013-05-31 18:39:07 -0400152 Used by the module type when the method does not raise
153 NotImplementedError.
Barry Warsawd7d21942012-07-29 16:36:17 -0400154
Eric Snow1500d492014-01-06 20:49:04 -0700155 This method is deprecated.
156
Eric Snowb523f842013-11-22 09:05:39 -0700157 """
158 # The exception will cause ModuleType.__repr__ to ignore this method.
159 raise NotImplementedError
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400160
Brett Cannon2a922ed2009-03-09 03:35:50 +0000161
Brett Cannon2a922ed2009-03-09 03:35:50 +0000162class ResourceLoader(Loader):
163
Brett Cannon7aa21f72009-03-15 00:53:05 +0000164 """Abstract base class for loaders which can return data from their
165 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000166
167 This ABC represents one of the optional protocols specified by PEP 302.
168
169 """
170
171 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000172 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000173 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000174 the specified path. The path must be a str."""
Brett Cannon100883f2013-04-09 16:59:39 -0400175 raise IOError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000176
177
178class InspectLoader(Loader):
179
Brett Cannon7aa21f72009-03-15 00:53:05 +0000180 """Abstract base class for loaders which support inspection about the
181 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000182
183 This ABC represents one of the optional protocols specified by PEP 302.
184
185 """
186
Raymond Hettingercd92f372011-01-13 02:31:25 +0000187 def is_package(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700188 """Optional method which when implemented should return whether the
Brett Cannon100883f2013-04-09 16:59:39 -0400189 module is a package. The fullname is a str. Returns a bool.
190
Eric Snowb523f842013-11-22 09:05:39 -0700191 Raises ImportError if the module cannot be found.
Brett Cannon100883f2013-04-09 16:59:39 -0400192 """
193 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000194
Raymond Hettingercd92f372011-01-13 02:31:25 +0000195 def get_code(self, fullname):
Brett Cannon3b62ca82013-05-27 21:11:04 -0400196 """Method which returns the code object for the module.
Brett Cannon100883f2013-04-09 16:59:39 -0400197
Brett Cannon3b62ca82013-05-27 21:11:04 -0400198 The fullname is a str. Returns a types.CodeType if possible, else
199 returns None if a code object does not make sense
200 (e.g. built-in module). Raises ImportError if the module cannot be
201 found.
Brett Cannon100883f2013-04-09 16:59:39 -0400202 """
Brett Cannon3b62ca82013-05-27 21:11:04 -0400203 source = self.get_source(fullname)
204 if source is None:
205 return None
206 return self.source_to_code(source)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000207
208 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000209 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000210 """Abstract method which should return the source code for the
Brett Cannon100883f2013-04-09 16:59:39 -0400211 module. The fullname is a str. Returns a str.
212
213 Raises ImportError if the module cannot be found.
214 """
215 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000216
Brett Cannon6eaac132014-05-09 12:28:22 -0400217 @staticmethod
218 def source_to_code(data, path='<string>'):
Brett Cannon9ffe85e2013-05-26 16:45:10 -0400219 """Compile 'data' into a code object.
220
221 The 'data' argument can be anything that compile() can handle. The'path'
222 argument should be where the data was retrieved (when applicable)."""
223 return compile(data, path, 'exec', dont_inherit=True)
224
Eric Snowb523f842013-11-22 09:05:39 -0700225 exec_module = _bootstrap._LoaderBasics.exec_module
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400226 load_module = _bootstrap._LoaderBasics.load_module
227
Eric Snowb523f842013-11-22 09:05:39 -0700228_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
Brett Cannona113ac52009-03-15 01:41:33 +0000229
Brett Cannon2a922ed2009-03-09 03:35:50 +0000230
Brett Cannon69194272009-07-20 04:23:48 +0000231class ExecutionLoader(InspectLoader):
232
233 """Abstract base class for loaders that wish to support the execution of
234 modules as scripts.
235
236 This ABC represents one of the optional protocols specified in PEP 302.
237
238 """
239
240 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000241 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000242 """Abstract method which should return the value that __file__ is to be
Brett Cannon100883f2013-04-09 16:59:39 -0400243 set to.
244
245 Raises ImportError if the module cannot be found.
246 """
247 raise ImportError
Brett Cannon69194272009-07-20 04:23:48 +0000248
Brett Cannon3b62ca82013-05-27 21:11:04 -0400249 def get_code(self, fullname):
250 """Method to return the code object for fullname.
251
252 Should return None if not applicable (e.g. built-in module).
253 Raise ImportError if the module cannot be found.
254 """
255 source = self.get_source(fullname)
256 if source is None:
257 return None
258 try:
259 path = self.get_filename(fullname)
260 except ImportError:
261 return self.source_to_code(source)
262 else:
263 return self.source_to_code(source, path)
264
Eric Snow7e70fa52013-10-04 20:28:52 -0600265_register(ExecutionLoader, machinery.ExtensionFileLoader)
Eric Snow51794452013-10-03 12:08:55 -0600266
Brett Cannon69194272009-07-20 04:23:48 +0000267
Brett Cannon938d44d2012-04-22 19:58:33 -0400268class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):
269
270 """Abstract base class partially implementing the ResourceLoader and
271 ExecutionLoader ABCs."""
272
273_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200274 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400275
276
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000277class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000278
Brett Cannonf23e3742010-06-27 23:57:46 +0000279 """Abstract base class for loading source code (and optionally any
280 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000281
Brett Cannonf23e3742010-06-27 23:57:46 +0000282 To support loading from source code, the abstractmethods inherited from
283 ResourceLoader and ExecutionLoader need to be implemented. To also support
284 loading from bytecode, the optional methods specified directly by this ABC
285 is required.
286
287 Inherited abstractmethods not implemented in this ABC:
288
289 * ResourceLoader.get_data
290 * ExecutionLoader.get_filename
291
292 """
293
Raymond Hettingercd92f372011-01-13 02:31:25 +0000294 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000295 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100296 if self.path_stats.__func__ is SourceLoader.path_stats:
Brett Cannon100883f2013-04-09 16:59:39 -0400297 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100298 return int(self.path_stats(path)['mtime'])
299
300 def path_stats(self, path):
301 """Return a metadata dict for the source pointed to by the path (str).
302 Possible keys:
303 - 'mtime' (mandatory) is the numeric timestamp of last source
304 code modification;
305 - 'size' (optional) is the size in bytes of the source code.
306 """
307 if self.path_mtime.__func__ is SourceLoader.path_mtime:
Brett Cannon100883f2013-04-09 16:59:39 -0400308 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100309 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000310
Raymond Hettingercd92f372011-01-13 02:31:25 +0000311 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000312 """Write the bytes to the path (if possible).
313
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000314 Accepts a str path and data as bytes.
315
Brett Cannon8d189072010-08-22 20:38:47 +0000316 Any needed intermediary directories are to be created. If for some
317 reason the file cannot be written because of permissions, fail
318 silently.
Brett Cannon8d189072010-08-22 20:38:47 +0000319 """
Brett Cannon8d189072010-08-22 20:38:47 +0000320
Brett Cannon938d44d2012-04-22 19:58:33 -0400321_register(SourceLoader, machinery.SourceFileLoader)