blob: 425b8bf8c79ffd54cf3ddae2d7dbae89df49857a [file] [log] [blame]
Brett Cannon23cbd8a2009-01-18 00:24:28 +00001"""Core implementation of import.
2
3This module is NOT meant to be directly imported! It has been designed such
4that it can be bootstrapped into Python as the implementation of import. As
5such it requires the injection of specific modules and attributes in order to
6work. One should use importlib as the public-facing version of this module.
7
8"""
9
Brett Cannon7c9875c2009-03-04 01:10:09 +000010# Injected modules are '_warnings', 'imp', 'sys', 'marshal', 'errno', '_io',
11# and '_os' (a.k.a. 'posix', 'nt' or 'os2').
Brett Cannon23cbd8a2009-01-18 00:24:28 +000012# Injected attribute is path_sep.
13#
14# When editing this code be aware that code executed at import time CANNOT
15# reference any injected objects! This includes not only global code but also
16# anything specified at the class level.
17
18
Brett Cannonce43ddf2009-03-12 22:28:55 +000019# Bootstrap-related code ######################################################
20
Brett Cannon23cbd8a2009-01-18 00:24:28 +000021# XXX Could also expose Modules/getpath.c:joinpath()
22def _path_join(*args):
23 """Replacement for os.path.join."""
24 return path_sep.join(x[:-len(path_sep)] if x.endswith(path_sep) else x
Brett Cannon61b14252010-07-03 21:48:25 +000025 for x in args if x)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000026
27
28def _path_exists(path):
29 """Replacement for os.path.exists."""
30 try:
31 _os.stat(path)
32 except OSError:
33 return False
34 else:
35 return True
36
37
38def _path_is_mode_type(path, mode):
39 """Test whether the path is the specified mode type."""
40 try:
41 stat_info = _os.stat(path)
42 except OSError:
43 return False
44 return (stat_info.st_mode & 0o170000) == mode
45
46
47# XXX Could also expose Modules/getpath.c:isfile()
48def _path_isfile(path):
49 """Replacement for os.path.isfile."""
50 return _path_is_mode_type(path, 0o100000)
51
52
53# XXX Could also expose Modules/getpath.c:isdir()
54def _path_isdir(path):
55 """Replacement for os.path.isdir."""
Brett Cannon61b14252010-07-03 21:48:25 +000056 if not path:
57 path = _os.getcwd()
Brett Cannon23cbd8a2009-01-18 00:24:28 +000058 return _path_is_mode_type(path, 0o040000)
59
60
61def _path_without_ext(path, ext_type):
62 """Replacement for os.path.splitext()[0]."""
Brett Cannon3eeaa0a2009-03-12 22:07:17 +000063 for suffix in _suffix_list(ext_type):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000064 if path.endswith(suffix):
65 return path[:-len(suffix)]
66 else:
67 raise ValueError("path is not of the specified type")
68
69
70def _path_absolute(path):
71 """Replacement for os.path.abspath."""
72 if not path:
73 path = _os.getcwd()
74 try:
75 return _os._getfullpathname(path)
76 except AttributeError:
77 if path.startswith('/'):
78 return path
79 else:
80 return _path_join(_os.getcwd(), path)
81
82
Brett Cannon3eeaa0a2009-03-12 22:07:17 +000083def _wrap(new, old):
Brett Cannon51d8bfc2009-02-07 02:13:28 +000084 """Simple substitute for functools.wraps."""
85 for replace in ['__module__', '__name__', '__doc__']:
86 setattr(new, replace, getattr(old, replace))
87 new.__dict__.update(old.__dict__)
88
89
Brett Cannon61b14252010-07-03 21:48:25 +000090code_type = type(_wrap.__code__)
91
Brett Cannonce43ddf2009-03-12 22:28:55 +000092# Finder/loader utility code ##################################################
93
Brett Cannon435aad82009-03-04 16:07:00 +000094def set_package(fxn):
Brett Cannon06c9d962009-02-07 01:52:25 +000095 """Set __package__ on the returned module."""
96 def wrapper(*args, **kwargs):
97 module = fxn(*args, **kwargs)
98 if not hasattr(module, '__package__') or module.__package__ is None:
99 module.__package__ = module.__name__
100 if not hasattr(module, '__path__'):
101 module.__package__ = module.__package__.rpartition('.')[0]
102 return module
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000103 _wrap(wrapper, fxn)
Brett Cannon06c9d962009-02-07 01:52:25 +0000104 return wrapper
105
106
Brett Cannon2cf03a82009-03-10 05:17:37 +0000107def set_loader(fxn):
108 """Set __loader__ on the returned module."""
109 def wrapper(self, *args, **kwargs):
110 module = fxn(self, *args, **kwargs)
111 if not hasattr(module, '__loader__'):
112 module.__loader__ = self
113 return module
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000114 _wrap(wrapper, fxn)
Brett Cannon2cf03a82009-03-10 05:17:37 +0000115 return wrapper
116
117
Brett Cannonce43ddf2009-03-12 22:28:55 +0000118def module_for_loader(fxn):
119 """Decorator to handle selecting the proper module for loaders.
120
Brett Cannon0e0d8a62009-03-15 00:00:19 +0000121 The decorated function is passed the module to use instead of the module
122 name. The module passed in to the function is either from sys.modules if
123 it already exists or is a new module which has __name__ set and is inserted
124 into sys.modules. If an exception is raised and the decorator created the
125 module it is subsequently removed from sys.modules.
Brett Cannonce43ddf2009-03-12 22:28:55 +0000126
Brett Cannon0e0d8a62009-03-15 00:00:19 +0000127 The decorator assumes that the decorated function takes the module name as
128 the second argument.
Brett Cannonce43ddf2009-03-12 22:28:55 +0000129
130 """
Brett Cannon61b14252010-07-03 21:48:25 +0000131 def decorated(self, fullname, *args, **kwargs):
Brett Cannonce43ddf2009-03-12 22:28:55 +0000132 module = sys.modules.get(fullname)
133 is_reload = bool(module)
134 if not is_reload:
135 # This must be done before open() is called as the 'io' module
136 # implicitly imports 'locale' and would otherwise trigger an
137 # infinite loop.
138 module = imp.new_module(fullname)
139 sys.modules[fullname] = module
140 try:
Brett Cannon61b14252010-07-03 21:48:25 +0000141 return fxn(self, module, *args, **kwargs)
Brett Cannonce43ddf2009-03-12 22:28:55 +0000142 except:
143 if not is_reload:
144 del sys.modules[fullname]
145 raise
146 _wrap(decorated, fxn)
147 return decorated
148
149
150def _check_name(method):
151 """Decorator to verify that the module being requested matches the one the
152 loader can handle.
153
154 The first argument (self) must define _name which the second argument is
Ezio Melotti42da6632011-03-15 05:18:48 +0200155 compared against. If the comparison fails then ImportError is raised.
Brett Cannonce43ddf2009-03-12 22:28:55 +0000156
157 """
158 def inner(self, name, *args, **kwargs):
159 if self._name != name:
160 raise ImportError("loader cannot handle %s" % name)
161 return method(self, name, *args, **kwargs)
162 _wrap(inner, method)
163 return inner
164
165
Brett Cannona113ac52009-03-15 01:41:33 +0000166def _requires_builtin(fxn):
167 """Decorator to verify the named module is built-in."""
168 def wrapper(self, fullname):
169 if fullname not in sys.builtin_module_names:
170 raise ImportError("{0} is not a built-in module".format(fullname))
171 return fxn(self, fullname)
172 _wrap(wrapper, fxn)
173 return wrapper
174
175
Brett Cannon8d110132009-03-15 02:20:16 +0000176def _requires_frozen(fxn):
177 """Decorator to verify the named module is frozen."""
178 def wrapper(self, fullname):
179 if not imp.is_frozen(fullname):
180 raise ImportError("{0} is not a frozen module".format(fullname))
181 return fxn(self, fullname)
182 _wrap(wrapper, fxn)
183 return wrapper
184
185
Brett Cannone9103d22009-03-12 22:37:06 +0000186def _suffix_list(suffix_type):
187 """Return a list of file suffixes based on the imp file type."""
188 return [suffix[0] for suffix in imp.get_suffixes()
189 if suffix[2] == suffix_type]
190
191
192# Loaders #####################################################################
Brett Cannonce43ddf2009-03-12 22:28:55 +0000193
Brett Cannon5abdc932009-01-22 22:43:07 +0000194class BuiltinImporter:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000195
Brett Cannon7aa21f72009-03-15 00:53:05 +0000196 """Meta path import for built-in modules.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000197
Brett Cannon7aa21f72009-03-15 00:53:05 +0000198 All methods are either class or static methods to avoid the need to
199 instantiate the class.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000200
201 """
202
Brett Cannon5abdc932009-01-22 22:43:07 +0000203 @classmethod
204 def find_module(cls, fullname, path=None):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000205 """Find the built-in module.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000206
207 If 'path' is ever specified then the search is considered a failure.
208
209 """
210 if path is not None:
211 return None
Brett Cannon5abdc932009-01-22 22:43:07 +0000212 return cls if imp.is_builtin(fullname) else None
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000213
Brett Cannon78246b62009-01-25 04:56:30 +0000214 @classmethod
Brett Cannon435aad82009-03-04 16:07:00 +0000215 @set_package
Brett Cannon2cf03a82009-03-10 05:17:37 +0000216 @set_loader
Brett Cannona113ac52009-03-15 01:41:33 +0000217 @_requires_builtin
Brett Cannon78246b62009-01-25 04:56:30 +0000218 def load_module(cls, fullname):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000219 """Load a built-in module."""
Brett Cannond2e7b332009-02-17 02:45:03 +0000220 is_reload = fullname in sys.modules
221 try:
222 return imp.init_builtin(fullname)
223 except:
224 if not is_reload and fullname in sys.modules:
225 del sys.modules[fullname]
226 raise
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000227
Brett Cannona113ac52009-03-15 01:41:33 +0000228 @classmethod
229 @_requires_builtin
230 def get_code(cls, fullname):
231 """Return None as built-in modules do not have code objects."""
232 return None
233
234 @classmethod
235 @_requires_builtin
236 def get_source(cls, fullname):
237 """Return None as built-in modules do not have source code."""
238 return None
239
240 @classmethod
241 @_requires_builtin
242 def is_package(cls, fullname):
243 """Return None as built-in module are never packages."""
244 return False
245
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000246
Brett Cannon5abdc932009-01-22 22:43:07 +0000247class FrozenImporter:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000248
Brett Cannon7aa21f72009-03-15 00:53:05 +0000249 """Meta path import for frozen modules.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000250
Brett Cannon7aa21f72009-03-15 00:53:05 +0000251 All methods are either class or static methods to avoid the need to
252 instantiate the class.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000253
Brett Cannon5abdc932009-01-22 22:43:07 +0000254 """
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000255
Brett Cannon5abdc932009-01-22 22:43:07 +0000256 @classmethod
257 def find_module(cls, fullname, path=None):
258 """Find a frozen module."""
259 return cls if imp.is_frozen(fullname) else None
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000260
Brett Cannon5abdc932009-01-22 22:43:07 +0000261 @classmethod
Brett Cannon435aad82009-03-04 16:07:00 +0000262 @set_package
Brett Cannon2cf03a82009-03-10 05:17:37 +0000263 @set_loader
Brett Cannon8d110132009-03-15 02:20:16 +0000264 @_requires_frozen
Brett Cannon5abdc932009-01-22 22:43:07 +0000265 def load_module(cls, fullname):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000266 """Load a frozen module."""
Brett Cannond2e7b332009-02-17 02:45:03 +0000267 is_reload = fullname in sys.modules
268 try:
269 return imp.init_frozen(fullname)
270 except:
271 if not is_reload and fullname in sys.modules:
272 del sys.modules[fullname]
273 raise
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000274
Brett Cannon8d110132009-03-15 02:20:16 +0000275 @classmethod
276 @_requires_frozen
277 def get_code(cls, fullname):
278 """Return the code object for the frozen module."""
279 return imp.get_frozen_object(fullname)
280
281 @classmethod
282 @_requires_frozen
283 def get_source(cls, fullname):
284 """Return None as frozen modules do not have source code."""
285 return None
286
287 @classmethod
288 @_requires_frozen
289 def is_package(cls, fullname):
290 """Return if the frozen module is a package."""
291 return imp.is_frozen_package(fullname)
292
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000293
Brett Cannon61b14252010-07-03 21:48:25 +0000294class _LoaderBasics:
295
296 """Base class of common code needed by both SourceLoader and
297 _SourcelessFileLoader."""
298
299 def is_package(self, fullname):
300 """Concrete implementation of InspectLoader.is_package by checking if
301 the path returned by get_filename has a filename of '__init__.py'."""
302 filename = self.get_filename(fullname).rpartition(path_sep)[2]
303 return filename.rsplit('.', 1)[0] == '__init__'
304
305 def _bytes_from_bytecode(self, fullname, data, source_mtime):
306 """Return the marshalled bytes from bytecode, verifying the magic
Ezio Melotti4969f702011-03-15 05:59:46 +0200307 number and timestamp along the way.
Brett Cannon61b14252010-07-03 21:48:25 +0000308
309 If source_mtime is None then skip the timestamp check.
310
311 """
312 magic = data[:4]
313 raw_timestamp = data[4:8]
314 if len(magic) != 4 or magic != imp.get_magic():
315 raise ImportError("bad magic number in {}".format(fullname))
316 elif len(raw_timestamp) != 4:
317 raise EOFError("bad timestamp in {}".format(fullname))
318 elif source_mtime is not None:
319 if marshal._r_long(raw_timestamp) != source_mtime:
320 raise ImportError("bytecode is stale for {}".format(fullname))
321 # Can't return the code object as errors from marshal loading need to
322 # propagate even when source is available.
323 return data[8:]
324
325 @module_for_loader
326 def _load_module(self, module, *, sourceless=False):
327 """Helper for load_module able to handle either source or sourceless
328 loading."""
329 name = module.__name__
330 code_object = self.get_code(name)
331 module.__file__ = self.get_filename(name)
332 if not sourceless:
333 module.__cached__ = imp.cache_from_source(module.__file__)
334 else:
335 module.__cached__ = module.__file__
336 module.__package__ = name
337 if self.is_package(name):
338 module.__path__ = [module.__file__.rsplit(path_sep, 1)[0]]
339 else:
340 module.__package__ = module.__package__.rpartition('.')[0]
341 module.__loader__ = self
342 exec(code_object, module.__dict__)
343 return module
344
345
346class SourceLoader(_LoaderBasics):
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000347
Raymond Hettingercd92f372011-01-13 02:31:25 +0000348 def path_mtime(self, path):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000349 """Optional method that returns the modification time (an int) for the
350 specified path, where path is a str.
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000351
352 Implementing this method allows the loader to read bytecode files.
353
354 """
355 raise NotImplementedError
356
Raymond Hettingercd92f372011-01-13 02:31:25 +0000357 def set_data(self, path, data):
Raymond Hettingerd958ea72011-01-13 19:08:04 +0000358 """Optional method which writes data (bytes) to a file path (a str).
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000359
360 Implementing this method allows for the writing of bytecode files.
361
362 """
363 raise NotImplementedError
364
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000365
366 def get_source(self, fullname):
367 """Concrete implementation of InspectLoader.get_source."""
368 import tokenize
369 path = self.get_filename(fullname)
370 try:
371 source_bytes = self.get_data(path)
372 except IOError:
373 raise ImportError("source not available through get_data()")
374 encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline)
Brett Cannon418182e2010-07-03 22:32:41 +0000375 newline_decoder = _io.IncrementalNewlineDecoder(None, True)
376 return newline_decoder.decode(source_bytes.decode(encoding[0]))
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000377
378 def get_code(self, fullname):
379 """Concrete implementation of InspectLoader.get_code.
380
381 Reading of bytecode requires path_mtime to be implemented. To write
382 bytecode, set_data must also be implemented.
383
384 """
385 source_path = self.get_filename(fullname)
386 bytecode_path = imp.cache_from_source(source_path)
387 source_mtime = None
388 if bytecode_path is not None:
389 try:
390 source_mtime = self.path_mtime(source_path)
391 except NotImplementedError:
392 pass
393 else:
394 try:
395 data = self.get_data(bytecode_path)
396 except IOError:
397 pass
398 else:
Brett Cannon61b14252010-07-03 21:48:25 +0000399 try:
400 bytes_data = self._bytes_from_bytecode(fullname, data,
401 source_mtime)
402 except (ImportError, EOFError):
403 pass
404 else:
405 found = marshal.loads(bytes_data)
406 if isinstance(found, code_type):
407 return found
408 else:
409 msg = "Non-code object in {}"
410 raise ImportError(msg.format(bytecode_path))
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000411 source_bytes = self.get_data(source_path)
412 code_object = compile(source_bytes, source_path, 'exec',
413 dont_inherit=True)
414 if (not sys.dont_write_bytecode and bytecode_path is not None and
415 source_mtime is not None):
416 # If e.g. Jython ever implements imp.cache_from_source to have
417 # their own cached file format, this block of code will most likely
418 # throw an exception.
419 data = bytearray(imp.get_magic())
420 data.extend(marshal._w_long(source_mtime))
421 data.extend(marshal.dumps(code_object))
422 try:
423 self.set_data(bytecode_path, data)
424 except NotImplementedError:
425 pass
426 return code_object
427
Brett Cannon61b14252010-07-03 21:48:25 +0000428 def load_module(self, fullname):
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000429 """Concrete implementation of Loader.load_module.
430
431 Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be
432 implemented to load source code. Use of bytecode is dictated by whether
433 get_code uses/writes bytecode.
434
435 """
Brett Cannon61b14252010-07-03 21:48:25 +0000436 return self._load_module(fullname)
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000437
438
Brett Cannon61b14252010-07-03 21:48:25 +0000439class _FileLoader:
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000440
Brett Cannon61b14252010-07-03 21:48:25 +0000441 """Base file loader class which implements the loader protocol methods that
442 require file system usage."""
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000443
444 def __init__(self, fullname, path):
Brett Cannon61b14252010-07-03 21:48:25 +0000445 """Cache the module name and the path to the file found by the
446 finder."""
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000447 self._name = fullname
448 self._path = path
449
450 @_check_name
451 def get_filename(self, fullname):
452 """Return the path to the source file as found by the finder."""
453 return self._path
454
Brett Cannon61b14252010-07-03 21:48:25 +0000455 def get_data(self, path):
456 """Return the data from path as raw bytes."""
Florent Xicluna764d6122010-09-03 19:55:26 +0000457 with _io.FileIO(path, 'r') as file:
Brett Cannon61b14252010-07-03 21:48:25 +0000458 return file.read()
459
460
461class _SourceFileLoader(_FileLoader, SourceLoader):
462
463 """Concrete implementation of SourceLoader using the file system."""
464
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000465 def path_mtime(self, path):
466 """Return the modification time for the path."""
467 return int(_os.stat(path).st_mtime)
468
Brett Cannon61b14252010-07-03 21:48:25 +0000469 def set_data(self, path, data):
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000470 """Write bytes data to a file."""
Brett Cannonee6d6472010-08-22 22:19:11 +0000471 parent, _, filename = path.rpartition(path_sep)
472 path_parts = []
473 # Figure out what directories are missing.
474 while parent and not _path_isdir(parent):
475 parent, _, part = parent.rpartition(path_sep)
476 path_parts.append(part)
477 # Create needed directories.
478 for part in reversed(path_parts):
479 parent = _path_join(parent, part)
480 try:
481 _os.mkdir(parent)
Brett Cannona7ceeb32010-08-26 21:07:13 +0000482 except OSError as exc:
Brett Cannonee6d6472010-08-22 22:19:11 +0000483 # Probably another Python process already created the dir.
484 if exc.errno == errno.EEXIST:
485 continue
Brett Cannona7ceeb32010-08-26 21:07:13 +0000486 else:
487 raise
488 except IOError as exc:
Brett Cannonee6d6472010-08-22 22:19:11 +0000489 # If can't get proper access, then just forget about writing
490 # the data.
Brett Cannona7ceeb32010-08-26 21:07:13 +0000491 if exc.errno == errno.EACCES:
Brett Cannonee6d6472010-08-22 22:19:11 +0000492 return
493 else:
494 raise
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000495 try:
Brett Cannonee6d6472010-08-22 22:19:11 +0000496 with _io.FileIO(path, 'wb') as file:
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000497 file.write(data)
498 except IOError as exc:
Brett Cannonee6d6472010-08-22 22:19:11 +0000499 # Don't worry if you can't write bytecode.
500 if exc.errno == errno.EACCES:
501 return
502 else:
Brett Cannon0cf9e6a2010-06-28 04:57:24 +0000503 raise
504
505
Brett Cannon61b14252010-07-03 21:48:25 +0000506class _SourcelessFileLoader(_FileLoader, _LoaderBasics):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000507
Brett Cannon61b14252010-07-03 21:48:25 +0000508 """Loader which handles sourceless file imports."""
Brett Cannon91cf8822009-02-21 05:41:15 +0000509
Brett Cannon61b14252010-07-03 21:48:25 +0000510 def load_module(self, fullname):
511 return self._load_module(fullname, sourceless=True)
Brett Cannon69194272009-07-20 04:23:48 +0000512
Brett Cannon91cf8822009-02-21 05:41:15 +0000513 def get_code(self, fullname):
Brett Cannon61b14252010-07-03 21:48:25 +0000514 path = self.get_filename(fullname)
515 data = self.get_data(path)
516 bytes_data = self._bytes_from_bytecode(fullname, data, None)
517 found = marshal.loads(bytes_data)
518 if isinstance(found, code_type):
519 return found
520 else:
521 raise ImportError("Non-code object in {}".format(path))
Brett Cannon91cf8822009-02-21 05:41:15 +0000522
Brett Cannond43b30b2009-03-10 03:29:23 +0000523 def get_source(self, fullname):
Brett Cannon61b14252010-07-03 21:48:25 +0000524 """Return None as there is no source code."""
525 return None
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000526
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000527
Brett Cannone9103d22009-03-12 22:37:06 +0000528class _ExtensionFileLoader:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000529
Brett Cannone9103d22009-03-12 22:37:06 +0000530 """Loader for extension modules.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000531
Brett Cannone9103d22009-03-12 22:37:06 +0000532 The constructor is designed to work with FileFinder.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000533
534 """
535
Brett Cannon61b14252010-07-03 21:48:25 +0000536 def __init__(self, name, path):
Brett Cannone9103d22009-03-12 22:37:06 +0000537 """Initialize the loader.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000538
Brett Cannone9103d22009-03-12 22:37:06 +0000539 If is_pkg is True then an exception is raised as extension modules
540 cannot be the __init__ module for an extension module.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000541
542 """
Brett Cannone9103d22009-03-12 22:37:06 +0000543 self._name = name
544 self._path = path
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000545
Brett Cannone9103d22009-03-12 22:37:06 +0000546 @_check_name
547 @set_package
548 @set_loader
549 def load_module(self, fullname):
550 """Load an extension module."""
551 is_reload = fullname in sys.modules
552 try:
553 return imp.load_dynamic(fullname, self._path)
554 except:
555 if not is_reload and fullname in sys.modules:
556 del sys.modules[fullname]
557 raise
558
559 @_check_name
560 def is_package(self, fullname):
561 """Return False as an extension module can never be a package."""
562 return False
563
564 @_check_name
565 def get_code(self, fullname):
566 """Return None as an extension module cannot create a code object."""
567 return None
568
569 @_check_name
570 def get_source(self, fullname):
571 """Return None as extension modules have no source code."""
572 return None
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000573
574
Brett Cannone9103d22009-03-12 22:37:06 +0000575# Finders #####################################################################
Brett Cannon4afab6b2009-02-21 03:31:35 +0000576
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000577class PathFinder:
Brett Cannon1d376682009-02-02 19:19:36 +0000578
579 """Meta path finder for sys.(path|path_hooks|path_importer_cache)."""
580
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000581 @classmethod
Brett Cannon32732e32009-02-15 05:48:13 +0000582 def _path_hooks(cls, path, hooks=None):
583 """Search sequence of hooks for a finder for 'path'.
Brett Cannon1d376682009-02-02 19:19:36 +0000584
Brett Cannon32732e32009-02-15 05:48:13 +0000585 If 'hooks' is false then use sys.path_hooks.
Brett Cannon1d376682009-02-02 19:19:36 +0000586
587 """
Brett Cannon32732e32009-02-15 05:48:13 +0000588 if not hooks:
589 hooks = sys.path_hooks
590 for hook in hooks:
Brett Cannon1d376682009-02-02 19:19:36 +0000591 try:
592 return hook(path)
593 except ImportError:
594 continue
595 else:
Brett Cannon32732e32009-02-15 05:48:13 +0000596 raise ImportError("no path hook found for {0}".format(path))
Brett Cannon1d376682009-02-02 19:19:36 +0000597
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000598 @classmethod
Brett Cannon32732e32009-02-15 05:48:13 +0000599 def _path_importer_cache(cls, path, default=None):
Brett Cannon1d376682009-02-02 19:19:36 +0000600 """Get the finder for the path from sys.path_importer_cache.
601
602 If the path is not in the cache, find the appropriate finder and cache
603 it. If None is cached, get the default finder and cache that
604 (if applicable).
605
606 Because of NullImporter, some finder should be returned. The only
607 explicit fail case is if None is cached but the path cannot be used for
608 the default hook, for which ImportError is raised.
609
610 """
611 try:
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000612 finder = sys.path_importer_cache[path]
Brett Cannon1d376682009-02-02 19:19:36 +0000613 except KeyError:
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000614 finder = cls._path_hooks(path)
Brett Cannon1d376682009-02-02 19:19:36 +0000615 sys.path_importer_cache[path] = finder
616 else:
Brett Cannon32732e32009-02-15 05:48:13 +0000617 if finder is None and default:
Brett Cannon1d376682009-02-02 19:19:36 +0000618 # Raises ImportError on failure.
Brett Cannon32732e32009-02-15 05:48:13 +0000619 finder = default(path)
Brett Cannon1d376682009-02-02 19:19:36 +0000620 sys.path_importer_cache[path] = finder
621 return finder
622
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000623 @classmethod
624 def find_module(cls, fullname, path=None):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000625 """Find the module on sys.path or 'path' based on sys.path_hooks and
626 sys.path_importer_cache."""
Brett Cannon1d376682009-02-02 19:19:36 +0000627 if not path:
628 path = sys.path
629 for entry in path:
630 try:
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000631 finder = cls._path_importer_cache(entry)
Brett Cannon1d376682009-02-02 19:19:36 +0000632 except ImportError:
633 continue
Brett Cannon8593a752009-03-30 19:57:15 +0000634 if finder:
635 loader = finder.find_module(fullname)
636 if loader:
637 return loader
Brett Cannon1d376682009-02-02 19:19:36 +0000638 else:
639 return None
640
641
Brett Cannonf87e04d2009-03-12 22:47:53 +0000642class _FileFinder:
Brett Cannone9103d22009-03-12 22:37:06 +0000643
Brett Cannon61b14252010-07-03 21:48:25 +0000644 """File-based finder.
Brett Cannone9103d22009-03-12 22:37:06 +0000645
Brett Cannon61b14252010-07-03 21:48:25 +0000646 Constructor takes a list of objects detailing what file extensions their
647 loader supports along with whether it can be used for a package.
Brett Cannone9103d22009-03-12 22:37:06 +0000648
649 """
650
Brett Cannon61b14252010-07-03 21:48:25 +0000651 def __init__(self, path, *details):
652 """Initialize with finder details."""
653 packages = []
654 modules = []
655 for detail in details:
656 modules.extend((suffix, detail.loader) for suffix in detail.suffixes)
657 if detail.supports_packages:
658 packages.extend((suffix, detail.loader)
659 for suffix in detail.suffixes)
660 self.packages = packages
661 self.modules = modules
662 self.path = path
Brett Cannone9103d22009-03-12 22:37:06 +0000663
Brett Cannon61b14252010-07-03 21:48:25 +0000664 def find_module(self, fullname):
665 """Try to find a loader for the specified module."""
Brett Cannone9103d22009-03-12 22:37:06 +0000666 tail_module = fullname.rpartition('.')[2]
Brett Cannon61b14252010-07-03 21:48:25 +0000667 base_path = _path_join(self.path, tail_module)
668 if _path_isdir(base_path) and _case_ok(self.path, tail_module):
669 for suffix, loader in self.packages:
670 init_filename = '__init__' + suffix
671 full_path = _path_join(base_path, init_filename)
672 if (_path_isfile(full_path) and
673 _case_ok(base_path, init_filename)):
674 return loader(fullname, full_path)
675 else:
676 msg = "Not importing directory {}: missing __init__"
677 _warnings.warn(msg.format(base_path), ImportWarning)
678 for suffix, loader in self.modules:
679 mod_filename = tail_module + suffix
680 full_path = _path_join(self.path, mod_filename)
681 if _path_isfile(full_path) and _case_ok(self.path, mod_filename):
682 return loader(fullname, full_path)
683 return None
684
685class _SourceFinderDetails:
686
687 loader = _SourceFileLoader
688 supports_packages = True
689
690 def __init__(self):
691 self.suffixes = _suffix_list(imp.PY_SOURCE)
692
693class _SourcelessFinderDetails:
694
695 loader = _SourcelessFileLoader
696 supports_packages = True
697
698 def __init__(self):
699 self.suffixes = _suffix_list(imp.PY_COMPILED)
Brett Cannone9103d22009-03-12 22:37:06 +0000700
701
Brett Cannon61b14252010-07-03 21:48:25 +0000702class _ExtensionFinderDetails:
Brett Cannone9103d22009-03-12 22:37:06 +0000703
Brett Cannon61b14252010-07-03 21:48:25 +0000704 loader = _ExtensionFileLoader
705 supports_packages = False
Brett Cannone9103d22009-03-12 22:37:06 +0000706
Brett Cannon61b14252010-07-03 21:48:25 +0000707 def __init__(self):
708 self.suffixes = _suffix_list(imp.C_EXTENSION)
Brett Cannone9103d22009-03-12 22:37:06 +0000709
710
Brett Cannonce43ddf2009-03-12 22:28:55 +0000711# Import itself ###############################################################
712
Brett Cannon61b14252010-07-03 21:48:25 +0000713def _file_path_hook(path):
714 """If the path is a directory, return a file-based finder."""
715 if _path_isdir(path):
716 return _FileFinder(path, _ExtensionFinderDetails(),
717 _SourceFinderDetails(),
718 _SourcelessFinderDetails())
719 else:
720 raise ImportError("only directories are supported")
Brett Cannonce43ddf2009-03-12 22:28:55 +0000721
722
Brett Cannon61b14252010-07-03 21:48:25 +0000723_DEFAULT_PATH_HOOK = _file_path_hook
Brett Cannon2dee5972009-02-21 03:15:37 +0000724
Brett Cannon32732e32009-02-15 05:48:13 +0000725class _DefaultPathFinder(PathFinder):
726
727 """Subclass of PathFinder that implements implicit semantics for
728 __import__."""
729
Brett Cannon32732e32009-02-15 05:48:13 +0000730 @classmethod
731 def _path_hooks(cls, path):
732 """Search sys.path_hooks as well as implicit path hooks."""
733 try:
734 return super()._path_hooks(path)
735 except ImportError:
Brett Cannon2dee5972009-02-21 03:15:37 +0000736 implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter]
Brett Cannon32732e32009-02-15 05:48:13 +0000737 return super()._path_hooks(path, implicit_hooks)
738
739 @classmethod
740 def _path_importer_cache(cls, path):
741 """Use the default path hook when None is stored in
742 sys.path_importer_cache."""
Brett Cannon2dee5972009-02-21 03:15:37 +0000743 return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK)
Brett Cannon32732e32009-02-15 05:48:13 +0000744
745
Brett Cannone9103d22009-03-12 22:37:06 +0000746class _ImportLockContext:
747
748 """Context manager for the import lock."""
749
750 def __enter__(self):
751 """Acquire the import lock."""
752 imp.acquire_lock()
753
754 def __exit__(self, exc_type, exc_value, exc_traceback):
755 """Release the import lock regardless of any raised exceptions."""
756 imp.release_lock()
757
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000758
Brett Cannon32732e32009-02-15 05:48:13 +0000759_IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, _DefaultPathFinder]
760
Brett Cannon0ffe6a92010-11-18 03:03:04 +0000761_ERR_MSG = 'No module named {}'
762
Brett Cannon7f9876c2009-02-06 02:47:33 +0000763def _gcd_import(name, package=None, level=0):
764 """Import and return the module based on its name, the package the call is
765 being made from, and the level adjustment.
766
767 This function represents the greatest common denominator of functionality
Brett Cannon2c318a12009-02-07 01:15:27 +0000768 between import_module and __import__. This includes settting __package__ if
769 the loader did not.
770
Brett Cannon7f9876c2009-02-06 02:47:33 +0000771 """
Brett Cannon2c318a12009-02-07 01:15:27 +0000772 if package:
773 if not hasattr(package, 'rindex'):
774 raise ValueError("__package__ not set to a string")
775 elif package not in sys.modules:
776 msg = ("Parent module {0!r} not loaded, cannot perform relative "
777 "import")
778 raise SystemError(msg.format(package))
779 if not name and level == 0:
780 raise ValueError("Empty module name")
Brett Cannon7f9876c2009-02-06 02:47:33 +0000781 if level > 0:
Brett Cannon2c318a12009-02-07 01:15:27 +0000782 dot = len(package)
Brett Cannon7f9876c2009-02-06 02:47:33 +0000783 for x in range(level, 1, -1):
784 try:
785 dot = package.rindex('.', 0, dot)
Brett Cannon7f9876c2009-02-06 02:47:33 +0000786 except ValueError:
Brett Cannon2c318a12009-02-07 01:15:27 +0000787 raise ValueError("attempted relative import beyond "
788 "top-level package")
789 if name:
790 name = "{0}.{1}".format(package[:dot], name)
791 else:
792 name = package[:dot]
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000793 with _ImportLockContext():
Brett Cannon7f9876c2009-02-06 02:47:33 +0000794 try:
Brett Cannon4d75fc12009-08-30 03:47:36 +0000795 module = sys.modules[name]
796 if module is None:
797 message = ("import of {} halted; "
798 "None in sys.modules".format(name))
799 raise ImportError(message)
800 return module
Brett Cannon7f9876c2009-02-06 02:47:33 +0000801 except KeyError:
802 pass
803 parent = name.rpartition('.')[0]
804 path = None
805 if parent:
806 if parent not in sys.modules:
Brett Cannon2c318a12009-02-07 01:15:27 +0000807 _gcd_import(parent)
808 # Backwards-compatibility; be nicer to skip the dict lookup.
809 parent_module = sys.modules[parent]
Brett Cannon1c1dcbf2009-08-30 20:22:21 +0000810 try:
811 path = parent_module.__path__
812 except AttributeError:
Brett Cannon0ffe6a92010-11-18 03:03:04 +0000813 msg = (_ERR_MSG + '; {} is not a package').format(name, parent)
814 raise ImportError(msg)
Brett Cannon32732e32009-02-15 05:48:13 +0000815 meta_path = sys.meta_path + _IMPLICIT_META_PATH
Brett Cannon2c318a12009-02-07 01:15:27 +0000816 for finder in meta_path:
Brett Cannon7f9876c2009-02-06 02:47:33 +0000817 loader = finder.find_module(name, path)
Brett Cannon2c318a12009-02-07 01:15:27 +0000818 if loader is not None:
819 loader.load_module(name)
820 break
Brett Cannon7f9876c2009-02-06 02:47:33 +0000821 else:
Brett Cannon0ffe6a92010-11-18 03:03:04 +0000822 raise ImportError(_ERR_MSG.format(name))
Brett Cannon2c318a12009-02-07 01:15:27 +0000823 # Backwards-compatibility; be nicer to skip the dict lookup.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000824 module = sys.modules[name]
825 if parent:
Brett Cannon2c318a12009-02-07 01:15:27 +0000826 # Set the module as an attribute on its parent.
827 setattr(parent_module, name.rpartition('.')[2], module)
828 # Set __package__ if the loader did not.
829 if not hasattr(module, '__package__') or module.__package__ is None:
830 # Watch out for what comes out of sys.modules to not be a module,
831 # e.g. an int.
832 try:
833 module.__package__ = module.__name__
834 if not hasattr(module, '__path__'):
835 module.__package__ = module.__package__.rpartition('.')[0]
836 except AttributeError:
837 pass
838 return module
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000839
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000840
Brett Cannon7aa21f72009-03-15 00:53:05 +0000841def __import__(name, globals={}, locals={}, fromlist=[], level=0):
Brett Cannon2c318a12009-02-07 01:15:27 +0000842 """Import a module.
843
844 The 'globals' argument is used to infer where the import is occuring from
845 to handle relative imports. The 'locals' argument is ignored. The
846 'fromlist' argument specifies what should exist as attributes on the module
847 being imported (e.g. ``from module import <fromlist>``). The 'level'
848 argument represents the package location to import from in a relative
849 import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
850
851 """
Brett Cannon6afbaef2009-08-30 19:08:58 +0000852 if not hasattr(name, 'rpartition'):
853 raise TypeError("module name must be str, not {}".format(type(name)))
Brett Cannon2c318a12009-02-07 01:15:27 +0000854 if level == 0:
855 module = _gcd_import(name)
856 else:
Brett Cannonde4ebfe2009-08-30 19:53:48 +0000857 # __package__ is not guaranteed to be defined or could be set to None
858 # to represent that it's proper value is unknown
859 package = globals.get('__package__')
860 if package is None:
Brett Cannon2c318a12009-02-07 01:15:27 +0000861 package = globals['__name__']
862 if '__path__' not in globals:
863 package = package.rpartition('.')[0]
864 module = _gcd_import(name, package, level)
865 # The hell that is fromlist ...
866 if not fromlist:
867 # Return up to the first dot in 'name'. This is complicated by the fact
868 # that 'name' may be relative.
869 if level == 0:
870 return sys.modules[name.partition('.')[0]]
871 elif not name:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000872 return module
Brett Cannon2c318a12009-02-07 01:15:27 +0000873 else:
874 cut_off = len(name) - len(name.partition('.')[0])
875 return sys.modules[module.__name__[:-cut_off]]
876 else:
877 # If a package was imported, try to import stuff from fromlist.
878 if hasattr(module, '__path__'):
879 if '*' in fromlist and hasattr(module, '__all__'):
Brett Cannon9e0e1a62009-08-30 18:28:46 +0000880 fromlist = list(fromlist)
Brett Cannon2c318a12009-02-07 01:15:27 +0000881 fromlist.remove('*')
882 fromlist.extend(module.__all__)
883 for x in (y for y in fromlist if not hasattr(module,y)):
884 try:
885 _gcd_import('{0}.{1}'.format(module.__name__, x))
886 except ImportError:
887 pass
888 return module