ยปCore Development>Code coverage>Lib/importlib/_bootstrap.py

Python code coverage for Lib/importlib/_bootstrap.py

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