def ast_from_module_name(self, modname, context_file=None):
if modname in self.astroid_cache:<EOL><INDENT>return self.astroid_cache[modname]<EOL><DEDENT>if modname == "<STR_LIT:__main__>":<EOL><INDENT>return self._build_stub_module(modname)<EOL><DEDENT>old_cwd = os.getcwd()<EOL>if context_file:<EOL><INDENT>os.chdir(os.path.dirname(context_file))<EOL><DEDENT>try:<EOL><INDENT>found_spec = self.file_from_module_name(modname, context_file)<EOL>if found_spec.type == spec.ModuleType.PY_ZIPMODULE:<EOL><INDENT>module = self.zip_import_data(found_spec.location)<EOL>if module is not None:<EOL><INDENT>return module<EOL><DEDENT><DEDENT>elif found_spec.type in (<EOL>spec.ModuleType.C_BUILTIN,<EOL>spec.ModuleType.C_EXTENSION,<EOL>):<EOL><INDENT>if (<EOL>found_spec.type == spec.ModuleType.C_EXTENSION<EOL>and not self._can_load_extension(modname)<EOL>):<EOL><INDENT>return self._build_stub_module(modname)<EOL><DEDENT>try:<EOL><INDENT>module = modutils.load_module_from_name(modname)<EOL><DEDENT>except Exception as ex:<EOL><INDENT>raise exceptions.AstroidImportError(<EOL>"<STR_LIT>",<EOL>modname=modname,<EOL>path=found_spec.location,<EOL>) from ex<EOL><DEDENT>return self.ast_from_module(module, modname)<EOL><DEDENT>elif found_spec.type == spec.ModuleType.PY_COMPILED:<EOL><INDENT>raise exceptions.AstroidImportError(<EOL>"<STR_LIT>",<EOL>modname=modname,<EOL>path=found_spec.location,<EOL>)<EOL><DEDENT>elif found_spec.type == spec.ModuleType.PY_NAMESPACE:<EOL><INDENT>return self._build_namespace_module(<EOL>modname, found_spec.submodule_search_locations<EOL>)<EOL><DEDENT>if found_spec.location is None:<EOL><INDENT>raise exceptions.AstroidImportError(<EOL>"<STR_LIT>", modname=modname<EOL>)<EOL><DEDENT>return self.ast_from_file(found_spec.location, modname, fallback=False)<EOL><DEDENT>except exceptions.AstroidBuildingError as e:<EOL><INDENT>for hook in self._failed_import_hooks:<EOL><INDENT>try:<EOL><INDENT>return hook(modname)<EOL><DEDENT>except exceptions.AstroidBuildingError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>raise e<EOL><DEDENT>finally:<EOL><INDENT>os.chdir(old_cwd)<EOL><DEDENT>
given a module name, return the astroid object