| 1 |
"""A pure Python implementation of import.""" |
| 2 |
__all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload'] |
| 3 |
|
| 4 |
# Bootstrap help ##################################################### |
| 5 |
|
| 6 |
# Until bootstrapping is complete, DO NOT import any modules that attempt |
| 7 |
# to import importlib._bootstrap (directly or indirectly). Since this |
| 8 |
# partially initialised package would be present in sys.modules, those |
| 9 |
# modules would get an uninitialised copy of the source version, instead |
| 10 |
# of a fully initialised version (either the frozen one or the one |
| 11 |
# initialised below if the frozen one is not available). |
| 12 |
import _imp # Just the builtin component, NOT the full Python module |
| 13 |
import sys |
| 14 |
|
| 15 |
try: |
| 16 |
import _frozen_importlib as _bootstrap |
| 17 |
except ImportError: |
| 18 |
from . import _bootstrap |
| 19 |
_bootstrap._setup(sys, _imp) |
| 20 |
else: |
| 21 |
# importlib._bootstrap is the built-in import, ensure we don't create |
| 22 |
# a second copy of the module. |
| 23 |
_bootstrap.__name__ = 'importlib._bootstrap' |
| 24 |
_bootstrap.__package__ = 'importlib' |
| 25 |
try: |
| 26 |
_bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py') |
| 27 |
except NameError: |
| 28 |
# __file__ is not guaranteed to be defined, e.g. if this code gets |
| 29 |
# frozen by a tool like cx_Freeze. |
| 30 |
pass |
| 31 |
sys.modules['importlib._bootstrap'] = _bootstrap |
| 32 |
|
| 33 |
try: |
| 34 |
import _frozen_importlib_external as _bootstrap_external |
| 35 |
except ImportError: |
| 36 |
from . import _bootstrap_external |
| 37 |
_bootstrap_external._set_bootstrap_module(_bootstrap) |
| 38 |
_bootstrap._bootstrap_external = _bootstrap_external |
| 39 |
else: |
| 40 |
_bootstrap_external.__name__ = 'importlib._bootstrap_external' |
| 41 |
_bootstrap_external.__package__ = 'importlib' |
| 42 |
try: |
| 43 |
_bootstrap_external.__file__ = __file__.replace('__init__.py', '_bootstrap_external.py') |
| 44 |
except NameError: |
| 45 |
# __file__ is not guaranteed to be defined, e.g. if this code gets |
| 46 |
# frozen by a tool like cx_Freeze. |
| 47 |
pass |
| 48 |
sys.modules['importlib._bootstrap_external'] = _bootstrap_external |
| 49 |
|
| 50 |
# To simplify imports in test code |
| 51 |
_pack_uint32 = _bootstrap_external._pack_uint32 |
| 52 |
_unpack_uint32 = _bootstrap_external._unpack_uint32 |
| 53 |
|
| 54 |
# Fully bootstrapped at this point, import whatever you like, circular |
| 55 |
# dependencies and startup overhead minimisation permitting :) |
| 56 |
|
| 57 |
|
| 58 |
# Public API ######################################################### |
| 59 |
|
| 60 |
from ._bootstrap import __import__ |
| 61 |
|
| 62 |
|
| 63 |
def invalidate_caches(): |
| 64 |
"""Call the invalidate_caches() method on all meta path finders stored in |
| 65 |
sys.meta_path (where implemented).""" |
| 66 |
for finder in sys.meta_path: |
| 67 |
if hasattr(finder, 'invalidate_caches'): |
| 68 |
finder.invalidate_caches() |
| 69 |
|
| 70 |
|
| 71 |
def import_module(name, package=None): |
| 72 |
"""Import a module. |
| 73 |
|
| 74 |
The 'package' argument is required when performing a relative import. It |
| 75 |
specifies the package to use as the anchor point from which to resolve the |
| 76 |
relative import to an absolute import. |
| 77 |
|
| 78 |
""" |
| 79 |
level = 0 |
| 80 |
if name.startswith('.'): |
| 81 |
if not package: |
| 82 |
raise TypeError("the 'package' argument is required to perform a " |
| 83 |
f"relative import for {name!r}") |
| 84 |
for character in name: |
| 85 |
if character != '.': |
| 86 |
break |
| 87 |
level += 1 |
| 88 |
return _bootstrap._gcd_import(name[level:], package, level) |
| 89 |
|
| 90 |
|
| 91 |
_RELOADING = {} |
| 92 |
|
| 93 |
|
| 94 |
def reload(module): |
| 95 |
"""Reload the module and return it. |
| 96 |
|
| 97 |
The module must have been successfully imported before. |
| 98 |
|
| 99 |
""" |
| 100 |
try: |
| 101 |
name = module.__spec__.name |
| 102 |
except AttributeError: |
| 103 |
try: |
| 104 |
name = module.__name__ |
| 105 |
except AttributeError: |
| 106 |
raise TypeError("reload() argument must be a module") from None |
| 107 |
|
| 108 |
if sys.modules.get(name) is not module: |
| 109 |
raise ImportError(f"module {name} not in sys.modules", name=name) |
| 110 |
if name in _RELOADING: |
| 111 |
return _RELOADING[name] |
| 112 |
_RELOADING[name] = module |
| 113 |
try: |
| 114 |
parent_name = name.rpartition('.')[0] |
| 115 |
if parent_name: |
| 116 |
try: |
| 117 |
parent = sys.modules[parent_name] |
| 118 |
except KeyError: |
| 119 |
raise ImportError(f"parent {parent_name!r} not in sys.modules", |
| 120 |
name=parent_name) from None |
| 121 |
else: |
| 122 |
pkgpath = parent.__path__ |
| 123 |
else: |
| 124 |
pkgpath = None |
| 125 |
target = module |
| 126 |
spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target) |
| 127 |
if spec is None: |
| 128 |
raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name) |
| 129 |
_bootstrap._exec(spec, module) |
| 130 |
# The module may have replaced itself in sys.modules! |
| 131 |
return sys.modules[name] |
| 132 |
finally: |
| 133 |
try: |
| 134 |
del _RELOADING[name] |
| 135 |
except KeyError: |
| 136 |
pass |