MCPcopy Index your code
hub / github.com/python/cpython / _ensure_module

Function _ensure_module

Lib/test/support/import_helper.py:381–407  ·  view source on GitHub ↗
(name, ispkg, addparent, clearnone)

Source from the content-addressed store, hash-verified

379
380
381def _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
410def _add_module(spec, ispkg, addparent):

Callers 2

_add_moduleFunction · 0.85
ensure_module_importedFunction · 0.85

Calls 2

_add_moduleFunction · 0.85
import_moduleMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…