blob: 7752ac4778e50971244e6043e35a1773ea0c1730 [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
6except ImportError as exc:
7 if exc.name != '_frozen_importlib':
8 raise
9 _frozen_importlib = None
Brett Cannon2a922ed2009-03-09 03:35:50 +000010import abc
Brett Cannonf23e3742010-06-27 23:57:46 +000011import imp
Brett Cannonf23e3742010-06-27 23:57:46 +000012import marshal
Brett Cannonf23e3742010-06-27 23:57:46 +000013import sys
14import tokenize
Brett Cannonf23e3742010-06-27 23:57:46 +000015import warnings
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:
22 frozen_cls = getattr(_frozen_importlib, cls.__name__)
23 abstract_cls.register(frozen_cls)
24
25
Nick Coghlan8a9080f2012-08-02 21:26:03 +100026class Finder(metaclass=abc.ABCMeta):
27
Brett Cannonf4dc9202012-08-10 12:21:12 -040028 """Legacy abstract base class for import finders.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100029
Brett Cannonf4dc9202012-08-10 12:21:12 -040030 It may be subclassed for compatibility with legacy third party
31 reimplementations of the import system. Otherwise, finder
32 implementations should derive from the more specific MetaPathFinder
33 or PathEntryFinder ABCs.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100034 """
35
Brett Cannonf4dc9202012-08-10 12:21:12 -040036 @abc.abstractmethod
Nick Coghlan8a9080f2012-08-02 21:26:03 +100037 def find_module(self, fullname, path=None):
Brett Cannonf4dc9202012-08-10 12:21:12 -040038 """An abstract method that should find a module.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100039 The fullname is a str and the optional path is a str or None.
Brett Cannon100883f2013-04-09 16:59:39 -040040 Returns a Loader object or None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100041 """
Nick Coghlan8a9080f2012-08-02 21:26:03 +100042
Nick Coghlan8a9080f2012-08-02 21:26:03 +100043
44class MetaPathFinder(Finder):
45
46 """Abstract base class for import finders on sys.meta_path."""
47
48 @abc.abstractmethod
49 def find_module(self, fullname, path):
Brett Cannonf4dc9202012-08-10 12:21:12 -040050 """Abstract method which, when implemented, should find a module.
Brett Cannon100883f2013-04-09 16:59:39 -040051 The fullname is a str and the path is a list of strings or None.
52 Returns a Loader object or None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100053 """
Nick Coghlan8a9080f2012-08-02 21:26:03 +100054
Brett Cannonf4dc9202012-08-10 12:21:12 -040055 def invalidate_caches(self):
56 """An optional method for clearing the finder's cache, if any.
57 This method is used by importlib.invalidate_caches().
58 """
Brett Cannonf4dc9202012-08-10 12:21:12 -040059
Nick Coghlan8a9080f2012-08-02 21:26:03 +100060_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
Nick Coghlanff794862012-08-02 21:45:24 +100061 machinery.PathFinder, machinery.WindowsRegistryFinder)
Nick Coghlan8a9080f2012-08-02 21:26:03 +100062
63
64class PathEntryFinder(Finder):
65
66 """Abstract base class for path entry finders used by PathFinder."""
67
68 @abc.abstractmethod
69 def find_loader(self, fullname):
Brett Cannon100883f2013-04-09 16:59:39 -040070 """Abstract method which, when implemented, returns a module loader or
71 a possible part of a namespace.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100072 The fullname is a str. Returns a 2-tuple of (Loader, portion) where
73 portion is a sequence of file system locations contributing to part of
Brett Cannonf4dc9202012-08-10 12:21:12 -040074 a namespace package. The sequence may be empty and the loader may be
75 None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100076 """
Brett Cannon100883f2013-04-09 16:59:39 -040077 return None, []
Nick Coghlan8a9080f2012-08-02 21:26:03 +100078
Brett Cannonf410ce82012-08-10 17:41:23 -040079 find_module = _bootstrap._find_module_shim
Brett Cannonf4dc9202012-08-10 12:21:12 -040080
81 def invalidate_caches(self):
82 """An optional method for clearing the finder's cache, if any.
83 This method is used by PathFinder.invalidate_caches().
84 """
Brett Cannonf4dc9202012-08-10 12:21:12 -040085
Nick Coghlan8a9080f2012-08-02 21:26:03 +100086_register(PathEntryFinder, machinery.FileFinder)
87
88
Brett Cannon2a922ed2009-03-09 03:35:50 +000089class Loader(metaclass=abc.ABCMeta):
90
Brett Cannon100883f2013-04-09 16:59:39 -040091 """Abstract base class for import loaders.
92
93 The optional method module_repr(module) may be defined to provide a
94 repr for a module when appropriate (see PEP 420). The __repr__() method on
95 the module type will use the method as appropriate.
96
97 """
Brett Cannon2a922ed2009-03-09 03:35:50 +000098
Brett Cannon7aa21f72009-03-15 00:53:05 +000099 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000100 def load_module(self, fullname):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000101 """Abstract method which when implemented should load a module.
Brett Cannon100883f2013-04-09 16:59:39 -0400102 The fullname is a str.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000103
Brett Cannon100883f2013-04-09 16:59:39 -0400104 ImportError is raised on failure.
105 """
106 raise ImportError
107
Barry Warsawd7d21942012-07-29 16:36:17 -0400108 def module_repr(self, module):
Brett Cannon100883f2013-04-09 16:59:39 -0400109 """Return a module's repr.
110
111 Used by the module type when implemented without raising an exception.
112 """
Barry Warsawd7d21942012-07-29 16:36:17 -0400113 raise NotImplementedError
114
Brett Cannon2a922ed2009-03-09 03:35:50 +0000115
Brett Cannon2a922ed2009-03-09 03:35:50 +0000116class ResourceLoader(Loader):
117
Brett Cannon7aa21f72009-03-15 00:53:05 +0000118 """Abstract base class for loaders which can return data from their
119 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000120
121 This ABC represents one of the optional protocols specified by PEP 302.
122
123 """
124
125 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000126 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000127 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000128 the specified path. The path must be a str."""
Brett Cannon100883f2013-04-09 16:59:39 -0400129 raise IOError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000130
131
132class InspectLoader(Loader):
133
Brett Cannon7aa21f72009-03-15 00:53:05 +0000134 """Abstract base class for loaders which support inspection about the
135 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000136
137 This ABC represents one of the optional protocols specified by PEP 302.
138
139 """
140
141 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000142 def is_package(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000143 """Abstract method which when implemented should return whether the
Brett Cannon100883f2013-04-09 16:59:39 -0400144 module is a package. The fullname is a str. Returns a bool.
145
146 Raises ImportError is the module cannot be found.
147 """
148 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000149
150 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000151 def get_code(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000152 """Abstract method which when implemented should return the code object
Brett Cannon100883f2013-04-09 16:59:39 -0400153 for the module. The fullname is a str. Returns a types.CodeType.
154
155 Raises ImportError if the module cannot be found.
156 """
157 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000158
159 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000160 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000161 """Abstract method which should return the source code for the
Brett Cannon100883f2013-04-09 16:59:39 -0400162 module. The fullname is a str. Returns a str.
163
164 Raises ImportError if the module cannot be found.
165 """
166 raise ImportError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000167
Brett Cannon938d44d2012-04-22 19:58:33 -0400168_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter,
169 machinery.ExtensionFileLoader)
Brett Cannona113ac52009-03-15 01:41:33 +0000170
Brett Cannon2a922ed2009-03-09 03:35:50 +0000171
Brett Cannon69194272009-07-20 04:23:48 +0000172class ExecutionLoader(InspectLoader):
173
174 """Abstract base class for loaders that wish to support the execution of
175 modules as scripts.
176
177 This ABC represents one of the optional protocols specified in PEP 302.
178
179 """
180
181 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000182 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000183 """Abstract method which should return the value that __file__ is to be
Brett Cannon100883f2013-04-09 16:59:39 -0400184 set to.
185
186 Raises ImportError if the module cannot be found.
187 """
188 raise ImportError
Brett Cannon69194272009-07-20 04:23:48 +0000189
190
Brett Cannon938d44d2012-04-22 19:58:33 -0400191class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):
192
193 """Abstract base class partially implementing the ResourceLoader and
194 ExecutionLoader ABCs."""
195
196_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200197 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400198
199
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000200class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000201
Brett Cannonf23e3742010-06-27 23:57:46 +0000202 """Abstract base class for loading source code (and optionally any
203 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000204
Brett Cannonf23e3742010-06-27 23:57:46 +0000205 To support loading from source code, the abstractmethods inherited from
206 ResourceLoader and ExecutionLoader need to be implemented. To also support
207 loading from bytecode, the optional methods specified directly by this ABC
208 is required.
209
210 Inherited abstractmethods not implemented in this ABC:
211
212 * ResourceLoader.get_data
213 * ExecutionLoader.get_filename
214
215 """
216
Raymond Hettingercd92f372011-01-13 02:31:25 +0000217 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000218 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100219 if self.path_stats.__func__ is SourceLoader.path_stats:
Brett Cannon100883f2013-04-09 16:59:39 -0400220 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100221 return int(self.path_stats(path)['mtime'])
222
223 def path_stats(self, path):
224 """Return a metadata dict for the source pointed to by the path (str).
225 Possible keys:
226 - 'mtime' (mandatory) is the numeric timestamp of last source
227 code modification;
228 - 'size' (optional) is the size in bytes of the source code.
229 """
230 if self.path_mtime.__func__ is SourceLoader.path_mtime:
Brett Cannon100883f2013-04-09 16:59:39 -0400231 raise IOError
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100232 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000233
Raymond Hettingercd92f372011-01-13 02:31:25 +0000234 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000235 """Write the bytes to the path (if possible).
236
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000237 Accepts a str path and data as bytes.
238
Brett Cannon8d189072010-08-22 20:38:47 +0000239 Any needed intermediary directories are to be created. If for some
240 reason the file cannot be written because of permissions, fail
241 silently.
Brett Cannon8d189072010-08-22 20:38:47 +0000242 """
Brett Cannon8d189072010-08-22 20:38:47 +0000243
Brett Cannon938d44d2012-04-22 19:58:33 -0400244_register(SourceLoader, machinery.SourceFileLoader)