2012-02-27 19:15:42 -04:00
|
|
|
"""A pure Python implementation of import."""
|
2012-02-19 20:48:16 -04:00
|
|
|
__all__ = ['__import__', 'import_module', 'invalidate_caches']
|
2009-03-12 19:01:40 -03:00
|
|
|
|
2012-06-17 17:33:38 -03:00
|
|
|
# Bootstrap help #####################################################
|
2012-07-20 10:40:09 -03:00
|
|
|
|
|
|
|
# Until bootstrapping is complete, DO NOT import any modules that attempt
|
|
|
|
# to import importlib._bootstrap (directly or indirectly). Since this
|
|
|
|
# partially initialised package would be present in sys.modules, those
|
|
|
|
# modules would get an uninitialised copy of the source version, instead
|
|
|
|
# of a fully initialised version (either the frozen one or the one
|
|
|
|
# initialised below if the frozen one is not available).
|
|
|
|
import _imp # Just the builtin component, NOT the full Python module
|
2012-06-17 17:33:38 -03:00
|
|
|
import sys
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2012-06-17 17:33:38 -03:00
|
|
|
try:
|
2012-07-04 15:03:40 -03:00
|
|
|
import _frozen_importlib as _bootstrap
|
|
|
|
except ImportError:
|
2012-06-17 17:33:38 -03:00
|
|
|
from . import _bootstrap
|
2012-07-20 10:40:09 -03:00
|
|
|
_bootstrap._setup(sys, _imp)
|
2012-06-17 17:33:38 -03:00
|
|
|
else:
|
|
|
|
# importlib._bootstrap is the built-in import, ensure we don't create
|
|
|
|
# a second copy of the module.
|
|
|
|
_bootstrap.__name__ = 'importlib._bootstrap'
|
|
|
|
_bootstrap.__package__ = 'importlib'
|
|
|
|
_bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py')
|
|
|
|
sys.modules['importlib._bootstrap'] = _bootstrap
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2012-01-25 19:58:03 -04:00
|
|
|
# To simplify imports in test code
|
|
|
|
_w_long = _bootstrap._w_long
|
|
|
|
_r_long = _bootstrap._r_long
|
|
|
|
|
2012-07-20 10:40:09 -03:00
|
|
|
# Fully bootstrapped at this point, import whatever you like, circular
|
|
|
|
# dependencies and startup overhead minimisation permitting :)
|
2012-01-25 19:58:03 -04:00
|
|
|
|
2009-01-22 18:44:04 -04:00
|
|
|
# Public API #########################################################
|
|
|
|
|
2012-02-27 19:15:42 -04:00
|
|
|
from ._bootstrap import __import__
|
|
|
|
|
|
|
|
|
|
|
|
def invalidate_caches():
|
2012-08-10 13:21:12 -03:00
|
|
|
"""Call the invalidate_caches() method on all meta path finders stored in
|
|
|
|
sys.meta_path (where implemented)."""
|
|
|
|
for finder in sys.meta_path:
|
2012-02-27 19:15:42 -04:00
|
|
|
if hasattr(finder, 'invalidate_caches'):
|
|
|
|
finder.invalidate_caches()
|
2009-01-19 22:21:27 -04:00
|
|
|
|
|
|
|
|
2012-05-12 18:43:17 -03:00
|
|
|
def find_loader(name, path=None):
|
|
|
|
"""Find the loader for the specified module.
|
|
|
|
|
|
|
|
First, sys.modules is checked to see if the module was already imported. If
|
|
|
|
so, then sys.modules[name].__loader__ is returned. If that happens to be
|
|
|
|
set to None, then ValueError is raised. If the module is not in
|
|
|
|
sys.modules, then sys.meta_path is searched for a suitable loader with the
|
|
|
|
value of 'path' given to the finders. None is returned if no loader could
|
|
|
|
be found.
|
|
|
|
|
2012-11-17 10:30:55 -04:00
|
|
|
Dotted names do not have their parent packages implicitly imported. You will
|
|
|
|
most likely need to explicitly import all parent packages in the proper
|
|
|
|
order for a submodule to get the correct loader.
|
2012-05-12 18:43:17 -03:00
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
loader = sys.modules[name].__loader__
|
|
|
|
if loader is None:
|
|
|
|
raise ValueError('{}.__loader__ is None'.format(name))
|
|
|
|
else:
|
|
|
|
return loader
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2013-03-13 15:09:08 -03:00
|
|
|
except AttributeError:
|
|
|
|
raise ValueError('{}.__loader__ is not set'.format(name))
|
2012-05-12 18:43:17 -03:00
|
|
|
return _bootstrap._find_module(name, path)
|
|
|
|
|
|
|
|
|
2009-01-19 22:21:27 -04:00
|
|
|
def import_module(name, package=None):
|
|
|
|
"""Import a module.
|
|
|
|
|
|
|
|
The 'package' argument is required when performing a relative import. It
|
|
|
|
specifies the package to use as the anchor point from which to resolve the
|
|
|
|
relative import to an absolute import.
|
|
|
|
|
|
|
|
"""
|
2009-02-06 21:15:27 -04:00
|
|
|
level = 0
|
2009-01-19 22:21:27 -04:00
|
|
|
if name.startswith('.'):
|
|
|
|
if not package:
|
|
|
|
raise TypeError("relative imports require the 'package' argument")
|
|
|
|
for character in name:
|
|
|
|
if character != '.':
|
|
|
|
break
|
|
|
|
level += 1
|
2009-02-06 21:15:27 -04:00
|
|
|
return _bootstrap._gcd_import(name[level:], package, level)
|