MCPcopy Index your code
hub / github.com/RustPython/RustPython / _bless_my_loader

Function _bless_my_loader

Lib/importlib/_bootstrap_external.py:630–675  ·  view source on GitHub ↗

Helper function for _warnings.c See GH#97850 for details.

(module_globals)

Source from the content-addressed store, hash-verified

628
629
630def _bless_my_loader(module_globals):
631 """Helper function for _warnings.c
632
633 See GH#97850 for details.
634 """
635 # 2022-10-06(warsaw): For now, this helper is only used in _warnings.c and
636 # that use case only has the module globals. This function could be
637 # extended to accept either that or a module object. However, in the
638 # latter case, it would be better to raise certain exceptions when looking
639 # at a module, which should have either a __loader__ or __spec__.loader.
640 # For backward compatibility, it is possible that we'll get an empty
641 # dictionary for the module globals, and that cannot raise an exception.
642 if not isinstance(module_globals, dict):
643 return None
644
645 missing = object()
646 loader = module_globals.get('__loader__', None)
647 spec = module_globals.get('__spec__', missing)
648
649 if loader is None:
650 if spec is missing:
651 # If working with a module:
652 # raise AttributeError('Module globals is missing a __spec__')
653 return None
654 elif spec is None:
655 raise ValueError('Module globals is missing a __spec__.loader')
656
657 spec_loader = getattr(spec, 'loader', missing)
658
659 if spec_loader in (missing, None):
660 if loader is None:
661 exc = AttributeError if spec_loader is missing else ValueError
662 raise exc('Module globals is missing a __spec__.loader')
663 _warnings.warn(
664 'Module globals is missing a __spec__.loader',
665 DeprecationWarning)
666 spec_loader = loader
667
668 assert spec_loader is not None
669 if loader is not None and loader != spec_loader:
670 _warnings.warn(
671 'Module globals; __loader__ != __spec__.loader',
672 DeprecationWarning)
673 return loader
674
675 return spec_loader
676
677
678# Loaders #####################################################################

Callers

nothing calls this directly

Calls 4

isinstanceFunction · 0.85
getattrFunction · 0.85
getMethod · 0.45
warnMethod · 0.45

Tested by

no test coverage detected