1 | n/a | """Core implementation of import. |
---|
2 | n/a | |
---|
3 | n/a | This module is NOT meant to be directly imported! It has been designed such |
---|
4 | n/a | that it can be bootstrapped into Python as the implementation of import. As |
---|
5 | n/a | such it requires the injection of specific modules and attributes in order to |
---|
6 | n/a | work. One should use importlib as the public-facing version of this module. |
---|
7 | n/a | |
---|
8 | n/a | """ |
---|
9 | n/a | # |
---|
10 | n/a | # IMPORTANT: Whenever making changes to this module, be sure to run |
---|
11 | n/a | # a top-level make in order to get the frozen version of the module |
---|
12 | n/a | # updated. Not doing so will result in the Makefile to fail for |
---|
13 | n/a | # all others who don't have a ./python around to freeze the module |
---|
14 | n/a | # in the early stages of compilation. |
---|
15 | n/a | # |
---|
16 | n/a | |
---|
17 | n/a | # See importlib._setup() for what is injected into the global namespace. |
---|
18 | n/a | |
---|
19 | n/a | # When editing this code be aware that code executed at import time CANNOT |
---|
20 | n/a | # reference any injected objects! This includes not only global code but also |
---|
21 | n/a | # anything specified at the class level. |
---|
22 | n/a | |
---|
23 | n/a | # Bootstrap-related code ###################################################### |
---|
24 | n/a | |
---|
25 | n/a | _bootstrap_external = None |
---|
26 | n/a | |
---|
27 | n/a | def _wrap(new, old): |
---|
28 | n/a | """Simple substitute for functools.update_wrapper.""" |
---|
29 | n/a | for replace in ['__module__', '__name__', '__qualname__', '__doc__']: |
---|
30 | n/a | if hasattr(old, replace): |
---|
31 | n/a | setattr(new, replace, getattr(old, replace)) |
---|
32 | n/a | new.__dict__.update(old.__dict__) |
---|
33 | n/a | |
---|
34 | n/a | |
---|
35 | n/a | def _new_module(name): |
---|
36 | n/a | return type(sys)(name) |
---|
37 | n/a | |
---|
38 | n/a | |
---|
39 | n/a | # Module-level locking ######################################################## |
---|
40 | n/a | |
---|
41 | n/a | # A dict mapping module names to weakrefs of _ModuleLock instances |
---|
42 | n/a | _module_locks = {} |
---|
43 | n/a | # A dict mapping thread ids to _ModuleLock instances |
---|
44 | n/a | _blocking_on = {} |
---|
45 | n/a | |
---|
46 | n/a | |
---|
47 | n/a | class _DeadlockError(RuntimeError): |
---|
48 | n/a | pass |
---|
49 | n/a | |
---|
50 | n/a | |
---|
51 | n/a | class _ModuleLock: |
---|
52 | n/a | """A recursive lock implementation which is able to detect deadlocks |
---|
53 | n/a | (e.g. thread 1 trying to take locks A then B, and thread 2 trying to |
---|
54 | n/a | take locks B then A). |
---|
55 | n/a | """ |
---|
56 | n/a | |
---|
57 | n/a | def __init__(self, name): |
---|
58 | n/a | self.lock = _thread.allocate_lock() |
---|
59 | n/a | self.wakeup = _thread.allocate_lock() |
---|
60 | n/a | self.name = name |
---|
61 | n/a | self.owner = None |
---|
62 | n/a | self.count = 0 |
---|
63 | n/a | self.waiters = 0 |
---|
64 | n/a | |
---|
65 | n/a | def has_deadlock(self): |
---|
66 | n/a | # Deadlock avoidance for concurrent circular imports. |
---|
67 | n/a | me = _thread.get_ident() |
---|
68 | n/a | tid = self.owner |
---|
69 | n/a | while True: |
---|
70 | n/a | lock = _blocking_on.get(tid) |
---|
71 | n/a | if lock is None: |
---|
72 | n/a | return False |
---|
73 | n/a | tid = lock.owner |
---|
74 | n/a | if tid == me: |
---|
75 | n/a | return True |
---|
76 | n/a | |
---|
77 | n/a | def acquire(self): |
---|
78 | n/a | """ |
---|
79 | n/a | Acquire the module lock. If a potential deadlock is detected, |
---|
80 | n/a | a _DeadlockError is raised. |
---|
81 | n/a | Otherwise, the lock is always acquired and True is returned. |
---|
82 | n/a | """ |
---|
83 | n/a | tid = _thread.get_ident() |
---|
84 | n/a | _blocking_on[tid] = self |
---|
85 | n/a | try: |
---|
86 | n/a | while True: |
---|
87 | n/a | with self.lock: |
---|
88 | n/a | if self.count == 0 or self.owner == tid: |
---|
89 | n/a | self.owner = tid |
---|
90 | n/a | self.count += 1 |
---|
91 | n/a | return True |
---|
92 | n/a | if self.has_deadlock(): |
---|
93 | n/a | raise _DeadlockError('deadlock detected by %r' % self) |
---|
94 | n/a | if self.wakeup.acquire(False): |
---|
95 | n/a | self.waiters += 1 |
---|
96 | n/a | # Wait for a release() call |
---|
97 | n/a | self.wakeup.acquire() |
---|
98 | n/a | self.wakeup.release() |
---|
99 | n/a | finally: |
---|
100 | n/a | del _blocking_on[tid] |
---|
101 | n/a | |
---|
102 | n/a | def release(self): |
---|
103 | n/a | tid = _thread.get_ident() |
---|
104 | n/a | with self.lock: |
---|
105 | n/a | if self.owner != tid: |
---|
106 | n/a | raise RuntimeError('cannot release un-acquired lock') |
---|
107 | n/a | assert self.count > 0 |
---|
108 | n/a | self.count -= 1 |
---|
109 | n/a | if self.count == 0: |
---|
110 | n/a | self.owner = None |
---|
111 | n/a | if self.waiters: |
---|
112 | n/a | self.waiters -= 1 |
---|
113 | n/a | self.wakeup.release() |
---|
114 | n/a | |
---|
115 | n/a | def __repr__(self): |
---|
116 | n/a | return '_ModuleLock({!r}) at {}'.format(self.name, id(self)) |
---|
117 | n/a | |
---|
118 | n/a | |
---|
119 | n/a | class _DummyModuleLock: |
---|
120 | n/a | """A simple _ModuleLock equivalent for Python builds without |
---|
121 | n/a | multi-threading support.""" |
---|
122 | n/a | |
---|
123 | n/a | def __init__(self, name): |
---|
124 | n/a | self.name = name |
---|
125 | n/a | self.count = 0 |
---|
126 | n/a | |
---|
127 | n/a | def acquire(self): |
---|
128 | n/a | self.count += 1 |
---|
129 | n/a | return True |
---|
130 | n/a | |
---|
131 | n/a | def release(self): |
---|
132 | n/a | if self.count == 0: |
---|
133 | n/a | raise RuntimeError('cannot release un-acquired lock') |
---|
134 | n/a | self.count -= 1 |
---|
135 | n/a | |
---|
136 | n/a | def __repr__(self): |
---|
137 | n/a | return '_DummyModuleLock({!r}) at {}'.format(self.name, id(self)) |
---|
138 | n/a | |
---|
139 | n/a | |
---|
140 | n/a | class _ModuleLockManager: |
---|
141 | n/a | |
---|
142 | n/a | def __init__(self, name): |
---|
143 | n/a | self._name = name |
---|
144 | n/a | self._lock = None |
---|
145 | n/a | |
---|
146 | n/a | def __enter__(self): |
---|
147 | n/a | try: |
---|
148 | n/a | self._lock = _get_module_lock(self._name) |
---|
149 | n/a | finally: |
---|
150 | n/a | _imp.release_lock() |
---|
151 | n/a | self._lock.acquire() |
---|
152 | n/a | |
---|
153 | n/a | def __exit__(self, *args, **kwargs): |
---|
154 | n/a | self._lock.release() |
---|
155 | n/a | |
---|
156 | n/a | |
---|
157 | n/a | # The following two functions are for consumption by Python/import.c. |
---|
158 | n/a | |
---|
159 | n/a | def _get_module_lock(name): |
---|
160 | n/a | """Get or create the module lock for a given module name. |
---|
161 | n/a | |
---|
162 | n/a | Should only be called with the import lock taken.""" |
---|
163 | n/a | lock = None |
---|
164 | n/a | try: |
---|
165 | n/a | lock = _module_locks[name]() |
---|
166 | n/a | except KeyError: |
---|
167 | n/a | pass |
---|
168 | n/a | if lock is None: |
---|
169 | n/a | if _thread is None: |
---|
170 | n/a | lock = _DummyModuleLock(name) |
---|
171 | n/a | else: |
---|
172 | n/a | lock = _ModuleLock(name) |
---|
173 | n/a | def cb(_): |
---|
174 | n/a | del _module_locks[name] |
---|
175 | n/a | _module_locks[name] = _weakref.ref(lock, cb) |
---|
176 | n/a | return lock |
---|
177 | n/a | |
---|
178 | n/a | def _lock_unlock_module(name): |
---|
179 | n/a | """Release the global import lock, and acquires then release the |
---|
180 | n/a | module lock for a given module name. |
---|
181 | n/a | This is used to ensure a module is completely initialized, in the |
---|
182 | n/a | event it is being imported by another thread. |
---|
183 | n/a | |
---|
184 | n/a | Should only be called with the import lock taken.""" |
---|
185 | n/a | lock = _get_module_lock(name) |
---|
186 | n/a | _imp.release_lock() |
---|
187 | n/a | try: |
---|
188 | n/a | lock.acquire() |
---|
189 | n/a | except _DeadlockError: |
---|
190 | n/a | # Concurrent circular import, we'll accept a partially initialized |
---|
191 | n/a | # module object. |
---|
192 | n/a | pass |
---|
193 | n/a | else: |
---|
194 | n/a | lock.release() |
---|
195 | n/a | |
---|
196 | n/a | # Frame stripping magic ############################################### |
---|
197 | n/a | def _call_with_frames_removed(f, *args, **kwds): |
---|
198 | n/a | """remove_importlib_frames in import.c will always remove sequences |
---|
199 | n/a | of importlib frames that end with a call to this function |
---|
200 | n/a | |
---|
201 | n/a | Use it instead of a normal call in places where including the importlib |
---|
202 | n/a | frames introduces unwanted noise into the traceback (e.g. when executing |
---|
203 | n/a | module code) |
---|
204 | n/a | """ |
---|
205 | n/a | return f(*args, **kwds) |
---|
206 | n/a | |
---|
207 | n/a | |
---|
208 | n/a | def _verbose_message(message, *args, verbosity=1): |
---|
209 | n/a | """Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" |
---|
210 | n/a | if sys.flags.verbose >= verbosity: |
---|
211 | n/a | if not message.startswith(('#', 'import ')): |
---|
212 | n/a | message = '# ' + message |
---|
213 | n/a | print(message.format(*args), file=sys.stderr) |
---|
214 | n/a | |
---|
215 | n/a | |
---|
216 | n/a | def _requires_builtin(fxn): |
---|
217 | n/a | """Decorator to verify the named module is built-in.""" |
---|
218 | n/a | def _requires_builtin_wrapper(self, fullname): |
---|
219 | n/a | if fullname not in sys.builtin_module_names: |
---|
220 | n/a | raise ImportError('{!r} is not a built-in module'.format(fullname), |
---|
221 | n/a | name=fullname) |
---|
222 | n/a | return fxn(self, fullname) |
---|
223 | n/a | _wrap(_requires_builtin_wrapper, fxn) |
---|
224 | n/a | return _requires_builtin_wrapper |
---|
225 | n/a | |
---|
226 | n/a | |
---|
227 | n/a | def _requires_frozen(fxn): |
---|
228 | n/a | """Decorator to verify the named module is frozen.""" |
---|
229 | n/a | def _requires_frozen_wrapper(self, fullname): |
---|
230 | n/a | if not _imp.is_frozen(fullname): |
---|
231 | n/a | raise ImportError('{!r} is not a frozen module'.format(fullname), |
---|
232 | n/a | name=fullname) |
---|
233 | n/a | return fxn(self, fullname) |
---|
234 | n/a | _wrap(_requires_frozen_wrapper, fxn) |
---|
235 | n/a | return _requires_frozen_wrapper |
---|
236 | n/a | |
---|
237 | n/a | |
---|
238 | n/a | # Typically used by loader classes as a method replacement. |
---|
239 | n/a | def _load_module_shim(self, fullname): |
---|
240 | n/a | """Load the specified module into sys.modules and return it. |
---|
241 | n/a | |
---|
242 | n/a | This method is deprecated. Use loader.exec_module instead. |
---|
243 | n/a | |
---|
244 | n/a | """ |
---|
245 | n/a | spec = spec_from_loader(fullname, self) |
---|
246 | n/a | if fullname in sys.modules: |
---|
247 | n/a | module = sys.modules[fullname] |
---|
248 | n/a | _exec(spec, module) |
---|
249 | n/a | return sys.modules[fullname] |
---|
250 | n/a | else: |
---|
251 | n/a | return _load(spec) |
---|
252 | n/a | |
---|
253 | n/a | # Module specifications ####################################################### |
---|
254 | n/a | |
---|
255 | n/a | def _module_repr(module): |
---|
256 | n/a | # The implementation of ModuleType.__repr__(). |
---|
257 | n/a | loader = getattr(module, '__loader__', None) |
---|
258 | n/a | if hasattr(loader, 'module_repr'): |
---|
259 | n/a | # As soon as BuiltinImporter, FrozenImporter, and NamespaceLoader |
---|
260 | n/a | # drop their implementations for module_repr. we can add a |
---|
261 | n/a | # deprecation warning here. |
---|
262 | n/a | try: |
---|
263 | n/a | return loader.module_repr(module) |
---|
264 | n/a | except Exception: |
---|
265 | n/a | pass |
---|
266 | n/a | try: |
---|
267 | n/a | spec = module.__spec__ |
---|
268 | n/a | except AttributeError: |
---|
269 | n/a | pass |
---|
270 | n/a | else: |
---|
271 | n/a | if spec is not None: |
---|
272 | n/a | return _module_repr_from_spec(spec) |
---|
273 | n/a | |
---|
274 | n/a | # We could use module.__class__.__name__ instead of 'module' in the |
---|
275 | n/a | # various repr permutations. |
---|
276 | n/a | try: |
---|
277 | n/a | name = module.__name__ |
---|
278 | n/a | except AttributeError: |
---|
279 | n/a | name = '?' |
---|
280 | n/a | try: |
---|
281 | n/a | filename = module.__file__ |
---|
282 | n/a | except AttributeError: |
---|
283 | n/a | if loader is None: |
---|
284 | n/a | return '<module {!r}>'.format(name) |
---|
285 | n/a | else: |
---|
286 | n/a | return '<module {!r} ({!r})>'.format(name, loader) |
---|
287 | n/a | else: |
---|
288 | n/a | return '<module {!r} from {!r}>'.format(name, filename) |
---|
289 | n/a | |
---|
290 | n/a | |
---|
291 | n/a | class _installed_safely: |
---|
292 | n/a | |
---|
293 | n/a | def __init__(self, module): |
---|
294 | n/a | self._module = module |
---|
295 | n/a | self._spec = module.__spec__ |
---|
296 | n/a | |
---|
297 | n/a | def __enter__(self): |
---|
298 | n/a | # This must be done before putting the module in sys.modules |
---|
299 | n/a | # (otherwise an optimization shortcut in import.c becomes |
---|
300 | n/a | # wrong) |
---|
301 | n/a | self._spec._initializing = True |
---|
302 | n/a | sys.modules[self._spec.name] = self._module |
---|
303 | n/a | |
---|
304 | n/a | def __exit__(self, *args): |
---|
305 | n/a | try: |
---|
306 | n/a | spec = self._spec |
---|
307 | n/a | if any(arg is not None for arg in args): |
---|
308 | n/a | try: |
---|
309 | n/a | del sys.modules[spec.name] |
---|
310 | n/a | except KeyError: |
---|
311 | n/a | pass |
---|
312 | n/a | else: |
---|
313 | n/a | _verbose_message('import {!r} # {!r}', spec.name, spec.loader) |
---|
314 | n/a | finally: |
---|
315 | n/a | self._spec._initializing = False |
---|
316 | n/a | |
---|
317 | n/a | |
---|
318 | n/a | class ModuleSpec: |
---|
319 | n/a | """The specification for a module, used for loading. |
---|
320 | n/a | |
---|
321 | n/a | A module's spec is the source for information about the module. For |
---|
322 | n/a | data associated with the module, including source, use the spec's |
---|
323 | n/a | loader. |
---|
324 | n/a | |
---|
325 | n/a | `name` is the absolute name of the module. `loader` is the loader |
---|
326 | n/a | to use when loading the module. `parent` is the name of the |
---|
327 | n/a | package the module is in. The parent is derived from the name. |
---|
328 | n/a | |
---|
329 | n/a | `is_package` determines if the module is considered a package or |
---|
330 | n/a | not. On modules this is reflected by the `__path__` attribute. |
---|
331 | n/a | |
---|
332 | n/a | `origin` is the specific location used by the loader from which to |
---|
333 | n/a | load the module, if that information is available. When filename is |
---|
334 | n/a | set, origin will match. |
---|
335 | n/a | |
---|
336 | n/a | `has_location` indicates that a spec's "origin" reflects a location. |
---|
337 | n/a | When this is True, `__file__` attribute of the module is set. |
---|
338 | n/a | |
---|
339 | n/a | `cached` is the location of the cached bytecode file, if any. It |
---|
340 | n/a | corresponds to the `__cached__` attribute. |
---|
341 | n/a | |
---|
342 | n/a | `submodule_search_locations` is the sequence of path entries to |
---|
343 | n/a | search when importing submodules. If set, is_package should be |
---|
344 | n/a | True--and False otherwise. |
---|
345 | n/a | |
---|
346 | n/a | Packages are simply modules that (may) have submodules. If a spec |
---|
347 | n/a | has a non-None value in `submodule_search_locations`, the import |
---|
348 | n/a | system will consider modules loaded from the spec as packages. |
---|
349 | n/a | |
---|
350 | n/a | Only finders (see importlib.abc.MetaPathFinder and |
---|
351 | n/a | importlib.abc.PathEntryFinder) should modify ModuleSpec instances. |
---|
352 | n/a | |
---|
353 | n/a | """ |
---|
354 | n/a | |
---|
355 | n/a | def __init__(self, name, loader, *, origin=None, loader_state=None, |
---|
356 | n/a | is_package=None): |
---|
357 | n/a | self.name = name |
---|
358 | n/a | self.loader = loader |
---|
359 | n/a | self.origin = origin |
---|
360 | n/a | self.loader_state = loader_state |
---|
361 | n/a | self.submodule_search_locations = [] if is_package else None |
---|
362 | n/a | |
---|
363 | n/a | # file-location attributes |
---|
364 | n/a | self._set_fileattr = False |
---|
365 | n/a | self._cached = None |
---|
366 | n/a | |
---|
367 | n/a | def __repr__(self): |
---|
368 | n/a | args = ['name={!r}'.format(self.name), |
---|
369 | n/a | 'loader={!r}'.format(self.loader)] |
---|
370 | n/a | if self.origin is not None: |
---|
371 | n/a | args.append('origin={!r}'.format(self.origin)) |
---|
372 | n/a | if self.submodule_search_locations is not None: |
---|
373 | n/a | args.append('submodule_search_locations={}' |
---|
374 | n/a | .format(self.submodule_search_locations)) |
---|
375 | n/a | return '{}({})'.format(self.__class__.__name__, ', '.join(args)) |
---|
376 | n/a | |
---|
377 | n/a | def __eq__(self, other): |
---|
378 | n/a | smsl = self.submodule_search_locations |
---|
379 | n/a | try: |
---|
380 | n/a | return (self.name == other.name and |
---|
381 | n/a | self.loader == other.loader and |
---|
382 | n/a | self.origin == other.origin and |
---|
383 | n/a | smsl == other.submodule_search_locations and |
---|
384 | n/a | self.cached == other.cached and |
---|
385 | n/a | self.has_location == other.has_location) |
---|
386 | n/a | except AttributeError: |
---|
387 | n/a | return False |
---|
388 | n/a | |
---|
389 | n/a | @property |
---|
390 | n/a | def cached(self): |
---|
391 | n/a | if self._cached is None: |
---|
392 | n/a | if self.origin is not None and self._set_fileattr: |
---|
393 | n/a | if _bootstrap_external is None: |
---|
394 | n/a | raise NotImplementedError |
---|
395 | n/a | self._cached = _bootstrap_external._get_cached(self.origin) |
---|
396 | n/a | return self._cached |
---|
397 | n/a | |
---|
398 | n/a | @cached.setter |
---|
399 | n/a | def cached(self, cached): |
---|
400 | n/a | self._cached = cached |
---|
401 | n/a | |
---|
402 | n/a | @property |
---|
403 | n/a | def parent(self): |
---|
404 | n/a | """The name of the module's parent.""" |
---|
405 | n/a | if self.submodule_search_locations is None: |
---|
406 | n/a | return self.name.rpartition('.')[0] |
---|
407 | n/a | else: |
---|
408 | n/a | return self.name |
---|
409 | n/a | |
---|
410 | n/a | @property |
---|
411 | n/a | def has_location(self): |
---|
412 | n/a | return self._set_fileattr |
---|
413 | n/a | |
---|
414 | n/a | @has_location.setter |
---|
415 | n/a | def has_location(self, value): |
---|
416 | n/a | self._set_fileattr = bool(value) |
---|
417 | n/a | |
---|
418 | n/a | |
---|
419 | n/a | def spec_from_loader(name, loader, *, origin=None, is_package=None): |
---|
420 | n/a | """Return a module spec based on various loader methods.""" |
---|
421 | n/a | if hasattr(loader, 'get_filename'): |
---|
422 | n/a | if _bootstrap_external is None: |
---|
423 | n/a | raise NotImplementedError |
---|
424 | n/a | spec_from_file_location = _bootstrap_external.spec_from_file_location |
---|
425 | n/a | |
---|
426 | n/a | if is_package is None: |
---|
427 | n/a | return spec_from_file_location(name, loader=loader) |
---|
428 | n/a | search = [] if is_package else None |
---|
429 | n/a | return spec_from_file_location(name, loader=loader, |
---|
430 | n/a | submodule_search_locations=search) |
---|
431 | n/a | |
---|
432 | n/a | if is_package is None: |
---|
433 | n/a | if hasattr(loader, 'is_package'): |
---|
434 | n/a | try: |
---|
435 | n/a | is_package = loader.is_package(name) |
---|
436 | n/a | except ImportError: |
---|
437 | n/a | is_package = None # aka, undefined |
---|
438 | n/a | else: |
---|
439 | n/a | # the default |
---|
440 | n/a | is_package = False |
---|
441 | n/a | |
---|
442 | n/a | return ModuleSpec(name, loader, origin=origin, is_package=is_package) |
---|
443 | n/a | |
---|
444 | n/a | |
---|
445 | n/a | _POPULATE = object() |
---|
446 | n/a | |
---|
447 | n/a | |
---|
448 | n/a | def _spec_from_module(module, loader=None, origin=None): |
---|
449 | n/a | # This function is meant for use in _setup(). |
---|
450 | n/a | try: |
---|
451 | n/a | spec = module.__spec__ |
---|
452 | n/a | except AttributeError: |
---|
453 | n/a | pass |
---|
454 | n/a | else: |
---|
455 | n/a | if spec is not None: |
---|
456 | n/a | return spec |
---|
457 | n/a | |
---|
458 | n/a | name = module.__name__ |
---|
459 | n/a | if loader is None: |
---|
460 | n/a | try: |
---|
461 | n/a | loader = module.__loader__ |
---|
462 | n/a | except AttributeError: |
---|
463 | n/a | # loader will stay None. |
---|
464 | n/a | pass |
---|
465 | n/a | try: |
---|
466 | n/a | location = module.__file__ |
---|
467 | n/a | except AttributeError: |
---|
468 | n/a | location = None |
---|
469 | n/a | if origin is None: |
---|
470 | n/a | if location is None: |
---|
471 | n/a | try: |
---|
472 | n/a | origin = loader._ORIGIN |
---|
473 | n/a | except AttributeError: |
---|
474 | n/a | origin = None |
---|
475 | n/a | else: |
---|
476 | n/a | origin = location |
---|
477 | n/a | try: |
---|
478 | n/a | cached = module.__cached__ |
---|
479 | n/a | except AttributeError: |
---|
480 | n/a | cached = None |
---|
481 | n/a | try: |
---|
482 | n/a | submodule_search_locations = list(module.__path__) |
---|
483 | n/a | except AttributeError: |
---|
484 | n/a | submodule_search_locations = None |
---|
485 | n/a | |
---|
486 | n/a | spec = ModuleSpec(name, loader, origin=origin) |
---|
487 | n/a | spec._set_fileattr = False if location is None else True |
---|
488 | n/a | spec.cached = cached |
---|
489 | n/a | spec.submodule_search_locations = submodule_search_locations |
---|
490 | n/a | return spec |
---|
491 | n/a | |
---|
492 | n/a | |
---|
493 | n/a | def _init_module_attrs(spec, module, *, override=False): |
---|
494 | n/a | # The passed-in module may be not support attribute assignment, |
---|
495 | n/a | # in which case we simply don't set the attributes. |
---|
496 | n/a | # __name__ |
---|
497 | n/a | if (override or getattr(module, '__name__', None) is None): |
---|
498 | n/a | try: |
---|
499 | n/a | module.__name__ = spec.name |
---|
500 | n/a | except AttributeError: |
---|
501 | n/a | pass |
---|
502 | n/a | # __loader__ |
---|
503 | n/a | if override or getattr(module, '__loader__', None) is None: |
---|
504 | n/a | loader = spec.loader |
---|
505 | n/a | if loader is None: |
---|
506 | n/a | # A backward compatibility hack. |
---|
507 | n/a | if spec.submodule_search_locations is not None: |
---|
508 | n/a | if _bootstrap_external is None: |
---|
509 | n/a | raise NotImplementedError |
---|
510 | n/a | _NamespaceLoader = _bootstrap_external._NamespaceLoader |
---|
511 | n/a | |
---|
512 | n/a | loader = _NamespaceLoader.__new__(_NamespaceLoader) |
---|
513 | n/a | loader._path = spec.submodule_search_locations |
---|
514 | n/a | try: |
---|
515 | n/a | module.__loader__ = loader |
---|
516 | n/a | except AttributeError: |
---|
517 | n/a | pass |
---|
518 | n/a | # __package__ |
---|
519 | n/a | if override or getattr(module, '__package__', None) is None: |
---|
520 | n/a | try: |
---|
521 | n/a | module.__package__ = spec.parent |
---|
522 | n/a | except AttributeError: |
---|
523 | n/a | pass |
---|
524 | n/a | # __spec__ |
---|
525 | n/a | try: |
---|
526 | n/a | module.__spec__ = spec |
---|
527 | n/a | except AttributeError: |
---|
528 | n/a | pass |
---|
529 | n/a | # __path__ |
---|
530 | n/a | if override or getattr(module, '__path__', None) is None: |
---|
531 | n/a | if spec.submodule_search_locations is not None: |
---|
532 | n/a | try: |
---|
533 | n/a | module.__path__ = spec.submodule_search_locations |
---|
534 | n/a | except AttributeError: |
---|
535 | n/a | pass |
---|
536 | n/a | # __file__/__cached__ |
---|
537 | n/a | if spec.has_location: |
---|
538 | n/a | if override or getattr(module, '__file__', None) is None: |
---|
539 | n/a | try: |
---|
540 | n/a | module.__file__ = spec.origin |
---|
541 | n/a | except AttributeError: |
---|
542 | n/a | pass |
---|
543 | n/a | |
---|
544 | n/a | if override or getattr(module, '__cached__', None) is None: |
---|
545 | n/a | if spec.cached is not None: |
---|
546 | n/a | try: |
---|
547 | n/a | module.__cached__ = spec.cached |
---|
548 | n/a | except AttributeError: |
---|
549 | n/a | pass |
---|
550 | n/a | return module |
---|
551 | n/a | |
---|
552 | n/a | |
---|
553 | n/a | def module_from_spec(spec): |
---|
554 | n/a | """Create a module based on the provided spec.""" |
---|
555 | n/a | # Typically loaders will not implement create_module(). |
---|
556 | n/a | module = None |
---|
557 | n/a | if hasattr(spec.loader, 'create_module'): |
---|
558 | n/a | # If create_module() returns `None` then it means default |
---|
559 | n/a | # module creation should be used. |
---|
560 | n/a | module = spec.loader.create_module(spec) |
---|
561 | n/a | elif hasattr(spec.loader, 'exec_module'): |
---|
562 | n/a | raise ImportError('loaders that define exec_module() ' |
---|
563 | n/a | 'must also define create_module()') |
---|
564 | n/a | if module is None: |
---|
565 | n/a | module = _new_module(spec.name) |
---|
566 | n/a | _init_module_attrs(spec, module) |
---|
567 | n/a | return module |
---|
568 | n/a | |
---|
569 | n/a | |
---|
570 | n/a | def _module_repr_from_spec(spec): |
---|
571 | n/a | """Return the repr to use for the module.""" |
---|
572 | n/a | # We mostly replicate _module_repr() using the spec attributes. |
---|
573 | n/a | name = '?' if spec.name is None else spec.name |
---|
574 | n/a | if spec.origin is None: |
---|
575 | n/a | if spec.loader is None: |
---|
576 | n/a | return '<module {!r}>'.format(name) |
---|
577 | n/a | else: |
---|
578 | n/a | return '<module {!r} ({!r})>'.format(name, spec.loader) |
---|
579 | n/a | else: |
---|
580 | n/a | if spec.has_location: |
---|
581 | n/a | return '<module {!r} from {!r}>'.format(name, spec.origin) |
---|
582 | n/a | else: |
---|
583 | n/a | return '<module {!r} ({})>'.format(spec.name, spec.origin) |
---|
584 | n/a | |
---|
585 | n/a | |
---|
586 | n/a | # Used by importlib.reload() and _load_module_shim(). |
---|
587 | n/a | def _exec(spec, module): |
---|
588 | n/a | """Execute the spec's specified module in an existing module's namespace.""" |
---|
589 | n/a | name = spec.name |
---|
590 | n/a | _imp.acquire_lock() |
---|
591 | n/a | with _ModuleLockManager(name): |
---|
592 | n/a | if sys.modules.get(name) is not module: |
---|
593 | n/a | msg = 'module {!r} not in sys.modules'.format(name) |
---|
594 | n/a | raise ImportError(msg, name=name) |
---|
595 | n/a | if spec.loader is None: |
---|
596 | n/a | if spec.submodule_search_locations is None: |
---|
597 | n/a | raise ImportError('missing loader', name=spec.name) |
---|
598 | n/a | # namespace package |
---|
599 | n/a | _init_module_attrs(spec, module, override=True) |
---|
600 | n/a | return module |
---|
601 | n/a | _init_module_attrs(spec, module, override=True) |
---|
602 | n/a | if not hasattr(spec.loader, 'exec_module'): |
---|
603 | n/a | # (issue19713) Once BuiltinImporter and ExtensionFileLoader |
---|
604 | n/a | # have exec_module() implemented, we can add a deprecation |
---|
605 | n/a | # warning here. |
---|
606 | n/a | spec.loader.load_module(name) |
---|
607 | n/a | else: |
---|
608 | n/a | spec.loader.exec_module(module) |
---|
609 | n/a | return sys.modules[name] |
---|
610 | n/a | |
---|
611 | n/a | |
---|
612 | n/a | def _load_backward_compatible(spec): |
---|
613 | n/a | # (issue19713) Once BuiltinImporter and ExtensionFileLoader |
---|
614 | n/a | # have exec_module() implemented, we can add a deprecation |
---|
615 | n/a | # warning here. |
---|
616 | n/a | spec.loader.load_module(spec.name) |
---|
617 | n/a | # The module must be in sys.modules at this point! |
---|
618 | n/a | module = sys.modules[spec.name] |
---|
619 | n/a | if getattr(module, '__loader__', None) is None: |
---|
620 | n/a | try: |
---|
621 | n/a | module.__loader__ = spec.loader |
---|
622 | n/a | except AttributeError: |
---|
623 | n/a | pass |
---|
624 | n/a | if getattr(module, '__package__', None) is None: |
---|
625 | n/a | try: |
---|
626 | n/a | # Since module.__path__ may not line up with |
---|
627 | n/a | # spec.submodule_search_paths, we can't necessarily rely |
---|
628 | n/a | # on spec.parent here. |
---|
629 | n/a | module.__package__ = module.__name__ |
---|
630 | n/a | if not hasattr(module, '__path__'): |
---|
631 | n/a | module.__package__ = spec.name.rpartition('.')[0] |
---|
632 | n/a | except AttributeError: |
---|
633 | n/a | pass |
---|
634 | n/a | if getattr(module, '__spec__', None) is None: |
---|
635 | n/a | try: |
---|
636 | n/a | module.__spec__ = spec |
---|
637 | n/a | except AttributeError: |
---|
638 | n/a | pass |
---|
639 | n/a | return module |
---|
640 | n/a | |
---|
641 | n/a | def _load_unlocked(spec): |
---|
642 | n/a | # A helper for direct use by the import system. |
---|
643 | n/a | if spec.loader is not None: |
---|
644 | n/a | # not a namespace package |
---|
645 | n/a | if not hasattr(spec.loader, 'exec_module'): |
---|
646 | n/a | return _load_backward_compatible(spec) |
---|
647 | n/a | |
---|
648 | n/a | module = module_from_spec(spec) |
---|
649 | n/a | with _installed_safely(module): |
---|
650 | n/a | if spec.loader is None: |
---|
651 | n/a | if spec.submodule_search_locations is None: |
---|
652 | n/a | raise ImportError('missing loader', name=spec.name) |
---|
653 | n/a | # A namespace package so do nothing. |
---|
654 | n/a | else: |
---|
655 | n/a | spec.loader.exec_module(module) |
---|
656 | n/a | |
---|
657 | n/a | # We don't ensure that the import-related module attributes get |
---|
658 | n/a | # set in the sys.modules replacement case. Such modules are on |
---|
659 | n/a | # their own. |
---|
660 | n/a | return sys.modules[spec.name] |
---|
661 | n/a | |
---|
662 | n/a | # A method used during testing of _load_unlocked() and by |
---|
663 | n/a | # _load_module_shim(). |
---|
664 | n/a | def _load(spec): |
---|
665 | n/a | """Return a new module object, loaded by the spec's loader. |
---|
666 | n/a | |
---|
667 | n/a | The module is not added to its parent. |
---|
668 | n/a | |
---|
669 | n/a | If a module is already in sys.modules, that existing module gets |
---|
670 | n/a | clobbered. |
---|
671 | n/a | |
---|
672 | n/a | """ |
---|
673 | n/a | _imp.acquire_lock() |
---|
674 | n/a | with _ModuleLockManager(spec.name): |
---|
675 | n/a | return _load_unlocked(spec) |
---|
676 | n/a | |
---|
677 | n/a | |
---|
678 | n/a | # Loaders ##################################################################### |
---|
679 | n/a | |
---|
680 | n/a | class BuiltinImporter: |
---|
681 | n/a | |
---|
682 | n/a | """Meta path import for built-in modules. |
---|
683 | n/a | |
---|
684 | n/a | All methods are either class or static methods to avoid the need to |
---|
685 | n/a | instantiate the class. |
---|
686 | n/a | |
---|
687 | n/a | """ |
---|
688 | n/a | |
---|
689 | n/a | @staticmethod |
---|
690 | n/a | def module_repr(module): |
---|
691 | n/a | """Return repr for the module. |
---|
692 | n/a | |
---|
693 | n/a | The method is deprecated. The import machinery does the job itself. |
---|
694 | n/a | |
---|
695 | n/a | """ |
---|
696 | n/a | return '<module {!r} (built-in)>'.format(module.__name__) |
---|
697 | n/a | |
---|
698 | n/a | @classmethod |
---|
699 | n/a | def find_spec(cls, fullname, path=None, target=None): |
---|
700 | n/a | if path is not None: |
---|
701 | n/a | return None |
---|
702 | n/a | if _imp.is_builtin(fullname): |
---|
703 | n/a | return spec_from_loader(fullname, cls, origin='built-in') |
---|
704 | n/a | else: |
---|
705 | n/a | return None |
---|
706 | n/a | |
---|
707 | n/a | @classmethod |
---|
708 | n/a | def find_module(cls, fullname, path=None): |
---|
709 | n/a | """Find the built-in module. |
---|
710 | n/a | |
---|
711 | n/a | If 'path' is ever specified then the search is considered a failure. |
---|
712 | n/a | |
---|
713 | n/a | This method is deprecated. Use find_spec() instead. |
---|
714 | n/a | |
---|
715 | n/a | """ |
---|
716 | n/a | spec = cls.find_spec(fullname, path) |
---|
717 | n/a | return spec.loader if spec is not None else None |
---|
718 | n/a | |
---|
719 | n/a | @classmethod |
---|
720 | n/a | def create_module(self, spec): |
---|
721 | n/a | """Create a built-in module""" |
---|
722 | n/a | if spec.name not in sys.builtin_module_names: |
---|
723 | n/a | raise ImportError('{!r} is not a built-in module'.format(spec.name), |
---|
724 | n/a | name=spec.name) |
---|
725 | n/a | return _call_with_frames_removed(_imp.create_builtin, spec) |
---|
726 | n/a | |
---|
727 | n/a | @classmethod |
---|
728 | n/a | def exec_module(self, module): |
---|
729 | n/a | """Exec a built-in module""" |
---|
730 | n/a | _call_with_frames_removed(_imp.exec_builtin, module) |
---|
731 | n/a | |
---|
732 | n/a | @classmethod |
---|
733 | n/a | @_requires_builtin |
---|
734 | n/a | def get_code(cls, fullname): |
---|
735 | n/a | """Return None as built-in modules do not have code objects.""" |
---|
736 | n/a | return None |
---|
737 | n/a | |
---|
738 | n/a | @classmethod |
---|
739 | n/a | @_requires_builtin |
---|
740 | n/a | def get_source(cls, fullname): |
---|
741 | n/a | """Return None as built-in modules do not have source code.""" |
---|
742 | n/a | return None |
---|
743 | n/a | |
---|
744 | n/a | @classmethod |
---|
745 | n/a | @_requires_builtin |
---|
746 | n/a | def is_package(cls, fullname): |
---|
747 | n/a | """Return False as built-in modules are never packages.""" |
---|
748 | n/a | return False |
---|
749 | n/a | |
---|
750 | n/a | load_module = classmethod(_load_module_shim) |
---|
751 | n/a | |
---|
752 | n/a | |
---|
753 | n/a | class FrozenImporter: |
---|
754 | n/a | |
---|
755 | n/a | """Meta path import for frozen modules. |
---|
756 | n/a | |
---|
757 | n/a | All methods are either class or static methods to avoid the need to |
---|
758 | n/a | instantiate the class. |
---|
759 | n/a | |
---|
760 | n/a | """ |
---|
761 | n/a | |
---|
762 | n/a | @staticmethod |
---|
763 | n/a | def module_repr(m): |
---|
764 | n/a | """Return repr for the module. |
---|
765 | n/a | |
---|
766 | n/a | The method is deprecated. The import machinery does the job itself. |
---|
767 | n/a | |
---|
768 | n/a | """ |
---|
769 | n/a | return '<module {!r} (frozen)>'.format(m.__name__) |
---|
770 | n/a | |
---|
771 | n/a | @classmethod |
---|
772 | n/a | def find_spec(cls, fullname, path=None, target=None): |
---|
773 | n/a | if _imp.is_frozen(fullname): |
---|
774 | n/a | return spec_from_loader(fullname, cls, origin='frozen') |
---|
775 | n/a | else: |
---|
776 | n/a | return None |
---|
777 | n/a | |
---|
778 | n/a | @classmethod |
---|
779 | n/a | def find_module(cls, fullname, path=None): |
---|
780 | n/a | """Find a frozen module. |
---|
781 | n/a | |
---|
782 | n/a | This method is deprecated. Use find_spec() instead. |
---|
783 | n/a | |
---|
784 | n/a | """ |
---|
785 | n/a | return cls if _imp.is_frozen(fullname) else None |
---|
786 | n/a | |
---|
787 | n/a | @classmethod |
---|
788 | n/a | def create_module(cls, spec): |
---|
789 | n/a | """Use default semantics for module creation.""" |
---|
790 | n/a | |
---|
791 | n/a | @staticmethod |
---|
792 | n/a | def exec_module(module): |
---|
793 | n/a | name = module.__spec__.name |
---|
794 | n/a | if not _imp.is_frozen(name): |
---|
795 | n/a | raise ImportError('{!r} is not a frozen module'.format(name), |
---|
796 | n/a | name=name) |
---|
797 | n/a | code = _call_with_frames_removed(_imp.get_frozen_object, name) |
---|
798 | n/a | exec(code, module.__dict__) |
---|
799 | n/a | |
---|
800 | n/a | @classmethod |
---|
801 | n/a | def load_module(cls, fullname): |
---|
802 | n/a | """Load a frozen module. |
---|
803 | n/a | |
---|
804 | n/a | This method is deprecated. Use exec_module() instead. |
---|
805 | n/a | |
---|
806 | n/a | """ |
---|
807 | n/a | return _load_module_shim(cls, fullname) |
---|
808 | n/a | |
---|
809 | n/a | @classmethod |
---|
810 | n/a | @_requires_frozen |
---|
811 | n/a | def get_code(cls, fullname): |
---|
812 | n/a | """Return the code object for the frozen module.""" |
---|
813 | n/a | return _imp.get_frozen_object(fullname) |
---|
814 | n/a | |
---|
815 | n/a | @classmethod |
---|
816 | n/a | @_requires_frozen |
---|
817 | n/a | def get_source(cls, fullname): |
---|
818 | n/a | """Return None as frozen modules do not have source code.""" |
---|
819 | n/a | return None |
---|
820 | n/a | |
---|
821 | n/a | @classmethod |
---|
822 | n/a | @_requires_frozen |
---|
823 | n/a | def is_package(cls, fullname): |
---|
824 | n/a | """Return True if the frozen module is a package.""" |
---|
825 | n/a | return _imp.is_frozen_package(fullname) |
---|
826 | n/a | |
---|
827 | n/a | |
---|
828 | n/a | # Import itself ############################################################### |
---|
829 | n/a | |
---|
830 | n/a | class _ImportLockContext: |
---|
831 | n/a | |
---|
832 | n/a | """Context manager for the import lock.""" |
---|
833 | n/a | |
---|
834 | n/a | def __enter__(self): |
---|
835 | n/a | """Acquire the import lock.""" |
---|
836 | n/a | _imp.acquire_lock() |
---|
837 | n/a | |
---|
838 | n/a | def __exit__(self, exc_type, exc_value, exc_traceback): |
---|
839 | n/a | """Release the import lock regardless of any raised exceptions.""" |
---|
840 | n/a | _imp.release_lock() |
---|
841 | n/a | |
---|
842 | n/a | |
---|
843 | n/a | def _resolve_name(name, package, level): |
---|
844 | n/a | """Resolve a relative module name to an absolute one.""" |
---|
845 | n/a | bits = package.rsplit('.', level - 1) |
---|
846 | n/a | if len(bits) < level: |
---|
847 | n/a | raise ValueError('attempted relative import beyond top-level package') |
---|
848 | n/a | base = bits[0] |
---|
849 | n/a | return '{}.{}'.format(base, name) if name else base |
---|
850 | n/a | |
---|
851 | n/a | |
---|
852 | n/a | def _find_spec_legacy(finder, name, path): |
---|
853 | n/a | # This would be a good place for a DeprecationWarning if |
---|
854 | n/a | # we ended up going that route. |
---|
855 | n/a | loader = finder.find_module(name, path) |
---|
856 | n/a | if loader is None: |
---|
857 | n/a | return None |
---|
858 | n/a | return spec_from_loader(name, loader) |
---|
859 | n/a | |
---|
860 | n/a | |
---|
861 | n/a | def _find_spec(name, path, target=None): |
---|
862 | n/a | """Find a module's spec.""" |
---|
863 | n/a | meta_path = sys.meta_path |
---|
864 | n/a | if meta_path is None: |
---|
865 | n/a | # PyImport_Cleanup() is running or has been called. |
---|
866 | n/a | raise ImportError("sys.meta_path is None, Python is likely " |
---|
867 | n/a | "shutting down") |
---|
868 | n/a | |
---|
869 | n/a | if not meta_path: |
---|
870 | n/a | _warnings.warn('sys.meta_path is empty', ImportWarning) |
---|
871 | n/a | |
---|
872 | n/a | # We check sys.modules here for the reload case. While a passed-in |
---|
873 | n/a | # target will usually indicate a reload there is no guarantee, whereas |
---|
874 | n/a | # sys.modules provides one. |
---|
875 | n/a | is_reload = name in sys.modules |
---|
876 | n/a | for finder in meta_path: |
---|
877 | n/a | with _ImportLockContext(): |
---|
878 | n/a | try: |
---|
879 | n/a | find_spec = finder.find_spec |
---|
880 | n/a | except AttributeError: |
---|
881 | n/a | spec = _find_spec_legacy(finder, name, path) |
---|
882 | n/a | if spec is None: |
---|
883 | n/a | continue |
---|
884 | n/a | else: |
---|
885 | n/a | spec = find_spec(name, path, target) |
---|
886 | n/a | if spec is not None: |
---|
887 | n/a | # The parent import may have already imported this module. |
---|
888 | n/a | if not is_reload and name in sys.modules: |
---|
889 | n/a | module = sys.modules[name] |
---|
890 | n/a | try: |
---|
891 | n/a | __spec__ = module.__spec__ |
---|
892 | n/a | except AttributeError: |
---|
893 | n/a | # We use the found spec since that is the one that |
---|
894 | n/a | # we would have used if the parent module hadn't |
---|
895 | n/a | # beaten us to the punch. |
---|
896 | n/a | return spec |
---|
897 | n/a | else: |
---|
898 | n/a | if __spec__ is None: |
---|
899 | n/a | return spec |
---|
900 | n/a | else: |
---|
901 | n/a | return __spec__ |
---|
902 | n/a | else: |
---|
903 | n/a | return spec |
---|
904 | n/a | else: |
---|
905 | n/a | return None |
---|
906 | n/a | |
---|
907 | n/a | |
---|
908 | n/a | def _sanity_check(name, package, level): |
---|
909 | n/a | """Verify arguments are "sane".""" |
---|
910 | n/a | if not isinstance(name, str): |
---|
911 | n/a | raise TypeError('module name must be str, not {}'.format(type(name))) |
---|
912 | n/a | if level < 0: |
---|
913 | n/a | raise ValueError('level must be >= 0') |
---|
914 | n/a | if level > 0: |
---|
915 | n/a | if not isinstance(package, str): |
---|
916 | n/a | raise TypeError('__package__ not set to a string') |
---|
917 | n/a | elif not package: |
---|
918 | n/a | raise ImportError('attempted relative import with no known parent ' |
---|
919 | n/a | 'package') |
---|
920 | n/a | elif package not in sys.modules: |
---|
921 | n/a | msg = ('Parent module {!r} not loaded, cannot perform relative ' |
---|
922 | n/a | 'import') |
---|
923 | n/a | raise SystemError(msg.format(package)) |
---|
924 | n/a | if not name and level == 0: |
---|
925 | n/a | raise ValueError('Empty module name') |
---|
926 | n/a | |
---|
927 | n/a | |
---|
928 | n/a | _ERR_MSG_PREFIX = 'No module named ' |
---|
929 | n/a | _ERR_MSG = _ERR_MSG_PREFIX + '{!r}' |
---|
930 | n/a | |
---|
931 | n/a | def _find_and_load_unlocked(name, import_): |
---|
932 | n/a | path = None |
---|
933 | n/a | parent = name.rpartition('.')[0] |
---|
934 | n/a | if parent: |
---|
935 | n/a | if parent not in sys.modules: |
---|
936 | n/a | _call_with_frames_removed(import_, parent) |
---|
937 | n/a | # Crazy side-effects! |
---|
938 | n/a | if name in sys.modules: |
---|
939 | n/a | return sys.modules[name] |
---|
940 | n/a | parent_module = sys.modules[parent] |
---|
941 | n/a | try: |
---|
942 | n/a | path = parent_module.__path__ |
---|
943 | n/a | except AttributeError: |
---|
944 | n/a | msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent) |
---|
945 | n/a | raise ModuleNotFoundError(msg, name=name) from None |
---|
946 | n/a | spec = _find_spec(name, path) |
---|
947 | n/a | if spec is None: |
---|
948 | n/a | raise ModuleNotFoundError(_ERR_MSG.format(name), name=name) |
---|
949 | n/a | else: |
---|
950 | n/a | module = _load_unlocked(spec) |
---|
951 | n/a | if parent: |
---|
952 | n/a | # Set the module as an attribute on its parent. |
---|
953 | n/a | parent_module = sys.modules[parent] |
---|
954 | n/a | setattr(parent_module, name.rpartition('.')[2], module) |
---|
955 | n/a | return module |
---|
956 | n/a | |
---|
957 | n/a | |
---|
958 | n/a | def _find_and_load(name, import_): |
---|
959 | n/a | """Find and load the module, and release the import lock.""" |
---|
960 | n/a | with _ModuleLockManager(name): |
---|
961 | n/a | return _find_and_load_unlocked(name, import_) |
---|
962 | n/a | |
---|
963 | n/a | |
---|
964 | n/a | def _gcd_import(name, package=None, level=0): |
---|
965 | n/a | """Import and return the module based on its name, the package the call is |
---|
966 | n/a | being made from, and the level adjustment. |
---|
967 | n/a | |
---|
968 | n/a | This function represents the greatest common denominator of functionality |
---|
969 | n/a | between import_module and __import__. This includes setting __package__ if |
---|
970 | n/a | the loader did not. |
---|
971 | n/a | |
---|
972 | n/a | """ |
---|
973 | n/a | _sanity_check(name, package, level) |
---|
974 | n/a | if level > 0: |
---|
975 | n/a | name = _resolve_name(name, package, level) |
---|
976 | n/a | _imp.acquire_lock() |
---|
977 | n/a | if name not in sys.modules: |
---|
978 | n/a | return _find_and_load(name, _gcd_import) |
---|
979 | n/a | module = sys.modules[name] |
---|
980 | n/a | if module is None: |
---|
981 | n/a | _imp.release_lock() |
---|
982 | n/a | message = ('import of {} halted; ' |
---|
983 | n/a | 'None in sys.modules'.format(name)) |
---|
984 | n/a | raise ModuleNotFoundError(message, name=name) |
---|
985 | n/a | _lock_unlock_module(name) |
---|
986 | n/a | return module |
---|
987 | n/a | |
---|
988 | n/a | |
---|
989 | n/a | def _handle_fromlist(module, fromlist, import_): |
---|
990 | n/a | """Figure out what __import__ should return. |
---|
991 | n/a | |
---|
992 | n/a | The import_ parameter is a callable which takes the name of module to |
---|
993 | n/a | import. It is required to decouple the function from assuming importlib's |
---|
994 | n/a | import implementation is desired. |
---|
995 | n/a | |
---|
996 | n/a | """ |
---|
997 | n/a | # The hell that is fromlist ... |
---|
998 | n/a | # If a package was imported, try to import stuff from fromlist. |
---|
999 | n/a | if hasattr(module, '__path__'): |
---|
1000 | n/a | if '*' in fromlist: |
---|
1001 | n/a | fromlist = list(fromlist) |
---|
1002 | n/a | fromlist.remove('*') |
---|
1003 | n/a | if hasattr(module, '__all__'): |
---|
1004 | n/a | fromlist.extend(module.__all__) |
---|
1005 | n/a | for x in fromlist: |
---|
1006 | n/a | if not hasattr(module, x): |
---|
1007 | n/a | from_name = '{}.{}'.format(module.__name__, x) |
---|
1008 | n/a | try: |
---|
1009 | n/a | _call_with_frames_removed(import_, from_name) |
---|
1010 | n/a | except ModuleNotFoundError as exc: |
---|
1011 | n/a | # Backwards-compatibility dictates we ignore failed |
---|
1012 | n/a | # imports triggered by fromlist for modules that don't |
---|
1013 | n/a | # exist. |
---|
1014 | n/a | if exc.name == from_name: |
---|
1015 | n/a | continue |
---|
1016 | n/a | raise |
---|
1017 | n/a | return module |
---|
1018 | n/a | |
---|
1019 | n/a | |
---|
1020 | n/a | def _calc___package__(globals): |
---|
1021 | n/a | """Calculate what __package__ should be. |
---|
1022 | n/a | |
---|
1023 | n/a | __package__ is not guaranteed to be defined or could be set to None |
---|
1024 | n/a | to represent that its proper value is unknown. |
---|
1025 | n/a | |
---|
1026 | n/a | """ |
---|
1027 | n/a | package = globals.get('__package__') |
---|
1028 | n/a | spec = globals.get('__spec__') |
---|
1029 | n/a | if package is not None: |
---|
1030 | n/a | if spec is not None and package != spec.parent: |
---|
1031 | n/a | _warnings.warn("__package__ != __spec__.parent " |
---|
1032 | n/a | f"({package!r} != {spec.parent!r})", |
---|
1033 | n/a | ImportWarning, stacklevel=3) |
---|
1034 | n/a | return package |
---|
1035 | n/a | elif spec is not None: |
---|
1036 | n/a | return spec.parent |
---|
1037 | n/a | else: |
---|
1038 | n/a | _warnings.warn("can't resolve package from __spec__ or __package__, " |
---|
1039 | n/a | "falling back on __name__ and __path__", |
---|
1040 | n/a | ImportWarning, stacklevel=3) |
---|
1041 | n/a | package = globals['__name__'] |
---|
1042 | n/a | if '__path__' not in globals: |
---|
1043 | n/a | package = package.rpartition('.')[0] |
---|
1044 | n/a | return package |
---|
1045 | n/a | |
---|
1046 | n/a | |
---|
1047 | n/a | def __import__(name, globals=None, locals=None, fromlist=(), level=0): |
---|
1048 | n/a | """Import a module. |
---|
1049 | n/a | |
---|
1050 | n/a | The 'globals' argument is used to infer where the import is occurring from |
---|
1051 | n/a | to handle relative imports. The 'locals' argument is ignored. The |
---|
1052 | n/a | 'fromlist' argument specifies what should exist as attributes on the module |
---|
1053 | n/a | being imported (e.g. ``from module import <fromlist>``). The 'level' |
---|
1054 | n/a | argument represents the package location to import from in a relative |
---|
1055 | n/a | import (e.g. ``from ..pkg import mod`` would have a 'level' of 2). |
---|
1056 | n/a | |
---|
1057 | n/a | """ |
---|
1058 | n/a | if level == 0: |
---|
1059 | n/a | module = _gcd_import(name) |
---|
1060 | n/a | else: |
---|
1061 | n/a | globals_ = globals if globals is not None else {} |
---|
1062 | n/a | package = _calc___package__(globals_) |
---|
1063 | n/a | module = _gcd_import(name, package, level) |
---|
1064 | n/a | if not fromlist: |
---|
1065 | n/a | # Return up to the first dot in 'name'. This is complicated by the fact |
---|
1066 | n/a | # that 'name' may be relative. |
---|
1067 | n/a | if level == 0: |
---|
1068 | n/a | return _gcd_import(name.partition('.')[0]) |
---|
1069 | n/a | elif not name: |
---|
1070 | n/a | return module |
---|
1071 | n/a | else: |
---|
1072 | n/a | # Figure out where to slice the module's name up to the first dot |
---|
1073 | n/a | # in 'name'. |
---|
1074 | n/a | cut_off = len(name) - len(name.partition('.')[0]) |
---|
1075 | n/a | # Slice end needs to be positive to alleviate need to special-case |
---|
1076 | n/a | # when ``'.' not in name``. |
---|
1077 | n/a | return sys.modules[module.__name__[:len(module.__name__)-cut_off]] |
---|
1078 | n/a | else: |
---|
1079 | n/a | return _handle_fromlist(module, fromlist, _gcd_import) |
---|
1080 | n/a | |
---|
1081 | n/a | |
---|
1082 | n/a | def _builtin_from_name(name): |
---|
1083 | n/a | spec = BuiltinImporter.find_spec(name) |
---|
1084 | n/a | if spec is None: |
---|
1085 | n/a | raise ImportError('no built-in module named ' + name) |
---|
1086 | n/a | return _load_unlocked(spec) |
---|
1087 | n/a | |
---|
1088 | n/a | |
---|
1089 | n/a | def _setup(sys_module, _imp_module): |
---|
1090 | n/a | """Setup importlib by importing needed built-in modules and injecting them |
---|
1091 | n/a | into the global namespace. |
---|
1092 | n/a | |
---|
1093 | n/a | As sys is needed for sys.modules access and _imp is needed to load built-in |
---|
1094 | n/a | modules, those two modules must be explicitly passed in. |
---|
1095 | n/a | |
---|
1096 | n/a | """ |
---|
1097 | n/a | global _imp, sys |
---|
1098 | n/a | _imp = _imp_module |
---|
1099 | n/a | sys = sys_module |
---|
1100 | n/a | |
---|
1101 | n/a | # Set up the spec for existing builtin/frozen modules. |
---|
1102 | n/a | module_type = type(sys) |
---|
1103 | n/a | for name, module in sys.modules.items(): |
---|
1104 | n/a | if isinstance(module, module_type): |
---|
1105 | n/a | if name in sys.builtin_module_names: |
---|
1106 | n/a | loader = BuiltinImporter |
---|
1107 | n/a | elif _imp.is_frozen(name): |
---|
1108 | n/a | loader = FrozenImporter |
---|
1109 | n/a | else: |
---|
1110 | n/a | continue |
---|
1111 | n/a | spec = _spec_from_module(module, loader) |
---|
1112 | n/a | _init_module_attrs(spec, module) |
---|
1113 | n/a | |
---|
1114 | n/a | # Directly load built-in modules needed during bootstrap. |
---|
1115 | n/a | self_module = sys.modules[__name__] |
---|
1116 | n/a | for builtin_name in ('_warnings',): |
---|
1117 | n/a | if builtin_name not in sys.modules: |
---|
1118 | n/a | builtin_module = _builtin_from_name(builtin_name) |
---|
1119 | n/a | else: |
---|
1120 | n/a | builtin_module = sys.modules[builtin_name] |
---|
1121 | n/a | setattr(self_module, builtin_name, builtin_module) |
---|
1122 | n/a | |
---|
1123 | n/a | # Directly load the _thread module (needed during bootstrap). |
---|
1124 | n/a | try: |
---|
1125 | n/a | thread_module = _builtin_from_name('_thread') |
---|
1126 | n/a | except ImportError: |
---|
1127 | n/a | # Python was built without threads |
---|
1128 | n/a | thread_module = None |
---|
1129 | n/a | setattr(self_module, '_thread', thread_module) |
---|
1130 | n/a | |
---|
1131 | n/a | # Directly load the _weakref module (needed during bootstrap). |
---|
1132 | n/a | weakref_module = _builtin_from_name('_weakref') |
---|
1133 | n/a | setattr(self_module, '_weakref', weakref_module) |
---|
1134 | n/a | |
---|
1135 | n/a | |
---|
1136 | n/a | def _install(sys_module, _imp_module): |
---|
1137 | n/a | """Install importlib as the implementation of import.""" |
---|
1138 | n/a | _setup(sys_module, _imp_module) |
---|
1139 | n/a | |
---|
1140 | n/a | sys.meta_path.append(BuiltinImporter) |
---|
1141 | n/a | sys.meta_path.append(FrozenImporter) |
---|
1142 | n/a | |
---|
1143 | n/a | global _bootstrap_external |
---|
1144 | n/a | import _frozen_importlib_external |
---|
1145 | n/a | _bootstrap_external = _frozen_importlib_external |
---|
1146 | n/a | _frozen_importlib_external._install(sys.modules[__name__]) |
---|