blob: 3995ff2a6867d9b27f8baf38b825ec4ad33d0ad1 [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
46 # XXX Deprecate
Nick Coghlan8a9080f2012-08-02 21:26:03 +100047 def find_module(self, fullname, path):
Eric Snowb523f842013-11-22 09:05:39 -070048 """Return a loader for the module.
49
50 If no module is found, return None. The fullname is a str and
51 the path is a list of strings or None.
52
Nick Coghlan8a9080f2012-08-02 21:26:03 +100053 """
Eric Snowb523f842013-11-22 09:05:39 -070054 return None
Nick Coghlan8a9080f2012-08-02 21:26:03 +100055
Brett Cannonf4dc9202012-08-10 12:21:12 -040056 def invalidate_caches(self):
57 """An optional method for clearing the finder's cache, if any.
58 This method is used by importlib.invalidate_caches().
59 """
Brett Cannonf4dc9202012-08-10 12:21:12 -040060
Nick Coghlan8a9080f2012-08-02 21:26:03 +100061_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
Nick Coghlanff794862012-08-02 21:45:24 +100062 machinery.PathFinder, machinery.WindowsRegistryFinder)
Nick Coghlan8a9080f2012-08-02 21:26:03 +100063
64
65class PathEntryFinder(Finder):
66
67 """Abstract base class for path entry finders used by PathFinder."""
68
Eric Snowb523f842013-11-22 09:05:39 -070069 # We don't define find_spec() here since that would break
70 # hasattr checks we do to support backward compatibility.
71
72 # XXX Deprecate.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100073 def find_loader(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -070074 """Return (loader, namespace portion) for the path entry.
75
76 The fullname is a str. The namespace portion is a sequence of
77 path entries contributing to part of a namespace package. The
78 sequence may be empty. If loader is not None, the portion will
79 be ignored.
80
81 The portion will be discarded if another path entry finder
82 locates the module as a normal module or package.
83
Nick Coghlan8a9080f2012-08-02 21:26:03 +100084 """
Brett Cannon100883f2013-04-09 16:59:39 -040085 return None, []
Nick Coghlan8a9080f2012-08-02 21:26:03 +100086
Eric Snowb523f842013-11-22 09:05:39 -070087 # XXX Deprecate.
Brett Cannonf410ce82012-08-10 17:41:23 -040088 find_module = _bootstrap._find_module_shim
Brett Cannonf4dc9202012-08-10 12:21:12 -040089
90 def invalidate_caches(self):
91 """An optional method for clearing the finder's cache, if any.
92 This method is used by PathFinder.invalidate_caches().
93 """
Brett Cannonf4dc9202012-08-10 12:21:12 -040094
Nick Coghlan8a9080f2012-08-02 21:26:03 +100095_register(PathEntryFinder, machinery.FileFinder)
96
97
Brett Cannon2a922ed2009-03-09 03:35:50 +000098class Loader(metaclass=abc.ABCMeta):
99
Eric Snowb523f842013-11-22 09:05:39 -0700100 """Abstract base class for import loaders."""
Brett Cannon100883f2013-04-09 16:59:39 -0400101
Eric Snowb523f842013-11-22 09:05:39 -0700102 def create_module(self, spec):
103 """Return a module to initialize and into which to load.
Brett Cannon100883f2013-04-09 16:59:39 -0400104
Eric Snowb523f842013-11-22 09:05:39 -0700105 This method should raise ImportError if anything prevents it
106 from creating a new module. It may return None to indicate
107 that the spec should create the new module.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000108
Eric Snowb523f842013-11-22 09:05:39 -0700109 create_module() is optional.
110
111 """
112 # By default, defer to _SpecMethods.create() for the new module.
113 return None
114
115 # We don't define exec_module() here since that would break
116 # hasattr checks we do to support backward compatibility.
117
118 # XXX Deprecate.
Raymond Hettingercd92f372011-01-13 02:31:25 +0000119 def load_module(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700120 """Return the loaded module.
121
122 The module must be added to sys.modules and have import-related
123 attributes set properly. The fullname is a str.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000124
Brett Cannon100883f2013-04-09 16:59:39 -0400125 ImportError is raised on failure.
Eric Snowb523f842013-11-22 09:05:39 -0700126
Brett Cannon100883f2013-04-09 16:59:39 -0400127 """
128 raise ImportError
129
Eric Snowb523f842013-11-22 09:05:39 -0700130 # XXX Deprecate.
Barry Warsawd7d21942012-07-29 16:36:17 -0400131 def module_repr(self, module):
Brett Cannon100883f2013-04-09 16:59:39 -0400132 """Return a module's repr.
133
Brett Cannonf1d7b112013-05-31 18:39:07 -0400134 Used by the module type when the method does not raise
135 NotImplementedError.
Barry Warsawd7d21942012-07-29 16:36:17 -0400136
Eric Snowb523f842013-11-22 09:05:39 -0700137 """
138 # The exception will cause ModuleType.__repr__ to ignore this method.
139 raise NotImplementedError
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400140
Brett Cannon2a922ed2009-03-09 03:35:50 +0000141
Brett Cannon2a922ed2009-03-09 03:35:50 +0000142class ResourceLoader(Loader):
143
Brett Cannon7aa21f72009-03-15 00:53:05 +0000144 """Abstract base class for loaders which can return data from their
145 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000146
147 This ABC represents one of the optional protocols specified by PEP 302.
148
149 """
150
151 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000152 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000153 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000154 the specified path. The path must be a str."""
Brett Cannon100883f2013-04-09 16:59:39 -0400155 raise IOError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000156
157
158class InspectLoader(Loader):
159
Brett Cannon7aa21f72009-03-15 00:53:05 +0000160 """Abstract base class for loaders which support inspection about the
161 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000162
163 This ABC represents one of the optional protocols specified by PEP 302.
164
165 """
166
Raymond Hettingercd92f372011-01-13 02:31:25 +0000167 def is_package(self, fullname):
Eric Snowb523f842013-11-22 09:05:39 -0700168 """Optional method which when implemented should return whether the
Brett Cannon100883f2013-04-09 16:59:39 -0400169 module is a package. The fullname is a str. Returns a bool.
170
Eric Snowb523f842013-11-22 09:05:39 -0700171 Raises ImportError if the module cannot be found.
Brett Cannon100883f2013-04-09 16:59:39 -0400172 """
173 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000174
Raymond Hettingercd92f372011-01-13 02:31:25 +0000175 def get_code(self, fullname):
Brett Cannon3b62ca82013-05-27 21:11:04 -0400176 """Method which returns the code object for the module.
Brett Cannon100883f2013-04-09 16:59:39 -0400177
Brett Cannon3b62ca82013-05-27 21:11:04 -0400178 The fullname is a str. Returns a types.CodeType if possible, else
179 returns None if a code object does not make sense
180 (e.g. built-in module). Raises ImportError if the module cannot be
181 found.
Brett Cannon100883f2013-04-09 16:59:39 -0400182 """
Brett Cannon3b62ca82013-05-27 21:11:04 -0400183 source = self.get_source(fullname)
184 if source is None:
185 return None
186 return self.source_to_code(source)
Brett Cannon2a922ed2009-03-09 03:35:50 +0000187
188 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000189 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000190 """Abstract method which should return the source code for the
Brett Cannon100883f2013-04-09 16:59:39 -0400191 module. The fullname is a str. Returns a str.
192
193 Raises ImportError if the module cannot be found.
194 """
195 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000196
Brett Cannon9ffe85e2013-05-26 16:45:10 -0400197 def source_to_code(self, data, path='<string>'):
198 """Compile 'data' into a code object.
199
200 The 'data' argument can be anything that compile() can handle. The'path'
201 argument should be where the data was retrieved (when applicable)."""
202 return compile(data, path, 'exec', dont_inherit=True)
203
Eric Snowb523f842013-11-22 09:05:39 -0700204 exec_module = _bootstrap._LoaderBasics.exec_module
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400205 load_module = _bootstrap._LoaderBasics.load_module
206
Eric Snowb523f842013-11-22 09:05:39 -0700207_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
Brett Cannona113ac52009-03-15 01:41:33 +0000208
Brett Cannon2a922ed2009-03-09 03:35:50 +0000209
Brett Cannon69194272009-07-20 04:23:48 +0000210class ExecutionLoader(InspectLoader):
211
212 """Abstract base class for loaders that wish to support the execution of
213 modules as scripts.
214
215 This ABC represents one of the optional protocols specified in PEP 302.
216
217 """
218
219 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000220 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000221 """Abstract method which should return the value that __file__ is to be
Brett Cannon100883f2013-04-09 16:59:39 -0400222 set to.
223
224 Raises ImportError if the module cannot be found.
225 """
226 raise ImportError
Brett Cannon69194272009-07-20 04:23:48 +0000227
Brett Cannon3b62ca82013-05-27 21:11:04 -0400228 def get_code(self, fullname):
229 """Method to return the code object for fullname.
230
231 Should return None if not applicable (e.g. built-in module).
232 Raise ImportError if the module cannot be found.
233 """
234 source = self.get_source(fullname)
235 if source is None:
236 return None
237 try:
238 path = self.get_filename(fullname)
239 except ImportError:
240 return self.source_to_code(source)
241 else:
242 return self.source_to_code(source, path)
243
Eric Snow7e70fa52013-10-04 20:28:52 -0600244_register(ExecutionLoader, machinery.ExtensionFileLoader)
Eric Snow51794452013-10-03 12:08:55 -0600245
Brett Cannon69194272009-07-20 04:23:48 +0000246
Brett Cannon938d44d2012-04-22 19:58:33 -0400247class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):
248
249 """Abstract base class partially implementing the ResourceLoader and
250 ExecutionLoader ABCs."""
251
252_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200253 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400254
255
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000256class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000257
Brett Cannonf23e3742010-06-27 23:57:46 +0000258 """Abstract base class for loading source code (and optionally any
259 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000260
Brett Cannonf23e3742010-06-27 23:57:46 +0000261 To support loading from source code, the abstractmethods inherited from
262 ResourceLoader and ExecutionLoader need to be implemented. To also support
263 loading from bytecode, the optional methods specified directly by this ABC
264 is required.
265
266 Inherited abstractmethods not implemented in this ABC:
267
268 * ResourceLoader.get_data
269 * ExecutionLoader.get_filename
270
271 """
272
Raymond Hettingercd92f372011-01-13 02:31:25 +0000273 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000274 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100275 if self.path_stats.__func__ is SourceLoader.path_stats:
Brett Cannon100883f2013-04-09 16:59:39 -0400276 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100277 return int(self.path_stats(path)['mtime'])
278
279 def path_stats(self, path):
280 """Return a metadata dict for the source pointed to by the path (str).
281 Possible keys:
282 - 'mtime' (mandatory) is the numeric timestamp of last source
283 code modification;
284 - 'size' (optional) is the size in bytes of the source code.
285 """
286 if self.path_mtime.__func__ is SourceLoader.path_mtime:
Brett Cannon100883f2013-04-09 16:59:39 -0400287 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100288 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000289
Raymond Hettingercd92f372011-01-13 02:31:25 +0000290 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000291 """Write the bytes to the path (if possible).
292
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000293 Accepts a str path and data as bytes.
294
Brett Cannon8d189072010-08-22 20:38:47 +0000295 Any needed intermediary directories are to be created. If for some
296 reason the file cannot be written because of permissions, fail
297 silently.
Brett Cannon8d189072010-08-22 20:38:47 +0000298 """
Brett Cannon8d189072010-08-22 20:38:47 +0000299
Brett Cannon938d44d2012-04-22 19:58:33 -0400300_register(SourceLoader, machinery.SourceFileLoader)