Find a module's spec.
(name, path, target=None)
| 1138 | |
| 1139 | |
| 1140 | def _find_spec(name, path, target=None): |
| 1141 | """Find a module's spec.""" |
| 1142 | meta_path = sys.meta_path |
| 1143 | if meta_path is None: |
| 1144 | raise ImportError("sys.meta_path is None, Python is likely " |
| 1145 | "shutting down") |
| 1146 | |
| 1147 | # gh-130094: Copy sys.meta_path so that we have a consistent view of the |
| 1148 | # list while iterating over it. |
| 1149 | meta_path = list(meta_path) |
| 1150 | if not meta_path: |
| 1151 | _warnings.warn('sys.meta_path is empty', ImportWarning) |
| 1152 | |
| 1153 | # We check sys.modules here for the reload case. While a passed-in |
| 1154 | # target will usually indicate a reload there is no guarantee, whereas |
| 1155 | # sys.modules provides one. |
| 1156 | is_reload = name in sys.modules |
| 1157 | for finder in meta_path: |
| 1158 | with _ImportLockContext(): |
| 1159 | try: |
| 1160 | find_spec = finder.find_spec |
| 1161 | except AttributeError: |
| 1162 | continue |
| 1163 | else: |
| 1164 | spec = find_spec(name, path, target) |
| 1165 | if spec is not None: |
| 1166 | # The parent import may have already imported this module. |
| 1167 | if not is_reload and name in sys.modules: |
| 1168 | module = sys.modules[name] |
| 1169 | try: |
| 1170 | __spec__ = module.__spec__ |
| 1171 | except AttributeError: |
| 1172 | # We use the found spec since that is the one that |
| 1173 | # we would have used if the parent module hadn't |
| 1174 | # beaten us to the punch. |
| 1175 | return spec |
| 1176 | else: |
| 1177 | if __spec__ is None: |
| 1178 | return spec |
| 1179 | else: |
| 1180 | return __spec__ |
| 1181 | else: |
| 1182 | return spec |
| 1183 | else: |
| 1184 | return None |
| 1185 | |
| 1186 | |
| 1187 | def _sanity_check(name, package, level): |
no test coverage detected
searching dependent graphs…