blob: d5b909e32f1bf36669deddae175594437f662089 [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
25 for x in args)
26
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."""
56 return _path_is_mode_type(path, 0o040000)
57
58
59def _path_without_ext(path, ext_type):
60 """Replacement for os.path.splitext()[0]."""
Brett Cannon3eeaa0a2009-03-12 22:07:17 +000061 for suffix in _suffix_list(ext_type):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000062 if path.endswith(suffix):
63 return path[:-len(suffix)]
64 else:
65 raise ValueError("path is not of the specified type")
66
67
68def _path_absolute(path):
69 """Replacement for os.path.abspath."""
70 if not path:
71 path = _os.getcwd()
72 try:
73 return _os._getfullpathname(path)
74 except AttributeError:
75 if path.startswith('/'):
76 return path
77 else:
78 return _path_join(_os.getcwd(), path)
79
80
Brett Cannon3eeaa0a2009-03-12 22:07:17 +000081class _closing:
Brett Cannon23cbd8a2009-01-18 00:24:28 +000082
83 """Simple replacement for contextlib.closing."""
84
85 def __init__(self, obj):
86 self.obj = obj
87
88 def __enter__(self):
89 return self.obj
90
91 def __exit__(self, *args):
92 self.obj.close()
93
94
Brett Cannon3eeaa0a2009-03-12 22:07:17 +000095def _wrap(new, old):
Brett Cannon51d8bfc2009-02-07 02:13:28 +000096 """Simple substitute for functools.wraps."""
97 for replace in ['__module__', '__name__', '__doc__']:
98 setattr(new, replace, getattr(old, replace))
99 new.__dict__.update(old.__dict__)
100
101
Brett Cannonce43ddf2009-03-12 22:28:55 +0000102# Finder/loader utility code ##################################################
103
Brett Cannon435aad82009-03-04 16:07:00 +0000104def set_package(fxn):
Brett Cannon06c9d962009-02-07 01:52:25 +0000105 """Set __package__ on the returned module."""
106 def wrapper(*args, **kwargs):
107 module = fxn(*args, **kwargs)
108 if not hasattr(module, '__package__') or module.__package__ is None:
109 module.__package__ = module.__name__
110 if not hasattr(module, '__path__'):
111 module.__package__ = module.__package__.rpartition('.')[0]
112 return module
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000113 _wrap(wrapper, fxn)
Brett Cannon06c9d962009-02-07 01:52:25 +0000114 return wrapper
115
116
Brett Cannon2cf03a82009-03-10 05:17:37 +0000117def set_loader(fxn):
118 """Set __loader__ on the returned module."""
119 def wrapper(self, *args, **kwargs):
120 module = fxn(self, *args, **kwargs)
121 if not hasattr(module, '__loader__'):
122 module.__loader__ = self
123 return module
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000124 _wrap(wrapper, fxn)
Brett Cannon2cf03a82009-03-10 05:17:37 +0000125 return wrapper
126
127
Brett Cannonce43ddf2009-03-12 22:28:55 +0000128def module_for_loader(fxn):
129 """Decorator to handle selecting the proper module for loaders.
130
Brett Cannon0e0d8a62009-03-15 00:00:19 +0000131 The decorated function is passed the module to use instead of the module
132 name. The module passed in to the function is either from sys.modules if
133 it already exists or is a new module which has __name__ set and is inserted
134 into sys.modules. If an exception is raised and the decorator created the
135 module it is subsequently removed from sys.modules.
Brett Cannonce43ddf2009-03-12 22:28:55 +0000136
Brett Cannon0e0d8a62009-03-15 00:00:19 +0000137 The decorator assumes that the decorated function takes the module name as
138 the second argument.
Brett Cannonce43ddf2009-03-12 22:28:55 +0000139
140 """
141 def decorated(self, fullname):
142 module = sys.modules.get(fullname)
143 is_reload = bool(module)
144 if not is_reload:
145 # This must be done before open() is called as the 'io' module
146 # implicitly imports 'locale' and would otherwise trigger an
147 # infinite loop.
148 module = imp.new_module(fullname)
149 sys.modules[fullname] = module
150 try:
151 return fxn(self, module)
152 except:
153 if not is_reload:
154 del sys.modules[fullname]
155 raise
156 _wrap(decorated, fxn)
157 return decorated
158
159
160def _check_name(method):
161 """Decorator to verify that the module being requested matches the one the
162 loader can handle.
163
164 The first argument (self) must define _name which the second argument is
165 comapred against. If the comparison fails then ImportError is raised.
166
167 """
168 def inner(self, name, *args, **kwargs):
169 if self._name != name:
170 raise ImportError("loader cannot handle %s" % name)
171 return method(self, name, *args, **kwargs)
172 _wrap(inner, method)
173 return inner
174
175
Brett Cannona113ac52009-03-15 01:41:33 +0000176def _requires_builtin(fxn):
177 """Decorator to verify the named module is built-in."""
178 def wrapper(self, fullname):
179 if fullname not in sys.builtin_module_names:
180 raise ImportError("{0} is not a built-in module".format(fullname))
181 return fxn(self, fullname)
182 _wrap(wrapper, fxn)
183 return wrapper
184
185
Brett Cannon8d110132009-03-15 02:20:16 +0000186def _requires_frozen(fxn):
187 """Decorator to verify the named module is frozen."""
188 def wrapper(self, fullname):
189 if not imp.is_frozen(fullname):
190 raise ImportError("{0} is not a frozen module".format(fullname))
191 return fxn(self, fullname)
192 _wrap(wrapper, fxn)
193 return wrapper
194
195
Brett Cannone9103d22009-03-12 22:37:06 +0000196def _suffix_list(suffix_type):
197 """Return a list of file suffixes based on the imp file type."""
198 return [suffix[0] for suffix in imp.get_suffixes()
199 if suffix[2] == suffix_type]
200
201
202# Loaders #####################################################################
Brett Cannonce43ddf2009-03-12 22:28:55 +0000203
Brett Cannon5abdc932009-01-22 22:43:07 +0000204class BuiltinImporter:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000205
Brett Cannon7aa21f72009-03-15 00:53:05 +0000206 """Meta path import for built-in modules.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000207
Brett Cannon7aa21f72009-03-15 00:53:05 +0000208 All methods are either class or static methods to avoid the need to
209 instantiate the class.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000210
211 """
212
Brett Cannon5abdc932009-01-22 22:43:07 +0000213 @classmethod
214 def find_module(cls, fullname, path=None):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000215 """Find the built-in module.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000216
217 If 'path' is ever specified then the search is considered a failure.
218
219 """
220 if path is not None:
221 return None
Brett Cannon5abdc932009-01-22 22:43:07 +0000222 return cls if imp.is_builtin(fullname) else None
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000223
Brett Cannon78246b62009-01-25 04:56:30 +0000224 @classmethod
Brett Cannon435aad82009-03-04 16:07:00 +0000225 @set_package
Brett Cannon2cf03a82009-03-10 05:17:37 +0000226 @set_loader
Brett Cannona113ac52009-03-15 01:41:33 +0000227 @_requires_builtin
Brett Cannon78246b62009-01-25 04:56:30 +0000228 def load_module(cls, fullname):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000229 """Load a built-in module."""
Brett Cannond2e7b332009-02-17 02:45:03 +0000230 is_reload = fullname in sys.modules
231 try:
232 return imp.init_builtin(fullname)
233 except:
234 if not is_reload and fullname in sys.modules:
235 del sys.modules[fullname]
236 raise
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000237
Brett Cannona113ac52009-03-15 01:41:33 +0000238 @classmethod
239 @_requires_builtin
240 def get_code(cls, fullname):
241 """Return None as built-in modules do not have code objects."""
242 return None
243
244 @classmethod
245 @_requires_builtin
246 def get_source(cls, fullname):
247 """Return None as built-in modules do not have source code."""
248 return None
249
250 @classmethod
251 @_requires_builtin
252 def is_package(cls, fullname):
253 """Return None as built-in module are never packages."""
254 return False
255
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000256
Brett Cannon5abdc932009-01-22 22:43:07 +0000257class FrozenImporter:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000258
Brett Cannon7aa21f72009-03-15 00:53:05 +0000259 """Meta path import for frozen modules.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000260
Brett Cannon7aa21f72009-03-15 00:53:05 +0000261 All methods are either class or static methods to avoid the need to
262 instantiate the class.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000263
Brett Cannon5abdc932009-01-22 22:43:07 +0000264 """
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000265
Brett Cannon5abdc932009-01-22 22:43:07 +0000266 @classmethod
267 def find_module(cls, fullname, path=None):
268 """Find a frozen module."""
269 return cls if imp.is_frozen(fullname) else None
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000270
Brett Cannon5abdc932009-01-22 22:43:07 +0000271 @classmethod
Brett Cannon435aad82009-03-04 16:07:00 +0000272 @set_package
Brett Cannon2cf03a82009-03-10 05:17:37 +0000273 @set_loader
Brett Cannon8d110132009-03-15 02:20:16 +0000274 @_requires_frozen
Brett Cannon5abdc932009-01-22 22:43:07 +0000275 def load_module(cls, fullname):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000276 """Load a frozen module."""
Brett Cannond2e7b332009-02-17 02:45:03 +0000277 is_reload = fullname in sys.modules
278 try:
279 return imp.init_frozen(fullname)
280 except:
281 if not is_reload and fullname in sys.modules:
282 del sys.modules[fullname]
283 raise
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000284
Brett Cannon8d110132009-03-15 02:20:16 +0000285 @classmethod
286 @_requires_frozen
287 def get_code(cls, fullname):
288 """Return the code object for the frozen module."""
289 return imp.get_frozen_object(fullname)
290
291 @classmethod
292 @_requires_frozen
293 def get_source(cls, fullname):
294 """Return None as frozen modules do not have source code."""
295 return None
296
297 @classmethod
298 @_requires_frozen
299 def is_package(cls, fullname):
300 """Return if the frozen module is a package."""
301 return imp.is_frozen_package(fullname)
302
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000303
Brett Cannon91cf8822009-02-21 05:41:15 +0000304class PyLoader:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000305
Brett Cannon7aa21f72009-03-15 00:53:05 +0000306 """Loader base class for Python source code.
Brett Cannon91cf8822009-02-21 05:41:15 +0000307
Brett Cannon7aa21f72009-03-15 00:53:05 +0000308 Subclasses need to implement the methods:
309
310 - source_path
311 - get_data
312 - is_package
Brett Cannon91cf8822009-02-21 05:41:15 +0000313
314 """
315
316 @module_for_loader
317 def load_module(self, module):
318 """Load a source module."""
Brett Cannon1014d422009-03-08 20:53:50 +0000319 return self._load_module(module)
Brett Cannon91cf8822009-02-21 05:41:15 +0000320
321 def _load_module(self, module):
322 """Initialize a module from source."""
323 name = module.__name__
Brett Cannon91cf8822009-02-21 05:41:15 +0000324 code_object = self.get_code(module.__name__)
Brett Cannon1014d422009-03-08 20:53:50 +0000325 # __file__ may have been set by the caller, e.g. bytecode path.
Brett Cannon91cf8822009-02-21 05:41:15 +0000326 if not hasattr(module, '__file__'):
Brett Cannon1014d422009-03-08 20:53:50 +0000327 module.__file__ = self.source_path(name)
Brett Cannon91cf8822009-02-21 05:41:15 +0000328 if self.is_package(name):
329 module.__path__ = [module.__file__.rsplit(path_sep, 1)[0]]
330 module.__package__ = module.__name__
331 if not hasattr(module, '__path__'):
332 module.__package__ = module.__package__.rpartition('.')[0]
Brett Cannon1014d422009-03-08 20:53:50 +0000333 module.__loader__ = self
Brett Cannon91cf8822009-02-21 05:41:15 +0000334 exec(code_object, module.__dict__)
335 return module
336
337 def get_code(self, fullname):
338 """Get a code object from source."""
339 source_path = self.source_path(fullname)
Brett Cannon1014d422009-03-08 20:53:50 +0000340 if source_path is None:
341 message = "a source path must exist to load {0}".format(fullname)
342 raise ImportError(message)
Brett Cannon91cf8822009-02-21 05:41:15 +0000343 source = self.get_data(source_path)
344 # Convert to universal newlines.
345 line_endings = b'\n'
346 for index, c in enumerate(source):
347 if c == ord(b'\n'):
348 break
349 elif c == ord(b'\r'):
350 line_endings = b'\r'
351 try:
352 if source[index+1] == ord(b'\n'):
353 line_endings += b'\n'
354 except IndexError:
355 pass
356 break
357 if line_endings != b'\n':
358 source = source.replace(line_endings, b'\n')
359 return compile(source, source_path, 'exec', dont_inherit=True)
360
Brett Cannond43b30b2009-03-10 03:29:23 +0000361 # Never use in implementing import! Imports code within the method.
362 def get_source(self, fullname):
363 """Return the source code for a module.
364
365 self.source_path() and self.get_data() are used to implement this
366 method.
367
368 """
369 path = self.source_path(fullname)
370 if path is None:
371 return None
372 try:
373 source_bytes = self.get_data(path)
374 except IOError:
375 return ImportError("source not available through get_data()")
376 import io
377 import tokenize
378 encoding = tokenize.detect_encoding(io.BytesIO(source_bytes).readline)
379 return source_bytes.decode(encoding[0])
380
Brett Cannon91cf8822009-02-21 05:41:15 +0000381
382class PyPycLoader(PyLoader):
383
384 """Loader base class for Python source and bytecode.
385
386 Requires implementing the methods needed for PyLoader as well as
Brett Cannon94aaf9e2009-02-21 23:12:24 +0000387 source_mtime, bytecode_path, and write_bytecode.
Brett Cannon91cf8822009-02-21 05:41:15 +0000388
389 """
390
391 @module_for_loader
392 def load_module(self, module):
393 """Load a module from source or bytecode."""
394 name = module.__name__
Brett Cannon2a922ed2009-03-09 03:35:50 +0000395 source_path = self.source_path(name)
396 bytecode_path = self.bytecode_path(name)
Brett Cannon29dff8a2009-03-09 00:14:37 +0000397 # get_code can worry about no viable paths existing.
398 module.__file__ = source_path or bytecode_path
Brett Cannon91cf8822009-02-21 05:41:15 +0000399 return self._load_module(module)
400
401 def get_code(self, fullname):
402 """Get a code object from source or bytecode."""
403 # XXX Care enough to make sure this call does not happen if the magic
404 # number is bad?
405 source_timestamp = self.source_mtime(fullname)
406 # Try to use bytecode if it is available.
407 bytecode_path = self.bytecode_path(fullname)
408 if bytecode_path:
409 data = self.get_data(bytecode_path)
410 magic = data[:4]
411 pyc_timestamp = marshal._r_long(data[4:8])
412 bytecode = data[8:]
413 try:
414 # Verify that the magic number is valid.
415 if imp.get_magic() != magic:
416 raise ImportError("bad magic number")
417 # Verify that the bytecode is not stale (only matters when
418 # there is source to fall back on.
419 if source_timestamp:
420 if pyc_timestamp < source_timestamp:
421 raise ImportError("bytecode is stale")
422 except ImportError:
423 # If source is available give it a shot.
424 if source_timestamp is not None:
425 pass
426 else:
427 raise
428 else:
429 # Bytecode seems fine, so try to use it.
430 # XXX If the bytecode is ill-formed, would it be beneficial to
431 # try for using source if available and issue a warning?
432 return marshal.loads(bytecode)
433 elif source_timestamp is None:
434 raise ImportError("no source or bytecode available to create code "
435 "object for {0!r}".format(fullname))
436 # Use the source.
437 code_object = super().get_code(fullname)
438 # Generate bytecode and write it out.
439 if not sys.dont_write_bytecode:
440 data = bytearray(imp.get_magic())
441 data.extend(marshal._w_long(source_timestamp))
442 data.extend(marshal.dumps(code_object))
443 self.write_bytecode(fullname, data)
444 return code_object
445
446
Brett Cannonf87e04d2009-03-12 22:47:53 +0000447class _PyFileLoader(PyLoader):
Brett Cannon91cf8822009-02-21 05:41:15 +0000448
449 """Load a Python source file."""
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000450
451 def __init__(self, name, path, is_pkg):
452 self._name = name
453 self._is_pkg = is_pkg
454 # Figure out the base path based on whether it was source or bytecode
455 # that was found.
456 try:
457 self._base_path = _path_without_ext(path, imp.PY_SOURCE)
458 except ValueError:
459 self._base_path = _path_without_ext(path, imp.PY_COMPILED)
460
461 def _find_path(self, ext_type):
462 """Find a path from the base path and the specified extension type that
463 exists, returning None if one is not found."""
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000464 for suffix in _suffix_list(ext_type):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000465 path = self._base_path + suffix
466 if _path_exists(path):
467 return path
468 else:
469 return None
470
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000471 @_check_name
Brett Cannon51c50262009-02-01 05:33:17 +0000472 def source_path(self, fullname):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000473 """Return the path to an existing source file for the module, or None
474 if one cannot be found."""
475 # Not a property so that it is easy to override.
476 return self._find_path(imp.PY_SOURCE)
477
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000478 @_check_name
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000479 def get_source(self, fullname):
480 """Return the source for the module as a string.
481
482 Return None if the source is not available. Raise ImportError if the
483 laoder cannot handle the specified module.
484
485 """
Brett Cannon51c50262009-02-01 05:33:17 +0000486 source_path = self._source_path(name)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000487 if source_path is None:
488 return None
489 import tokenize
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000490 with _closing(_io.FileIO(source_path, 'r')) as file: # Assuming bytes.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000491 encoding, lines = tokenize.detect_encoding(file.readline)
492 # XXX Will fail when passed to compile() if the encoding is
493 # anything other than UTF-8.
494 return open(source_path, encoding=encoding).read()
495
Brett Cannon91cf8822009-02-21 05:41:15 +0000496
497 def get_data(self, path):
498 """Return the data from path as raw bytes."""
Brett Cannon7c9875c2009-03-04 01:10:09 +0000499 return _io.FileIO(path, 'r').read() # Assuming bytes.
Brett Cannon91cf8822009-02-21 05:41:15 +0000500
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000501 @_check_name
Brett Cannon91cf8822009-02-21 05:41:15 +0000502 def is_package(self, fullname):
503 """Return a boolean based on whether the module is a package.
504
505 Raises ImportError (like get_source) if the loader cannot handle the
506 package.
507
508 """
509 return self._is_pkg
510
511
Brett Cannonf87e04d2009-03-12 22:47:53 +0000512class _PyPycFileLoader(PyPycLoader, _PyFileLoader):
Brett Cannon91cf8822009-02-21 05:41:15 +0000513
514 """Load a module from a source or bytecode file."""
515
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000516 @_check_name
Brett Cannon94aaf9e2009-02-21 23:12:24 +0000517 def source_mtime(self, name):
518 """Return the modification time of the source for the specified
519 module."""
520 source_path = self.source_path(name)
521 if not source_path:
522 return None
523 return int(_os.stat(source_path).st_mtime)
524
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000525 @_check_name
Brett Cannon91cf8822009-02-21 05:41:15 +0000526 def bytecode_path(self, fullname):
527 """Return the path to a bytecode file, or None if one does not
528 exist."""
529 # Not a property for easy overriding.
530 return self._find_path(imp.PY_COMPILED)
531
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000532 @_check_name
Brett Cannon776e7012009-02-01 06:07:57 +0000533 def write_bytecode(self, name, data):
534 """Write out 'data' for the specified module, returning a boolean
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000535 signifying if the write-out actually occurred.
536
537 Raises ImportError (just like get_source) if the specified module
538 cannot be handled by the loader.
539
540 """
Brett Cannon51c50262009-02-01 05:33:17 +0000541 bytecode_path = self.bytecode_path(name)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000542 if not bytecode_path:
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000543 bytecode_path = self._base_path + _suffix_list(imp.PY_COMPILED)[0]
Brett Cannon7c9875c2009-03-04 01:10:09 +0000544 file = _io.FileIO(bytecode_path, 'w') # Assuming bytes.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000545 try:
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000546 with _closing(file) as bytecode_file:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000547 bytecode_file.write(data)
548 return True
549 except IOError as exc:
550 if exc.errno == errno.EACCES:
551 return False
552 else:
553 raise
554
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000555
Brett Cannone9103d22009-03-12 22:37:06 +0000556class _ExtensionFileLoader:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000557
Brett Cannone9103d22009-03-12 22:37:06 +0000558 """Loader for extension modules.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000559
Brett Cannone9103d22009-03-12 22:37:06 +0000560 The constructor is designed to work with FileFinder.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000561
562 """
563
Brett Cannone9103d22009-03-12 22:37:06 +0000564 def __init__(self, name, path, is_pkg):
565 """Initialize the loader.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000566
Brett Cannone9103d22009-03-12 22:37:06 +0000567 If is_pkg is True then an exception is raised as extension modules
568 cannot be the __init__ module for an extension module.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000569
570 """
Brett Cannone9103d22009-03-12 22:37:06 +0000571 self._name = name
572 self._path = path
573 if is_pkg:
574 raise ValueError("extension modules cannot be packages")
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000575
Brett Cannone9103d22009-03-12 22:37:06 +0000576 @_check_name
577 @set_package
578 @set_loader
579 def load_module(self, fullname):
580 """Load an extension module."""
581 is_reload = fullname in sys.modules
582 try:
583 return imp.load_dynamic(fullname, self._path)
584 except:
585 if not is_reload and fullname in sys.modules:
586 del sys.modules[fullname]
587 raise
588
589 @_check_name
590 def is_package(self, fullname):
591 """Return False as an extension module can never be a package."""
592 return False
593
594 @_check_name
595 def get_code(self, fullname):
596 """Return None as an extension module cannot create a code object."""
597 return None
598
599 @_check_name
600 def get_source(self, fullname):
601 """Return None as extension modules have no source code."""
602 return None
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000603
604
Brett Cannone9103d22009-03-12 22:37:06 +0000605# Finders #####################################################################
Brett Cannon4afab6b2009-02-21 03:31:35 +0000606
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000607class PathFinder:
Brett Cannon1d376682009-02-02 19:19:36 +0000608
609 """Meta path finder for sys.(path|path_hooks|path_importer_cache)."""
610
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000611 @classmethod
Brett Cannon32732e32009-02-15 05:48:13 +0000612 def _path_hooks(cls, path, hooks=None):
613 """Search sequence of hooks for a finder for 'path'.
Brett Cannon1d376682009-02-02 19:19:36 +0000614
Brett Cannon32732e32009-02-15 05:48:13 +0000615 If 'hooks' is false then use sys.path_hooks.
Brett Cannon1d376682009-02-02 19:19:36 +0000616
617 """
Brett Cannon32732e32009-02-15 05:48:13 +0000618 if not hooks:
619 hooks = sys.path_hooks
620 for hook in hooks:
Brett Cannon1d376682009-02-02 19:19:36 +0000621 try:
622 return hook(path)
623 except ImportError:
624 continue
625 else:
Brett Cannon32732e32009-02-15 05:48:13 +0000626 raise ImportError("no path hook found for {0}".format(path))
Brett Cannon1d376682009-02-02 19:19:36 +0000627
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000628 @classmethod
Brett Cannon32732e32009-02-15 05:48:13 +0000629 def _path_importer_cache(cls, path, default=None):
Brett Cannon1d376682009-02-02 19:19:36 +0000630 """Get the finder for the path from sys.path_importer_cache.
631
632 If the path is not in the cache, find the appropriate finder and cache
633 it. If None is cached, get the default finder and cache that
634 (if applicable).
635
636 Because of NullImporter, some finder should be returned. The only
637 explicit fail case is if None is cached but the path cannot be used for
638 the default hook, for which ImportError is raised.
639
640 """
641 try:
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000642 finder = sys.path_importer_cache[path]
Brett Cannon1d376682009-02-02 19:19:36 +0000643 except KeyError:
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000644 finder = cls._path_hooks(path)
Brett Cannon1d376682009-02-02 19:19:36 +0000645 sys.path_importer_cache[path] = finder
646 else:
Brett Cannon32732e32009-02-15 05:48:13 +0000647 if finder is None and default:
Brett Cannon1d376682009-02-02 19:19:36 +0000648 # Raises ImportError on failure.
Brett Cannon32732e32009-02-15 05:48:13 +0000649 finder = default(path)
Brett Cannon1d376682009-02-02 19:19:36 +0000650 sys.path_importer_cache[path] = finder
651 return finder
652
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000653 @classmethod
654 def find_module(cls, fullname, path=None):
Brett Cannon7aa21f72009-03-15 00:53:05 +0000655 """Find the module on sys.path or 'path' based on sys.path_hooks and
656 sys.path_importer_cache."""
Brett Cannon1d376682009-02-02 19:19:36 +0000657 if not path:
658 path = sys.path
659 for entry in path:
660 try:
Brett Cannonf7e5a8c2009-02-05 02:52:18 +0000661 finder = cls._path_importer_cache(entry)
Brett Cannon1d376682009-02-02 19:19:36 +0000662 except ImportError:
663 continue
664 loader = finder.find_module(fullname)
665 if loader:
666 return loader
667 else:
668 return None
669
670
Brett Cannone9103d22009-03-12 22:37:06 +0000671class _ChainedFinder:
672
673 """Finder that sequentially calls other finders."""
674
675 def __init__(self, *finders):
676 self._finders = finders
677
678 def find_module(self, fullname, path=None):
679 for finder in self._finders:
680 result = finder.find_module(fullname, path)
681 if result:
682 return result
683 else:
684 return None
685
686
Brett Cannonf87e04d2009-03-12 22:47:53 +0000687class _FileFinder:
Brett Cannone9103d22009-03-12 22:37:06 +0000688
689 """Base class for file finders.
690
691 Subclasses are expected to define the following attributes:
692
693 * _suffixes
694 Sequence of file suffixes whose order will be followed.
695
696 * _possible_package
697 True if importer should check for packages.
698
699 * _loader
700 A callable that takes the module name, a file path, and whether
701 the path points to a package and returns a loader for the module
702 found at that path.
703
704 """
705
706 def __init__(self, path_entry):
707 """Initialize an importer for the passed-in sys.path entry (which is
708 assumed to have already been verified as an existing directory).
709
710 Can be used as an entry on sys.path_hook.
711
712 """
713 absolute_path = _path_absolute(path_entry)
714 if not _path_isdir(absolute_path):
715 raise ImportError("only directories are supported")
716 self._path_entry = absolute_path
717
718 def find_module(self, fullname, path=None):
719 tail_module = fullname.rpartition('.')[2]
720 package_directory = None
721 if self._possible_package:
722 for ext in self._suffixes:
723 package_directory = _path_join(self._path_entry, tail_module)
724 init_filename = '__init__' + ext
725 package_init = _path_join(package_directory, init_filename)
726 if (_path_isfile(package_init) and
727 _case_ok(self._path_entry, tail_module) and
728 _case_ok(package_directory, init_filename)):
729 return self._loader(fullname, package_init, True)
730 for ext in self._suffixes:
731 file_name = tail_module + ext
732 file_path = _path_join(self._path_entry, file_name)
733 if (_path_isfile(file_path) and
734 _case_ok(self._path_entry, file_name)):
735 return self._loader(fullname, file_path, False)
736 else:
737 # Raise a warning if it matches a directory w/o an __init__ file.
738 if (package_directory is not None and
739 _path_isdir(package_directory) and
740 _case_ok(self._path_entry, tail_module)):
741 _warnings.warn("Not importing directory %s: missing __init__"
742 % package_directory, ImportWarning)
743 return None
744
745
Brett Cannonf87e04d2009-03-12 22:47:53 +0000746class _PyFileFinder(_FileFinder):
Brett Cannone9103d22009-03-12 22:37:06 +0000747
748 """Importer for source/bytecode files."""
749
750 _possible_package = True
Brett Cannonf87e04d2009-03-12 22:47:53 +0000751 _loader = _PyFileLoader
Brett Cannone9103d22009-03-12 22:37:06 +0000752
753 def __init__(self, path_entry):
754 # Lack of imp during class creation means _suffixes is set here.
755 # Make sure that Python source files are listed first! Needed for an
756 # optimization by the loader.
757 self._suffixes = _suffix_list(imp.PY_SOURCE)
758 super().__init__(path_entry)
759
760
Brett Cannonf87e04d2009-03-12 22:47:53 +0000761class _PyPycFileFinder(_PyFileFinder):
Brett Cannone9103d22009-03-12 22:37:06 +0000762
763 """Finder for source and bytecode files."""
764
Brett Cannonf87e04d2009-03-12 22:47:53 +0000765 _loader = _PyPycFileLoader
Brett Cannone9103d22009-03-12 22:37:06 +0000766
767 def __init__(self, path_entry):
768 super().__init__(path_entry)
769 self._suffixes += _suffix_list(imp.PY_COMPILED)
770
771
772
773
Brett Cannonf87e04d2009-03-12 22:47:53 +0000774class _ExtensionFileFinder(_FileFinder):
Brett Cannone9103d22009-03-12 22:37:06 +0000775
776 """Importer for extension files."""
777
778 _possible_package = False
779 _loader = _ExtensionFileLoader
780
781 def __init__(self, path_entry):
782 # Assigning to _suffixes here instead of at the class level because
783 # imp is not imported at the time of class creation.
784 self._suffixes = _suffix_list(imp.C_EXTENSION)
785 super().__init__(path_entry)
786
787
Brett Cannonce43ddf2009-03-12 22:28:55 +0000788# Import itself ###############################################################
789
Brett Cannone9103d22009-03-12 22:37:06 +0000790def _chained_path_hook(*path_hooks):
791 """Create a closure which sequentially checks path hooks to see which ones
792 (if any) can work with a path."""
793 def path_hook(entry):
794 """Check to see if 'entry' matches any of the enclosed path hooks."""
795 finders = []
796 for hook in path_hooks:
797 try:
798 finder = hook(entry)
799 except ImportError:
800 continue
801 else:
802 finders.append(finder)
803 if not finders:
804 raise ImportError("no finder found")
805 else:
806 return _ChainedFinder(*finders)
Brett Cannonce43ddf2009-03-12 22:28:55 +0000807
Brett Cannone9103d22009-03-12 22:37:06 +0000808 return path_hook
Brett Cannonce43ddf2009-03-12 22:28:55 +0000809
810
Brett Cannonf87e04d2009-03-12 22:47:53 +0000811_DEFAULT_PATH_HOOK = _chained_path_hook(_ExtensionFileFinder, _PyPycFileFinder)
Brett Cannon2dee5972009-02-21 03:15:37 +0000812
Brett Cannon32732e32009-02-15 05:48:13 +0000813class _DefaultPathFinder(PathFinder):
814
815 """Subclass of PathFinder that implements implicit semantics for
816 __import__."""
817
Brett Cannon32732e32009-02-15 05:48:13 +0000818 @classmethod
819 def _path_hooks(cls, path):
820 """Search sys.path_hooks as well as implicit path hooks."""
821 try:
822 return super()._path_hooks(path)
823 except ImportError:
Brett Cannon2dee5972009-02-21 03:15:37 +0000824 implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter]
Brett Cannon32732e32009-02-15 05:48:13 +0000825 return super()._path_hooks(path, implicit_hooks)
826
827 @classmethod
828 def _path_importer_cache(cls, path):
829 """Use the default path hook when None is stored in
830 sys.path_importer_cache."""
Brett Cannon2dee5972009-02-21 03:15:37 +0000831 return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK)
Brett Cannon32732e32009-02-15 05:48:13 +0000832
833
Brett Cannone9103d22009-03-12 22:37:06 +0000834class _ImportLockContext:
835
836 """Context manager for the import lock."""
837
838 def __enter__(self):
839 """Acquire the import lock."""
840 imp.acquire_lock()
841
842 def __exit__(self, exc_type, exc_value, exc_traceback):
843 """Release the import lock regardless of any raised exceptions."""
844 imp.release_lock()
845
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000846
Brett Cannon32732e32009-02-15 05:48:13 +0000847_IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, _DefaultPathFinder]
848
Brett Cannon7f9876c2009-02-06 02:47:33 +0000849def _gcd_import(name, package=None, level=0):
850 """Import and return the module based on its name, the package the call is
851 being made from, and the level adjustment.
852
853 This function represents the greatest common denominator of functionality
Brett Cannon2c318a12009-02-07 01:15:27 +0000854 between import_module and __import__. This includes settting __package__ if
855 the loader did not.
856
Brett Cannon7f9876c2009-02-06 02:47:33 +0000857 """
Brett Cannon2c318a12009-02-07 01:15:27 +0000858 if package:
859 if not hasattr(package, 'rindex'):
860 raise ValueError("__package__ not set to a string")
861 elif package not in sys.modules:
862 msg = ("Parent module {0!r} not loaded, cannot perform relative "
863 "import")
864 raise SystemError(msg.format(package))
865 if not name and level == 0:
866 raise ValueError("Empty module name")
Brett Cannon7f9876c2009-02-06 02:47:33 +0000867 if level > 0:
Brett Cannon2c318a12009-02-07 01:15:27 +0000868 dot = len(package)
Brett Cannon7f9876c2009-02-06 02:47:33 +0000869 for x in range(level, 1, -1):
870 try:
871 dot = package.rindex('.', 0, dot)
Brett Cannon7f9876c2009-02-06 02:47:33 +0000872 except ValueError:
Brett Cannon2c318a12009-02-07 01:15:27 +0000873 raise ValueError("attempted relative import beyond "
874 "top-level package")
875 if name:
876 name = "{0}.{1}".format(package[:dot], name)
877 else:
878 name = package[:dot]
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000879 with _ImportLockContext():
Brett Cannon7f9876c2009-02-06 02:47:33 +0000880 try:
881 return sys.modules[name]
882 except KeyError:
883 pass
884 parent = name.rpartition('.')[0]
885 path = None
886 if parent:
887 if parent not in sys.modules:
Brett Cannon2c318a12009-02-07 01:15:27 +0000888 _gcd_import(parent)
889 # Backwards-compatibility; be nicer to skip the dict lookup.
890 parent_module = sys.modules[parent]
Brett Cannon7f9876c2009-02-06 02:47:33 +0000891 path = parent_module.__path__
Brett Cannon32732e32009-02-15 05:48:13 +0000892 meta_path = sys.meta_path + _IMPLICIT_META_PATH
Brett Cannon2c318a12009-02-07 01:15:27 +0000893 for finder in meta_path:
Brett Cannon7f9876c2009-02-06 02:47:33 +0000894 loader = finder.find_module(name, path)
Brett Cannon2c318a12009-02-07 01:15:27 +0000895 if loader is not None:
896 loader.load_module(name)
897 break
Brett Cannon7f9876c2009-02-06 02:47:33 +0000898 else:
899 raise ImportError("No module named {0}".format(name))
Brett Cannon2c318a12009-02-07 01:15:27 +0000900 # Backwards-compatibility; be nicer to skip the dict lookup.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000901 module = sys.modules[name]
902 if parent:
Brett Cannon2c318a12009-02-07 01:15:27 +0000903 # Set the module as an attribute on its parent.
904 setattr(parent_module, name.rpartition('.')[2], module)
905 # Set __package__ if the loader did not.
906 if not hasattr(module, '__package__') or module.__package__ is None:
907 # Watch out for what comes out of sys.modules to not be a module,
908 # e.g. an int.
909 try:
910 module.__package__ = module.__name__
911 if not hasattr(module, '__path__'):
912 module.__package__ = module.__package__.rpartition('.')[0]
913 except AttributeError:
914 pass
915 return module
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000916
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000917
Brett Cannon7aa21f72009-03-15 00:53:05 +0000918def __import__(name, globals={}, locals={}, fromlist=[], level=0):
Brett Cannon2c318a12009-02-07 01:15:27 +0000919 """Import a module.
920
921 The 'globals' argument is used to infer where the import is occuring from
922 to handle relative imports. The 'locals' argument is ignored. The
923 'fromlist' argument specifies what should exist as attributes on the module
924 being imported (e.g. ``from module import <fromlist>``). The 'level'
925 argument represents the package location to import from in a relative
926 import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
927
928 """
929 if level == 0:
930 module = _gcd_import(name)
931 else:
932 # __package__ is not guaranteed to be defined.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000933 try:
Brett Cannon2c318a12009-02-07 01:15:27 +0000934 package = globals['__package__']
935 except KeyError:
936 package = globals['__name__']
937 if '__path__' not in globals:
938 package = package.rpartition('.')[0]
939 module = _gcd_import(name, package, level)
940 # The hell that is fromlist ...
941 if not fromlist:
942 # Return up to the first dot in 'name'. This is complicated by the fact
943 # that 'name' may be relative.
944 if level == 0:
945 return sys.modules[name.partition('.')[0]]
946 elif not name:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000947 return module
Brett Cannon2c318a12009-02-07 01:15:27 +0000948 else:
949 cut_off = len(name) - len(name.partition('.')[0])
950 return sys.modules[module.__name__[:-cut_off]]
951 else:
952 # If a package was imported, try to import stuff from fromlist.
953 if hasattr(module, '__path__'):
954 if '*' in fromlist and hasattr(module, '__all__'):
955 fromlist.remove('*')
956 fromlist.extend(module.__all__)
957 for x in (y for y in fromlist if not hasattr(module,y)):
958 try:
959 _gcd_import('{0}.{1}'.format(module.__name__, x))
960 except ImportError:
961 pass
962 return module