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

Method _maybe_import_module

Lib/_pyrepl/_module_completer.py:273–286  ·  view source on GitHub ↗
(self, fqname: str)

Source from the content-addressed store, hash-verified

271 return self._global_cache
272
273 def _maybe_import_module(self, fqname: str) -> ModuleType | None:
274 if any(pattern.fullmatch(fqname) for pattern in AUTO_IMPORT_DENYLIST):
275 # Special-cased modules with known import side-effects
276 return None
277 root = fqname.split(".")[0]
278 mod_info = next((m for m in self.global_cache if m.name == root), None)
279 if not mod_info or not self._is_stdlib_module(mod_info):
280 # Only import stdlib modules (no risk of import side-effects)
281 return None
282 try:
283 return importlib.import_module(fqname)
284 except Exception:
285 sys.modules.pop(fqname, None) # Clean half-imported module
286 return None
287
288 def _get_import_completion_action(self, path: str) -> CompletionAction:
289 prompt = ("[ module not imported, press again to import it "

Callers 2

test_no_side_effectsMethod · 0.95
_find_attributesMethod · 0.95

Calls 5

_is_stdlib_moduleMethod · 0.95
anyFunction · 0.50
splitMethod · 0.45
import_moduleMethod · 0.45
popMethod · 0.45

Tested by 1

test_no_side_effectsMethod · 0.76