Find a module's spec.
(name, path, target=None)
| 1241 | |
| 1242 | |
| 1243 | def _find_spec(name, path, target=None): |
| 1244 | """Find a module's spec.""" |
| 1245 | meta_path = sys.meta_path |
| 1246 | if meta_path is None: |
| 1247 | raise ImportError("sys.meta_path is None, Python is likely " |
| 1248 | "shutting down") |
| 1249 | |
| 1250 | # gh-130094: Copy sys.meta_path so that we have a consistent view of the |
| 1251 | # list while iterating over it. |
| 1252 | meta_path = list(meta_path) |
| 1253 | if not meta_path: |
| 1254 | _warnings.warn('sys.meta_path is empty', ImportWarning) |
| 1255 | |
| 1256 | # We check sys.modules here for the reload case. While a passed-in |
| 1257 | # target will usually indicate a reload there is no guarantee, whereas |
| 1258 | # sys.modules provides one. |
| 1259 | is_reload = name in sys.modules |
| 1260 | for finder in meta_path: |
| 1261 | with _ImportLockContext(): |
| 1262 | try: |
| 1263 | find_spec = finder.find_spec |
| 1264 | except AttributeError: |
| 1265 | continue |
| 1266 | else: |
| 1267 | spec = find_spec(name, path, target) |
| 1268 | if spec is not None: |
| 1269 | # The parent import may have already imported this module. |
| 1270 | if not is_reload and name in sys.modules: |
| 1271 | module = sys.modules[name] |
| 1272 | try: |
| 1273 | __spec__ = module.__spec__ |
| 1274 | except AttributeError: |
| 1275 | # We use the found spec since that is the one that |
| 1276 | # we would have used if the parent module hadn't |
| 1277 | # beaten us to the punch. |
| 1278 | return spec |
| 1279 | else: |
| 1280 | if __spec__ is None: |
| 1281 | return spec |
| 1282 | else: |
| 1283 | return __spec__ |
| 1284 | else: |
| 1285 | return spec |
| 1286 | else: |
| 1287 | return None |
| 1288 | |
| 1289 | |
| 1290 | def _sanity_check(name, package, level): |
no test coverage detected