1 | n/a | """A pure Python implementation of import.""" |
---|
2 | n/a | __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload'] |
---|
3 | n/a | |
---|
4 | n/a | # Bootstrap help ##################################################### |
---|
5 | n/a | |
---|
6 | n/a | # Until bootstrapping is complete, DO NOT import any modules that attempt |
---|
7 | n/a | # to import importlib._bootstrap (directly or indirectly). Since this |
---|
8 | n/a | # partially initialised package would be present in sys.modules, those |
---|
9 | n/a | # modules would get an uninitialised copy of the source version, instead |
---|
10 | n/a | # of a fully initialised version (either the frozen one or the one |
---|
11 | n/a | # initialised below if the frozen one is not available). |
---|
12 | n/a | import _imp # Just the builtin component, NOT the full Python module |
---|
13 | n/a | import sys |
---|
14 | n/a | |
---|
15 | n/a | try: |
---|
16 | n/a | import _frozen_importlib as _bootstrap |
---|
17 | n/a | except ImportError: |
---|
18 | n/a | from . import _bootstrap |
---|
19 | n/a | _bootstrap._setup(sys, _imp) |
---|
20 | n/a | else: |
---|
21 | n/a | # importlib._bootstrap is the built-in import, ensure we don't create |
---|
22 | n/a | # a second copy of the module. |
---|
23 | n/a | _bootstrap.__name__ = 'importlib._bootstrap' |
---|
24 | n/a | _bootstrap.__package__ = 'importlib' |
---|
25 | n/a | try: |
---|
26 | n/a | _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py') |
---|
27 | n/a | except NameError: |
---|
28 | n/a | # __file__ is not guaranteed to be defined, e.g. if this code gets |
---|
29 | n/a | # frozen by a tool like cx_Freeze. |
---|
30 | n/a | pass |
---|
31 | n/a | sys.modules['importlib._bootstrap'] = _bootstrap |
---|
32 | n/a | |
---|
33 | n/a | try: |
---|
34 | n/a | import _frozen_importlib_external as _bootstrap_external |
---|
35 | n/a | except ImportError: |
---|
36 | n/a | from . import _bootstrap_external |
---|
37 | n/a | _bootstrap_external._setup(_bootstrap) |
---|
38 | n/a | _bootstrap._bootstrap_external = _bootstrap_external |
---|
39 | n/a | else: |
---|
40 | n/a | _bootstrap_external.__name__ = 'importlib._bootstrap_external' |
---|
41 | n/a | _bootstrap_external.__package__ = 'importlib' |
---|
42 | n/a | try: |
---|
43 | n/a | _bootstrap_external.__file__ = __file__.replace('__init__.py', '_bootstrap_external.py') |
---|
44 | n/a | except NameError: |
---|
45 | n/a | # __file__ is not guaranteed to be defined, e.g. if this code gets |
---|
46 | n/a | # frozen by a tool like cx_Freeze. |
---|
47 | n/a | pass |
---|
48 | n/a | sys.modules['importlib._bootstrap_external'] = _bootstrap_external |
---|
49 | n/a | |
---|
50 | n/a | # To simplify imports in test code |
---|
51 | n/a | _w_long = _bootstrap_external._w_long |
---|
52 | n/a | _r_long = _bootstrap_external._r_long |
---|
53 | n/a | |
---|
54 | n/a | # Fully bootstrapped at this point, import whatever you like, circular |
---|
55 | n/a | # dependencies and startup overhead minimisation permitting :) |
---|
56 | n/a | |
---|
57 | n/a | import types |
---|
58 | n/a | import warnings |
---|
59 | n/a | |
---|
60 | n/a | |
---|
61 | n/a | # Public API ######################################################### |
---|
62 | n/a | |
---|
63 | n/a | from ._bootstrap import __import__ |
---|
64 | n/a | |
---|
65 | n/a | |
---|
66 | n/a | def invalidate_caches(): |
---|
67 | n/a | """Call the invalidate_caches() method on all meta path finders stored in |
---|
68 | n/a | sys.meta_path (where implemented).""" |
---|
69 | n/a | for finder in sys.meta_path: |
---|
70 | n/a | if hasattr(finder, 'invalidate_caches'): |
---|
71 | n/a | finder.invalidate_caches() |
---|
72 | n/a | |
---|
73 | n/a | |
---|
74 | n/a | def find_loader(name, path=None): |
---|
75 | n/a | """Return the loader for the specified module. |
---|
76 | n/a | |
---|
77 | n/a | This is a backward-compatible wrapper around find_spec(). |
---|
78 | n/a | |
---|
79 | n/a | This function is deprecated in favor of importlib.util.find_spec(). |
---|
80 | n/a | |
---|
81 | n/a | """ |
---|
82 | n/a | warnings.warn('Use importlib.util.find_spec() instead.', |
---|
83 | n/a | DeprecationWarning, stacklevel=2) |
---|
84 | n/a | try: |
---|
85 | n/a | loader = sys.modules[name].__loader__ |
---|
86 | n/a | if loader is None: |
---|
87 | n/a | raise ValueError('{}.__loader__ is None'.format(name)) |
---|
88 | n/a | else: |
---|
89 | n/a | return loader |
---|
90 | n/a | except KeyError: |
---|
91 | n/a | pass |
---|
92 | n/a | except AttributeError: |
---|
93 | n/a | raise ValueError('{}.__loader__ is not set'.format(name)) from None |
---|
94 | n/a | |
---|
95 | n/a | spec = _bootstrap._find_spec(name, path) |
---|
96 | n/a | # We won't worry about malformed specs (missing attributes). |
---|
97 | n/a | if spec is None: |
---|
98 | n/a | return None |
---|
99 | n/a | if spec.loader is None: |
---|
100 | n/a | if spec.submodule_search_locations is None: |
---|
101 | n/a | raise ImportError('spec for {} missing loader'.format(name), |
---|
102 | n/a | name=name) |
---|
103 | n/a | raise ImportError('namespace packages do not have loaders', |
---|
104 | n/a | name=name) |
---|
105 | n/a | return spec.loader |
---|
106 | n/a | |
---|
107 | n/a | |
---|
108 | n/a | def import_module(name, package=None): |
---|
109 | n/a | """Import a module. |
---|
110 | n/a | |
---|
111 | n/a | The 'package' argument is required when performing a relative import. It |
---|
112 | n/a | specifies the package to use as the anchor point from which to resolve the |
---|
113 | n/a | relative import to an absolute import. |
---|
114 | n/a | |
---|
115 | n/a | """ |
---|
116 | n/a | level = 0 |
---|
117 | n/a | if name.startswith('.'): |
---|
118 | n/a | if not package: |
---|
119 | n/a | msg = ("the 'package' argument is required to perform a relative " |
---|
120 | n/a | "import for {!r}") |
---|
121 | n/a | raise TypeError(msg.format(name)) |
---|
122 | n/a | for character in name: |
---|
123 | n/a | if character != '.': |
---|
124 | n/a | break |
---|
125 | n/a | level += 1 |
---|
126 | n/a | return _bootstrap._gcd_import(name[level:], package, level) |
---|
127 | n/a | |
---|
128 | n/a | |
---|
129 | n/a | _RELOADING = {} |
---|
130 | n/a | |
---|
131 | n/a | |
---|
132 | n/a | def reload(module): |
---|
133 | n/a | """Reload the module and return it. |
---|
134 | n/a | |
---|
135 | n/a | The module must have been successfully imported before. |
---|
136 | n/a | |
---|
137 | n/a | """ |
---|
138 | n/a | if not module or not isinstance(module, types.ModuleType): |
---|
139 | n/a | raise TypeError("reload() argument must be module") |
---|
140 | n/a | try: |
---|
141 | n/a | name = module.__spec__.name |
---|
142 | n/a | except AttributeError: |
---|
143 | n/a | name = module.__name__ |
---|
144 | n/a | |
---|
145 | n/a | if sys.modules.get(name) is not module: |
---|
146 | n/a | msg = "module {} not in sys.modules" |
---|
147 | n/a | raise ImportError(msg.format(name), name=name) |
---|
148 | n/a | if name in _RELOADING: |
---|
149 | n/a | return _RELOADING[name] |
---|
150 | n/a | _RELOADING[name] = module |
---|
151 | n/a | try: |
---|
152 | n/a | parent_name = name.rpartition('.')[0] |
---|
153 | n/a | if parent_name: |
---|
154 | n/a | try: |
---|
155 | n/a | parent = sys.modules[parent_name] |
---|
156 | n/a | except KeyError: |
---|
157 | n/a | msg = "parent {!r} not in sys.modules" |
---|
158 | n/a | raise ImportError(msg.format(parent_name), |
---|
159 | n/a | name=parent_name) from None |
---|
160 | n/a | else: |
---|
161 | n/a | pkgpath = parent.__path__ |
---|
162 | n/a | else: |
---|
163 | n/a | pkgpath = None |
---|
164 | n/a | target = module |
---|
165 | n/a | spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target) |
---|
166 | n/a | _bootstrap._exec(spec, module) |
---|
167 | n/a | # The module may have replaced itself in sys.modules! |
---|
168 | n/a | return sys.modules[name] |
---|
169 | n/a | finally: |
---|
170 | n/a | try: |
---|
171 | n/a | del _RELOADING[name] |
---|
172 | n/a | except KeyError: |
---|
173 | n/a | pass |
---|