blob: 949a612338fb5f4a0819cb3a4721275e0332f748 [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
Brett Cannon8593a752009-03-30 19:57:15 +0000664 if finder:
665 loader = finder.find_module(fullname)
666 if loader:
667 return loader
Brett Cannon1d376682009-02-02 19:19:36 +0000668 else:
669 return None
670
671
Brett Cannone9103d22009-03-12 22:37:06 +0000672class _ChainedFinder:
673
674 """Finder that sequentially calls other finders."""
675
676 def __init__(self, *finders):
677 self._finders = finders
678
679 def find_module(self, fullname, path=None):
680 for finder in self._finders:
681 result = finder.find_module(fullname, path)
682 if result:
683 return result
684 else:
685 return None
686
687
Brett Cannonf87e04d2009-03-12 22:47:53 +0000688class _FileFinder:
Brett Cannone9103d22009-03-12 22:37:06 +0000689
690 """Base class for file finders.
691
692 Subclasses are expected to define the following attributes:
693
694 * _suffixes
695 Sequence of file suffixes whose order will be followed.
696
697 * _possible_package
698 True if importer should check for packages.
699
700 * _loader
701 A callable that takes the module name, a file path, and whether
702 the path points to a package and returns a loader for the module
703 found at that path.
704
705 """
706
707 def __init__(self, path_entry):
708 """Initialize an importer for the passed-in sys.path entry (which is
709 assumed to have already been verified as an existing directory).
710
711 Can be used as an entry on sys.path_hook.
712
713 """
714 absolute_path = _path_absolute(path_entry)
715 if not _path_isdir(absolute_path):
716 raise ImportError("only directories are supported")
717 self._path_entry = absolute_path
718
719 def find_module(self, fullname, path=None):
720 tail_module = fullname.rpartition('.')[2]
721 package_directory = None
722 if self._possible_package:
723 for ext in self._suffixes:
724 package_directory = _path_join(self._path_entry, tail_module)
725 init_filename = '__init__' + ext
726 package_init = _path_join(package_directory, init_filename)
727 if (_path_isfile(package_init) and
728 _case_ok(self._path_entry, tail_module) and
729 _case_ok(package_directory, init_filename)):
730 return self._loader(fullname, package_init, True)
731 for ext in self._suffixes:
732 file_name = tail_module + ext
733 file_path = _path_join(self._path_entry, file_name)
734 if (_path_isfile(file_path) and
735 _case_ok(self._path_entry, file_name)):
736 return self._loader(fullname, file_path, False)
737 else:
738 # Raise a warning if it matches a directory w/o an __init__ file.
739 if (package_directory is not None and
740 _path_isdir(package_directory) and
741 _case_ok(self._path_entry, tail_module)):
742 _warnings.warn("Not importing directory %s: missing __init__"
743 % package_directory, ImportWarning)
744 return None
745
746
Brett Cannonf87e04d2009-03-12 22:47:53 +0000747class _PyFileFinder(_FileFinder):
Brett Cannone9103d22009-03-12 22:37:06 +0000748
749 """Importer for source/bytecode files."""
750
751 _possible_package = True
Brett Cannonf87e04d2009-03-12 22:47:53 +0000752 _loader = _PyFileLoader
Brett Cannone9103d22009-03-12 22:37:06 +0000753
754 def __init__(self, path_entry):
755 # Lack of imp during class creation means _suffixes is set here.
756 # Make sure that Python source files are listed first! Needed for an
757 # optimization by the loader.
758 self._suffixes = _suffix_list(imp.PY_SOURCE)
759 super().__init__(path_entry)
760
761
Brett Cannonf87e04d2009-03-12 22:47:53 +0000762class _PyPycFileFinder(_PyFileFinder):
Brett Cannone9103d22009-03-12 22:37:06 +0000763
764 """Finder for source and bytecode files."""
765
Brett Cannonf87e04d2009-03-12 22:47:53 +0000766 _loader = _PyPycFileLoader
Brett Cannone9103d22009-03-12 22:37:06 +0000767
768 def __init__(self, path_entry):
769 super().__init__(path_entry)
770 self._suffixes += _suffix_list(imp.PY_COMPILED)
771
772
773
774
Brett Cannonf87e04d2009-03-12 22:47:53 +0000775class _ExtensionFileFinder(_FileFinder):
Brett Cannone9103d22009-03-12 22:37:06 +0000776
777 """Importer for extension files."""
778
779 _possible_package = False
780 _loader = _ExtensionFileLoader
781
782 def __init__(self, path_entry):
783 # Assigning to _suffixes here instead of at the class level because
784 # imp is not imported at the time of class creation.
785 self._suffixes = _suffix_list(imp.C_EXTENSION)
786 super().__init__(path_entry)
787
788
Brett Cannonce43ddf2009-03-12 22:28:55 +0000789# Import itself ###############################################################
790
Brett Cannone9103d22009-03-12 22:37:06 +0000791def _chained_path_hook(*path_hooks):
792 """Create a closure which sequentially checks path hooks to see which ones
793 (if any) can work with a path."""
794 def path_hook(entry):
795 """Check to see if 'entry' matches any of the enclosed path hooks."""
796 finders = []
797 for hook in path_hooks:
798 try:
799 finder = hook(entry)
800 except ImportError:
801 continue
802 else:
803 finders.append(finder)
804 if not finders:
805 raise ImportError("no finder found")
806 else:
807 return _ChainedFinder(*finders)
Brett Cannonce43ddf2009-03-12 22:28:55 +0000808
Brett Cannone9103d22009-03-12 22:37:06 +0000809 return path_hook
Brett Cannonce43ddf2009-03-12 22:28:55 +0000810
811
Brett Cannonf87e04d2009-03-12 22:47:53 +0000812_DEFAULT_PATH_HOOK = _chained_path_hook(_ExtensionFileFinder, _PyPycFileFinder)
Brett Cannon2dee5972009-02-21 03:15:37 +0000813
Brett Cannon32732e32009-02-15 05:48:13 +0000814class _DefaultPathFinder(PathFinder):
815
816 """Subclass of PathFinder that implements implicit semantics for
817 __import__."""
818
Brett Cannon32732e32009-02-15 05:48:13 +0000819 @classmethod
820 def _path_hooks(cls, path):
821 """Search sys.path_hooks as well as implicit path hooks."""
822 try:
823 return super()._path_hooks(path)
824 except ImportError:
Brett Cannon2dee5972009-02-21 03:15:37 +0000825 implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter]
Brett Cannon32732e32009-02-15 05:48:13 +0000826 return super()._path_hooks(path, implicit_hooks)
827
828 @classmethod
829 def _path_importer_cache(cls, path):
830 """Use the default path hook when None is stored in
831 sys.path_importer_cache."""
Brett Cannon2dee5972009-02-21 03:15:37 +0000832 return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK)
Brett Cannon32732e32009-02-15 05:48:13 +0000833
834
Brett Cannone9103d22009-03-12 22:37:06 +0000835class _ImportLockContext:
836
837 """Context manager for the import lock."""
838
839 def __enter__(self):
840 """Acquire the import lock."""
841 imp.acquire_lock()
842
843 def __exit__(self, exc_type, exc_value, exc_traceback):
844 """Release the import lock regardless of any raised exceptions."""
845 imp.release_lock()
846
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000847
Brett Cannon32732e32009-02-15 05:48:13 +0000848_IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, _DefaultPathFinder]
849
Brett Cannon7f9876c2009-02-06 02:47:33 +0000850def _gcd_import(name, package=None, level=0):
851 """Import and return the module based on its name, the package the call is
852 being made from, and the level adjustment.
853
854 This function represents the greatest common denominator of functionality
Brett Cannon2c318a12009-02-07 01:15:27 +0000855 between import_module and __import__. This includes settting __package__ if
856 the loader did not.
857
Brett Cannon7f9876c2009-02-06 02:47:33 +0000858 """
Brett Cannon2c318a12009-02-07 01:15:27 +0000859 if package:
860 if not hasattr(package, 'rindex'):
861 raise ValueError("__package__ not set to a string")
862 elif package not in sys.modules:
863 msg = ("Parent module {0!r} not loaded, cannot perform relative "
864 "import")
865 raise SystemError(msg.format(package))
866 if not name and level == 0:
867 raise ValueError("Empty module name")
Brett Cannon7f9876c2009-02-06 02:47:33 +0000868 if level > 0:
Brett Cannon2c318a12009-02-07 01:15:27 +0000869 dot = len(package)
Brett Cannon7f9876c2009-02-06 02:47:33 +0000870 for x in range(level, 1, -1):
871 try:
872 dot = package.rindex('.', 0, dot)
Brett Cannon7f9876c2009-02-06 02:47:33 +0000873 except ValueError:
Brett Cannon2c318a12009-02-07 01:15:27 +0000874 raise ValueError("attempted relative import beyond "
875 "top-level package")
876 if name:
877 name = "{0}.{1}".format(package[:dot], name)
878 else:
879 name = package[:dot]
Brett Cannon3eeaa0a2009-03-12 22:07:17 +0000880 with _ImportLockContext():
Brett Cannon7f9876c2009-02-06 02:47:33 +0000881 try:
882 return sys.modules[name]
883 except KeyError:
884 pass
885 parent = name.rpartition('.')[0]
886 path = None
887 if parent:
888 if parent not in sys.modules:
Brett Cannon2c318a12009-02-07 01:15:27 +0000889 _gcd_import(parent)
890 # Backwards-compatibility; be nicer to skip the dict lookup.
891 parent_module = sys.modules[parent]
Brett Cannon7f9876c2009-02-06 02:47:33 +0000892 path = parent_module.__path__
Brett Cannon32732e32009-02-15 05:48:13 +0000893 meta_path = sys.meta_path + _IMPLICIT_META_PATH
Brett Cannon2c318a12009-02-07 01:15:27 +0000894 for finder in meta_path:
Brett Cannon7f9876c2009-02-06 02:47:33 +0000895 loader = finder.find_module(name, path)
Brett Cannon2c318a12009-02-07 01:15:27 +0000896 if loader is not None:
897 loader.load_module(name)
898 break
Brett Cannon7f9876c2009-02-06 02:47:33 +0000899 else:
900 raise ImportError("No module named {0}".format(name))
Brett Cannon2c318a12009-02-07 01:15:27 +0000901 # Backwards-compatibility; be nicer to skip the dict lookup.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000902 module = sys.modules[name]
903 if parent:
Brett Cannon2c318a12009-02-07 01:15:27 +0000904 # Set the module as an attribute on its parent.
905 setattr(parent_module, name.rpartition('.')[2], module)
906 # Set __package__ if the loader did not.
907 if not hasattr(module, '__package__') or module.__package__ is None:
908 # Watch out for what comes out of sys.modules to not be a module,
909 # e.g. an int.
910 try:
911 module.__package__ = module.__name__
912 if not hasattr(module, '__path__'):
913 module.__package__ = module.__package__.rpartition('.')[0]
914 except AttributeError:
915 pass
916 return module
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000917
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000918
Brett Cannon7aa21f72009-03-15 00:53:05 +0000919def __import__(name, globals={}, locals={}, fromlist=[], level=0):
Brett Cannon2c318a12009-02-07 01:15:27 +0000920 """Import a module.
921
922 The 'globals' argument is used to infer where the import is occuring from
923 to handle relative imports. The 'locals' argument is ignored. The
924 'fromlist' argument specifies what should exist as attributes on the module
925 being imported (e.g. ``from module import <fromlist>``). The 'level'
926 argument represents the package location to import from in a relative
927 import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
928
929 """
930 if level == 0:
931 module = _gcd_import(name)
932 else:
933 # __package__ is not guaranteed to be defined.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000934 try:
Brett Cannon2c318a12009-02-07 01:15:27 +0000935 package = globals['__package__']
936 except KeyError:
937 package = globals['__name__']
938 if '__path__' not in globals:
939 package = package.rpartition('.')[0]
940 module = _gcd_import(name, package, level)
941 # The hell that is fromlist ...
942 if not fromlist:
943 # Return up to the first dot in 'name'. This is complicated by the fact
944 # that 'name' may be relative.
945 if level == 0:
946 return sys.modules[name.partition('.')[0]]
947 elif not name:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000948 return module
Brett Cannon2c318a12009-02-07 01:15:27 +0000949 else:
950 cut_off = len(name) - len(name.partition('.')[0])
951 return sys.modules[module.__name__[:-cut_off]]
952 else:
953 # If a package was imported, try to import stuff from fromlist.
954 if hasattr(module, '__path__'):
955 if '*' in fromlist and hasattr(module, '__all__'):
956 fromlist.remove('*')
957 fromlist.extend(module.__all__)
958 for x in (y for y in fromlist if not hasattr(module,y)):
959 try:
960 _gcd_import('{0}.{1}'.format(module.__name__, x))
961 except ImportError:
962 pass
963 return module