(spec, module, *, override=False)
| 711 | |
| 712 | |
| 713 | def _init_module_attrs(spec, module, *, override=False): |
| 714 | # The passed-in module may be not support attribute assignment, |
| 715 | # in which case we simply don't set the attributes. |
| 716 | # __name__ |
| 717 | if (override or getattr(module, '__name__', None) is None): |
| 718 | try: |
| 719 | module.__name__ = spec.name |
| 720 | except AttributeError: |
| 721 | pass |
| 722 | # __loader__ |
| 723 | if override or getattr(module, '__loader__', None) is None: |
| 724 | loader = spec.loader |
| 725 | if loader is None: |
| 726 | # A backward compatibility hack. |
| 727 | if spec.submodule_search_locations is not None: |
| 728 | if _bootstrap_external is None: |
| 729 | raise NotImplementedError |
| 730 | NamespaceLoader = _bootstrap_external.NamespaceLoader |
| 731 | |
| 732 | loader = NamespaceLoader.__new__(NamespaceLoader) |
| 733 | loader._path = spec.submodule_search_locations |
| 734 | spec.loader = loader |
| 735 | # While the docs say that module.__file__ is not set for |
| 736 | # built-in modules, and the code below will avoid setting it if |
| 737 | # spec.has_location is false, this is incorrect for namespace |
| 738 | # packages. Namespace packages have no location, but their |
| 739 | # __spec__.origin is None, and thus their module.__file__ |
| 740 | # should also be None for consistency. While a bit of a hack, |
| 741 | # this is the best place to ensure this consistency. |
| 742 | # |
| 743 | # See bpo-32305 |
| 744 | module.__file__ = None |
| 745 | try: |
| 746 | module.__loader__ = loader |
| 747 | except AttributeError: |
| 748 | pass |
| 749 | # __package__ |
| 750 | if override or getattr(module, '__package__', None) is None: |
| 751 | try: |
| 752 | module.__package__ = spec.parent |
| 753 | except AttributeError: |
| 754 | pass |
| 755 | # __spec__ |
| 756 | try: |
| 757 | module.__spec__ = spec |
| 758 | except AttributeError: |
| 759 | pass |
| 760 | # __path__ |
| 761 | if override or getattr(module, '__path__', None) is None: |
| 762 | if spec.submodule_search_locations is not None: |
| 763 | # XXX We should extend __path__ if it's already a list. |
| 764 | try: |
| 765 | module.__path__ = spec.submodule_search_locations |
| 766 | except AttributeError: |
| 767 | pass |
| 768 | # __file__ |
| 769 | if spec.has_location: |
| 770 | if override or getattr(module, '__file__', None) is None: |
no test coverage detected
searching dependent graphs…