blob: 11e45f4a91361ebb5aba5aabb8f6a00cc02df169 [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.
40 Returns a Loader object.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100041 """
42 raise NotImplementedError
43
Nick Coghlan8a9080f2012-08-02 21:26:03 +100044
45class MetaPathFinder(Finder):
46
47 """Abstract base class for import finders on sys.meta_path."""
48
49 @abc.abstractmethod
50 def find_module(self, fullname, path):
Brett Cannonf4dc9202012-08-10 12:21:12 -040051 """Abstract method which, when implemented, should find a module.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100052 The fullname is a str and the path is a str or None.
53 Returns a Loader object.
54 """
55 raise NotImplementedError
56
Brett Cannonf4dc9202012-08-10 12:21:12 -040057 def invalidate_caches(self):
58 """An optional method for clearing the finder's cache, if any.
59 This method is used by importlib.invalidate_caches().
60 """
61 return NotImplemented
62
Nick Coghlan8a9080f2012-08-02 21:26:03 +100063_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
Nick Coghlanff794862012-08-02 21:45:24 +100064 machinery.PathFinder, machinery.WindowsRegistryFinder)
Nick Coghlan8a9080f2012-08-02 21:26:03 +100065
66
67class PathEntryFinder(Finder):
68
69 """Abstract base class for path entry finders used by PathFinder."""
70
71 @abc.abstractmethod
72 def find_loader(self, fullname):
Brett Cannonf4dc9202012-08-10 12:21:12 -040073 """Abstract method which, when implemented, returns a module loader.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100074 The fullname is a str. Returns a 2-tuple of (Loader, portion) where
75 portion is a sequence of file system locations contributing to part of
Brett Cannonf4dc9202012-08-10 12:21:12 -040076 a namespace package. The sequence may be empty and the loader may be
77 None.
Nick Coghlan8a9080f2012-08-02 21:26:03 +100078 """
79 raise NotImplementedError
80
Brett Cannonf410ce82012-08-10 17:41:23 -040081 find_module = _bootstrap._find_module_shim
Brett Cannonf4dc9202012-08-10 12:21:12 -040082
83 def invalidate_caches(self):
84 """An optional method for clearing the finder's cache, if any.
85 This method is used by PathFinder.invalidate_caches().
86 """
87 return NotImplemented
88
Nick Coghlan8a9080f2012-08-02 21:26:03 +100089_register(PathEntryFinder, machinery.FileFinder)
90
91
Brett Cannon2a922ed2009-03-09 03:35:50 +000092class Loader(metaclass=abc.ABCMeta):
93
Brett Cannon7aa21f72009-03-15 00:53:05 +000094 """Abstract base class for import loaders."""
Brett Cannon2a922ed2009-03-09 03:35:50 +000095
Brett Cannon7aa21f72009-03-15 00:53:05 +000096 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +000097 def load_module(self, fullname):
Raymond Hettingerd958ea72011-01-13 19:08:04 +000098 """Abstract method which when implemented should load a module.
99 The fullname is a str."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000100 raise NotImplementedError
101
Barry Warsaw38f75cb2012-07-31 16:39:43 -0400102 @abc.abstractmethod
Barry Warsawd7d21942012-07-29 16:36:17 -0400103 def module_repr(self, module):
104 """Abstract method which when implemented calculates and returns the
105 given module's repr."""
106 raise NotImplementedError
107
Brett Cannon2a922ed2009-03-09 03:35:50 +0000108
Brett Cannon2a922ed2009-03-09 03:35:50 +0000109class ResourceLoader(Loader):
110
Brett Cannon7aa21f72009-03-15 00:53:05 +0000111 """Abstract base class for loaders which can return data from their
112 back-end storage.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000113
114 This ABC represents one of the optional protocols specified by PEP 302.
115
116 """
117
118 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000119 def get_data(self, path):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000120 """Abstract method which when implemented should return the bytes for
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000121 the specified path. The path must be a str."""
Brett Cannon2a922ed2009-03-09 03:35:50 +0000122 raise NotImplementedError
123
124
125class InspectLoader(Loader):
126
Brett Cannon7aa21f72009-03-15 00:53:05 +0000127 """Abstract base class for loaders which support inspection about the
128 modules they can load.
Brett Cannon2a922ed2009-03-09 03:35:50 +0000129
130 This ABC represents one of the optional protocols specified by PEP 302.
131
132 """
133
134 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000135 def is_package(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000136 """Abstract method which when implemented should return whether the
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000137 module is a package. The fullname is a str. Returns a bool."""
Brett Cannonf23e3742010-06-27 23:57:46 +0000138 raise NotImplementedError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000139
140 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000141 def get_code(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000142 """Abstract method which when implemented should return the code object
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000143 for the module. The fullname is a str. Returns a types.CodeType."""
Brett Cannonf23e3742010-06-27 23:57:46 +0000144 raise NotImplementedError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000145
146 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000147 def get_source(self, fullname):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000148 """Abstract method which should return the source code for the
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000149 module. The fullname is a str. Returns a str."""
Brett Cannonf23e3742010-06-27 23:57:46 +0000150 raise NotImplementedError
Brett Cannon2a922ed2009-03-09 03:35:50 +0000151
Brett Cannon938d44d2012-04-22 19:58:33 -0400152_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter,
153 machinery.ExtensionFileLoader)
Brett Cannona113ac52009-03-15 01:41:33 +0000154
Brett Cannon2a922ed2009-03-09 03:35:50 +0000155
Brett Cannon69194272009-07-20 04:23:48 +0000156class ExecutionLoader(InspectLoader):
157
158 """Abstract base class for loaders that wish to support the execution of
159 modules as scripts.
160
161 This ABC represents one of the optional protocols specified in PEP 302.
162
163 """
164
165 @abc.abstractmethod
Raymond Hettingercd92f372011-01-13 02:31:25 +0000166 def get_filename(self, fullname):
Brett Cannon69194272009-07-20 04:23:48 +0000167 """Abstract method which should return the value that __file__ is to be
168 set to."""
169 raise NotImplementedError
170
171
Brett Cannon938d44d2012-04-22 19:58:33 -0400172class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):
173
174 """Abstract base class partially implementing the ResourceLoader and
175 ExecutionLoader ABCs."""
176
177_register(FileLoader, machinery.SourceFileLoader,
Marc-Andre Lemburg4fe29c92012-04-25 02:31:37 +0200178 machinery.SourcelessFileLoader)
Brett Cannon938d44d2012-04-22 19:58:33 -0400179
180
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000181class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
Brett Cannon2a922ed2009-03-09 03:35:50 +0000182
Brett Cannonf23e3742010-06-27 23:57:46 +0000183 """Abstract base class for loading source code (and optionally any
184 corresponding bytecode).
Brett Cannon2a922ed2009-03-09 03:35:50 +0000185
Brett Cannonf23e3742010-06-27 23:57:46 +0000186 To support loading from source code, the abstractmethods inherited from
187 ResourceLoader and ExecutionLoader need to be implemented. To also support
188 loading from bytecode, the optional methods specified directly by this ABC
189 is required.
190
191 Inherited abstractmethods not implemented in this ABC:
192
193 * ResourceLoader.get_data
194 * ExecutionLoader.get_filename
195
196 """
197
Raymond Hettingercd92f372011-01-13 02:31:25 +0000198 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000199 """Return the (int) modification time for the path (str)."""
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100200 if self.path_stats.__func__ is SourceLoader.path_stats:
201 raise NotImplementedError
202 return int(self.path_stats(path)['mtime'])
203
204 def path_stats(self, path):
205 """Return a metadata dict for the source pointed to by the path (str).
206 Possible keys:
207 - 'mtime' (mandatory) is the numeric timestamp of last source
208 code modification;
209 - 'size' (optional) is the size in bytes of the source code.
210 """
211 if self.path_mtime.__func__ is SourceLoader.path_mtime:
212 raise NotImplementedError
213 return {'mtime': self.path_mtime(path)}
Brett Cannon8d189072010-08-22 20:38:47 +0000214
Raymond Hettingercd92f372011-01-13 02:31:25 +0000215 def set_data(self, path, data):
Brett Cannon8d189072010-08-22 20:38:47 +0000216 """Write the bytes to the path (if possible).
217
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000218 Accepts a str path and data as bytes.
219
Brett Cannon8d189072010-08-22 20:38:47 +0000220 Any needed intermediary directories are to be created. If for some
221 reason the file cannot be written because of permissions, fail
222 silently.
223
224 """
225 raise NotImplementedError
226
Brett Cannon938d44d2012-04-22 19:58:33 -0400227_register(SourceLoader, machinery.SourceFileLoader)