Find a module's spec.
(name, path, target=None)
| 1052 | |
| 1053 | |
| 1054 | def _find_spec(name, path, target=None): |
| 1055 | """Find a module's spec.""" |
| 1056 | meta_path = sys.meta_path |
| 1057 | if meta_path is None: |
| 1058 | # PyImport_Cleanup() is running or has been called. |
| 1059 | raise ImportError("sys.meta_path is None, Python is likely " |
| 1060 | "shutting down") |
| 1061 | |
| 1062 | if not meta_path: |
| 1063 | _warnings.warn('sys.meta_path is empty', ImportWarning) |
| 1064 | |
| 1065 | # We check sys.modules here for the reload case. While a passed-in |
| 1066 | # target will usually indicate a reload there is no guarantee, whereas |
| 1067 | # sys.modules provides one. |
| 1068 | is_reload = name in sys.modules |
| 1069 | for finder in meta_path: |
| 1070 | with _ImportLockContext(): |
| 1071 | try: |
| 1072 | find_spec = finder.find_spec |
| 1073 | except AttributeError: |
| 1074 | spec = _find_spec_legacy(finder, name, path) |
| 1075 | if spec is None: |
| 1076 | continue |
| 1077 | else: |
| 1078 | spec = find_spec(name, path, target) |
| 1079 | if spec is not None: |
| 1080 | # The parent import may have already imported this module. |
| 1081 | if not is_reload and name in sys.modules: |
| 1082 | module = sys.modules[name] |
| 1083 | try: |
| 1084 | __spec__ = module.__spec__ |
| 1085 | except AttributeError: |
| 1086 | # We use the found spec since that is the one that |
| 1087 | # we would have used if the parent module hadn't |
| 1088 | # beaten us to the punch. |
| 1089 | return spec |
| 1090 | else: |
| 1091 | if __spec__ is None: |
| 1092 | return spec |
| 1093 | else: |
| 1094 | return __spec__ |
| 1095 | else: |
| 1096 | return spec |
| 1097 | else: |
| 1098 | return None |
| 1099 | |
| 1100 | |
| 1101 | def _sanity_check(name, package, level): |
no test coverage detected