Catch exception during module loading: just skip the module registration

This commit is contained in:
nemunaire 2015-06-02 22:24:45 +02:00
parent 91688875d4
commit c1858fff3a
3 changed files with 15 additions and 12 deletions

View File

@ -385,14 +385,16 @@ class Bot(threading.Thread):
module.__nemubot_context__.add_hook(s, h)
nemubot.hooks.last_registered = []
# Save a reference to the module
self.modules[module_name] = module
# Launch the module
if hasattr(module, "load"):
module.load(module.__nemubot_context__)
try:
module.load(module.__nemubot_context__)
except:
module.__nemubot_context__.unload()
raise
return True
# Save a reference to the module
self.modules[module_name] = module
def unload_module(self, name):

View File

@ -57,12 +57,8 @@ class ModuleLoader(SourceFileLoader):
def _load(self, module, name):
# Add the module to the global modules list
if self.add_module(module):
logger.info("Module '%s' successfully loaded.", name)
else:
logger.error("An error occurs while importing `%s'.", name)
raise ImportError("An error occurs while importing `%s'."
% name)
self.add_module(module)
logger.info("Module '%s' successfully loaded.", name)
return module

View File

@ -117,7 +117,12 @@ def load_file(filename, context):
for mod in config.getNodes("module"):
context.modules_configuration[mod["name"]] = mod
if get_boolean(mod, "autoload", default=True):
__import__(mod["name"])
try:
__import__(mod["name"])
except:
logger.exception("Exception occurs when loading module"
" '%s'", mod["name"])
# Load files asked by the configuration file
for load in config.getNodes("include"):