|
11 | 11 | from ._bootstrap_external import source_from_cache |
12 | 12 | from ._bootstrap_external import spec_from_file_location |
13 | 13 |
|
14 | | -from contextlib import contextmanager |
15 | 14 | import _imp |
16 | | -import functools |
17 | 15 | import sys |
18 | 16 | import types |
19 | | -import warnings |
20 | 17 |
|
21 | 18 |
|
22 | 19 | def source_hash(source_bytes): |
@@ -115,90 +112,6 @@ def find_spec(name, package=None): |
115 | 112 | return spec |
116 | 113 |
|
117 | 114 |
|
118 | | -@contextmanager |
119 | | -def _module_to_load(name): |
120 | | - is_reload = name in sys.modules |
121 | | - |
122 | | - module = sys.modules.get(name) |
123 | | - if not is_reload: |
124 | | - # This must be done before open() is called as the 'io' module |
125 | | - # implicitly imports 'locale' and would otherwise trigger an |
126 | | - # infinite loop. |
127 | | - module = type(sys)(name) |
128 | | - # This must be done before putting the module in sys.modules |
129 | | - # (otherwise an optimization shortcut in import.c becomes wrong) |
130 | | - module.__initializing__ = True |
131 | | - sys.modules[name] = module |
132 | | - try: |
133 | | - yield module |
134 | | - except Exception: |
135 | | - if not is_reload: |
136 | | - try: |
137 | | - del sys.modules[name] |
138 | | - except KeyError: |
139 | | - pass |
140 | | - finally: |
141 | | - module.__initializing__ = False |
142 | | - |
143 | | - |
144 | | -def set_loader(fxn): |
145 | | - """Set __loader__ on the returned module. |
146 | | -
|
147 | | - This function is deprecated. |
148 | | -
|
149 | | - """ |
150 | | - @functools.wraps(fxn) |
151 | | - def set_loader_wrapper(self, *args, **kwargs): |
152 | | - warnings.warn('The import system now takes care of this automatically; ' |
153 | | - 'this decorator is slated for removal in Python 3.12', |
154 | | - DeprecationWarning, stacklevel=2) |
155 | | - module = fxn(self, *args, **kwargs) |
156 | | - if getattr(module, '__loader__', None) is None: |
157 | | - module.__loader__ = self |
158 | | - return module |
159 | | - return set_loader_wrapper |
160 | | - |
161 | | - |
162 | | -def module_for_loader(fxn): |
163 | | - """Decorator to handle selecting the proper module for loaders. |
164 | | -
|
165 | | - The decorated function is passed the module to use instead of the module |
166 | | - name. The module passed in to the function is either from sys.modules if |
167 | | - it already exists or is a new module. If the module is new, then __name__ |
168 | | - is set the first argument to the method, __loader__ is set to self, and |
169 | | - __package__ is set accordingly (if self.is_package() is defined) will be set |
170 | | - before it is passed to the decorated function (if self.is_package() does |
171 | | - not work for the module it will be set post-load). |
172 | | -
|
173 | | - If an exception is raised and the decorator created the module it is |
174 | | - subsequently removed from sys.modules. |
175 | | -
|
176 | | - The decorator assumes that the decorated function takes the module name as |
177 | | - the second argument. |
178 | | -
|
179 | | - """ |
180 | | - warnings.warn('The import system now takes care of this automatically; ' |
181 | | - 'this decorator is slated for removal in Python 3.12', |
182 | | - DeprecationWarning, stacklevel=2) |
183 | | - @functools.wraps(fxn) |
184 | | - def module_for_loader_wrapper(self, fullname, *args, **kwargs): |
185 | | - with _module_to_load(fullname) as module: |
186 | | - module.__loader__ = self |
187 | | - try: |
188 | | - is_package = self.is_package(fullname) |
189 | | - except (ImportError, AttributeError): |
190 | | - pass |
191 | | - else: |
192 | | - if is_package: |
193 | | - module.__package__ = fullname |
194 | | - else: |
195 | | - module.__package__ = fullname.rpartition('.')[0] |
196 | | - # If __package__ was not set above, __import__() will do it later. |
197 | | - return fxn(self, module, *args, **kwargs) |
198 | | - |
199 | | - return module_for_loader_wrapper |
200 | | - |
201 | | - |
202 | 115 | class _LazyModule(types.ModuleType): |
203 | 116 |
|
204 | 117 | """A subclass of the module type which triggers loading upon attribute access.""" |
|
0 commit comments