def _InternalImportModule(self, name, deferredImports, namespace = False):
"""Internal method used for importing a module which assumes that the
name given is an absolute name. None is returned if the module
cannot be found."""
try:
# Check in module cache before trying to import it again.
return self._modules[name]
except KeyError:
pass
if name in self._builtinModules:
module = self._AddModule(name)
logging.debug("Adding module [%s] [C_BUILTIN]", name)
self._RunHook("load", module.name, module)
module.inImport = False
return module
pos = name.rfind(".")
if pos < 0: # Top-level module
path = self.path
searchName = name
parentModule = None
else: # Dotted module name - look up the parent module
parentName = name[:pos]
parentModule = \
self._InternalImportModule(parentName, deferredImports,
namespace = namespace)
if parentModule is None:
return None
if namespace:
parentModule.ExtendPath()
path = parentModule.path
searchName = name[pos + 1:]
if name in self.aliases:
actualName = self.aliases[name]
module = self._InternalImportModule(actualName, deferredImports)
self._modules[name] = module
return module
try:
fp, path, info = self._FindModule(searchName, path, namespace)
if info[-1] == imp.C_BUILTIN and parentModule is not None:
return None
module = self._LoadModule(name, fp, path, info, deferredImports,
parentModule, namespace)
except ImportError:
logging.debug("Module [%s] cannot be imported", name)
self._modules[name] = None
return None
return module
评论列表
文章目录