| 952 | |
| 953 | @classmethod |
| 954 | def _fix_up_module(cls, module): |
| 955 | spec = module.__spec__ |
| 956 | state = spec.loader_state |
| 957 | if state is None: |
| 958 | # The module is missing FrozenImporter-specific values. |
| 959 | |
| 960 | # Fix up the spec attrs. |
| 961 | origname = vars(module).pop('__origname__', None) |
| 962 | assert origname, 'see PyImport_ImportFrozenModuleObject()' |
| 963 | ispkg = hasattr(module, '__path__') |
| 964 | assert _imp.is_frozen_package(module.__name__) == ispkg, ispkg |
| 965 | filename, pkgdir = cls._resolve_filename(origname, spec.name, ispkg) |
| 966 | spec.loader_state = type(sys.implementation)( |
| 967 | filename=filename, |
| 968 | origname=origname, |
| 969 | ) |
| 970 | __path__ = spec.submodule_search_locations |
| 971 | if ispkg: |
| 972 | assert __path__ == [], __path__ |
| 973 | if pkgdir: |
| 974 | spec.submodule_search_locations.insert(0, pkgdir) |
| 975 | else: |
| 976 | assert __path__ is None, __path__ |
| 977 | |
| 978 | # Fix up the module attrs (the bare minimum). |
| 979 | assert not hasattr(module, '__file__'), module.__file__ |
| 980 | if filename: |
| 981 | try: |
| 982 | module.__file__ = filename |
| 983 | except AttributeError: |
| 984 | pass |
| 985 | if ispkg: |
| 986 | if module.__path__ != __path__: |
| 987 | assert module.__path__ == [], module.__path__ |
| 988 | module.__path__.extend(__path__) |
| 989 | else: |
| 990 | # These checks ensure that _fix_up_module() is only called |
| 991 | # in the right places. |
| 992 | __path__ = spec.submodule_search_locations |
| 993 | ispkg = __path__ is not None |
| 994 | # Check the loader state. |
| 995 | assert sorted(vars(state)) == ['filename', 'origname'], state |
| 996 | if state.origname: |
| 997 | # The only frozen modules with "origname" set are stdlib modules. |
| 998 | (__file__, pkgdir, |
| 999 | ) = cls._resolve_filename(state.origname, spec.name, ispkg) |
| 1000 | assert state.filename == __file__, (state.filename, __file__) |
| 1001 | if pkgdir: |
| 1002 | assert __path__ == [pkgdir], (__path__, pkgdir) |
| 1003 | else: |
| 1004 | assert __path__ == ([] if ispkg else None), __path__ |
| 1005 | else: |
| 1006 | __file__ = None |
| 1007 | assert state.filename is None, state.filename |
| 1008 | assert __path__ == ([] if ispkg else None), __path__ |
| 1009 | # Check the file attrs. |
| 1010 | if __file__: |
| 1011 | assert hasattr(module, '__file__') |