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 import warnings
58
59
60 # Public API #########################################################
61
62 from ._bootstrap import __import__
63
64
65 def invalidate_caches():
66 """Call the invalidate_caches() method on all meta path finders stored in
67 sys.meta_path (where implemented)."""
68 for finder in sys.meta_path:
69 if hasattr(finder, 'invalidate_caches'):
70 finder.invalidate_caches()
71
72
73 def import_module(name, package=None):
74 """Import a module.
75
76 The 'package' argument is required when performing a relative import. It
77 specifies the package to use as the anchor point from which to resolve the
78 relative import to an absolute import.
79
80 """
81 level = 0
82 if name.startswith('.'):
83 if not package:
84 raise TypeError("the 'package' argument is required to perform a "
85 f"relative import for {name!r}")
86 for character in name:
87 if character != '.':
88 break
89 level += 1
90 return _bootstrap._gcd_import(name[level:], package, level)
91
92
93 _RELOADING = {}
94
95
96 def reload(module):
97 """Reload the module and return it.
98
99 The module must have been successfully imported before.
100
101 """
102 try:
103 name = module.__spec__.name
104 except AttributeError:
105 try:
106 name = module.__name__
107 except AttributeError:
108 raise TypeError("reload() argument must be a module")
109
110 if sys.modules.get(name) is not module:
111 raise ImportError(f"module {name} not in sys.modules", name=name)
112 if name in _RELOADING:
113 return _RELOADING[name]
114 _RELOADING[name] = module
115 try:
116 parent_name = name.rpartition('.')[0]
117 if parent_name:
118 try:
119 parent = sys.modules[parent_name]
120 except KeyError:
121 raise ImportError(f"parent {parent_name!r} not in sys.modules",
122 name=parent_name) from None
123 else:
124 pkgpath = parent.__path__
125 else:
126 pkgpath = None
127 target = module
128 spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
129 if spec is None:
130 raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
131 _bootstrap._exec(spec, module)
132 # The module may have replaced itself in sys.modules!
133 return sys.modules[name]
134 finally:
135 try:
136 del _RELOADING[name]
137 except KeyError:
138 pass