(name, ispkg, addparent, clearnone)
| 379 | |
| 380 | |
| 381 | def _ensure_module(name, ispkg, addparent, clearnone): |
| 382 | try: |
| 383 | mod = orig = sys.modules[name] |
| 384 | except KeyError: |
| 385 | mod = orig = None |
| 386 | missing = True |
| 387 | else: |
| 388 | missing = False |
| 389 | if mod is not None: |
| 390 | # It was already imported. |
| 391 | return mod, orig, missing |
| 392 | # Otherwise, None means it was explicitly disabled. |
| 393 | |
| 394 | assert name != '__main__' |
| 395 | if not missing: |
| 396 | assert orig is None, (name, sys.modules[name]) |
| 397 | if not clearnone: |
| 398 | raise ModuleNotFoundError(name) |
| 399 | del sys.modules[name] |
| 400 | # Try normal import, then fall back to adding the module. |
| 401 | try: |
| 402 | mod = importlib.import_module(name) |
| 403 | except ModuleNotFoundError: |
| 404 | if addparent and not clearnone: |
| 405 | addparent = None |
| 406 | mod = _add_module(name, ispkg, addparent) |
| 407 | return mod, orig, missing |
| 408 | |
| 409 | |
| 410 | def _add_module(spec, ispkg, addparent): |
no test coverage detected
searching dependent graphs…