| 1036 | |
| 1037 | @classmethod |
| 1038 | def _fix_up_module(cls, module): |
| 1039 | spec = module.__spec__ |
| 1040 | state = spec.loader_state |
| 1041 | if state is None: |
| 1042 | # The module is missing FrozenImporter-specific values. |
| 1043 | |
| 1044 | # Fix up the spec attrs. |
| 1045 | origname = vars(module).pop('__origname__', None) |
| 1046 | assert origname, 'see PyImport_ImportFrozenModuleObject()' |
| 1047 | ispkg = hasattr(module, '__path__') |
| 1048 | assert _imp.is_frozen_package(module.__name__) == ispkg, ispkg |
| 1049 | filename, pkgdir = cls._resolve_filename(origname, spec.name, ispkg) |
| 1050 | spec.loader_state = type(sys.implementation)( |
| 1051 | filename=filename, |
| 1052 | origname=origname, |
| 1053 | ) |
| 1054 | __path__ = spec.submodule_search_locations |
| 1055 | if ispkg: |
| 1056 | assert __path__ == [], __path__ |
| 1057 | if pkgdir: |
| 1058 | spec.submodule_search_locations.insert(0, pkgdir) |
| 1059 | else: |
| 1060 | assert __path__ is None, __path__ |
| 1061 | |
| 1062 | # Fix up the module attrs (the bare minimum). |
| 1063 | assert not hasattr(module, '__file__'), module.__file__ |
| 1064 | if filename: |
| 1065 | try: |
| 1066 | module.__file__ = filename |
| 1067 | except AttributeError: |
| 1068 | pass |
| 1069 | if ispkg: |
| 1070 | if module.__path__ != __path__: |
| 1071 | assert module.__path__ == [], module.__path__ |
| 1072 | module.__path__.extend(__path__) |
| 1073 | else: |
| 1074 | # These checks ensure that _fix_up_module() is only called |
| 1075 | # in the right places. |
| 1076 | __path__ = spec.submodule_search_locations |
| 1077 | ispkg = __path__ is not None |
| 1078 | # Check the loader state. |
| 1079 | assert sorted(vars(state)) == ['filename', 'origname'], state |
| 1080 | if state.origname: |
| 1081 | # The only frozen modules with "origname" set are stdlib modules. |
| 1082 | (__file__, pkgdir, |
| 1083 | ) = cls._resolve_filename(state.origname, spec.name, ispkg) |
| 1084 | assert state.filename == __file__, (state.filename, __file__) |
| 1085 | if pkgdir: |
| 1086 | assert __path__ == [pkgdir], (__path__, pkgdir) |
| 1087 | else: |
| 1088 | assert __path__ == ([] if ispkg else None), __path__ |
| 1089 | else: |
| 1090 | __file__ = None |
| 1091 | assert state.filename is None, state.filename |
| 1092 | assert __path__ == ([] if ispkg else None), __path__ |
| 1093 | # Check the file attrs. |
| 1094 | if __file__: |
| 1095 | assert hasattr(module, '__file__') |