Skip to content

Commit

Permalink
fix module removal
Browse files Browse the repository at this point in the history
  • Loading branch information
calebj committed Aug 20, 2018
1 parent dc2c4c9 commit 6d66ab0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def unregister(self, callables, jobs, shutdowns, urls):
self.memory['url_callbacks'].pop(func.url_regex, None)

def unregister_module(self, module):
relevant_parts = sopel.loader.clean_module(module, self.config)
relevant_parts = sopel.loader.clean_module(module, self.config, modify=False)
self.unregister(*relevant_parts)

if hasattr(module, "teardown"):
Expand Down
13 changes: 8 additions & 5 deletions sopel/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def is_triggerable(obj):
return any(hasattr(obj, attr) for attr in ('rule', 'intents', 'commands', 'nickname_commands'))


def clean_module(module, config):
def clean_module(module, config, modify=True):
callables = []
shutdowns = []
jobs = []
Expand All @@ -222,10 +222,12 @@ def clean_module(module, config):
if getattr(obj, '__name__', None) == 'shutdown':
shutdowns.append(obj)
elif is_triggerable(obj):
clean_callable(obj, config)
if modify:
clean_callable(obj, config)
callables.append(obj)
elif hasattr(obj, 'interval'):
clean_callable(obj, config)
if modify:
clean_callable(obj, config)
jobs.append(obj)
elif hasattr(obj, 'url_regex'):
urls.append(obj)
Expand Down Expand Up @@ -271,10 +273,11 @@ def trace_reload(module, depth): # recursive
reload_list = sorted(for_reload, reverse=True, key=lambda k: for_reload[k])
not_reloaded = dict()

for module in reload_list:
if pre_reload is not None:
if pre_reload is not None:
for module in reload_list:
pre_reload(module)

for module in reload_list:
try:
_reload(module)
except Exception: # catch and write all errors
Expand Down
8 changes: 7 additions & 1 deletion sopel/modules/reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def pre_reload(module):
if module.__name__ == name or module.__name__.startswith(name + '.'):
bot.unregister_module(module)

try:
del sys.modules[module.__name__]
except Exception:
pass

try:
bot.unregister_module(module)
reload_all(module, pre_reload=pre_reload, reload_if=reload_check)
Expand Down Expand Up @@ -155,7 +160,7 @@ def f_unload(bot, trigger):
failed_names = []

if not name:
return bot.reply('Reload what?')
return bot.reply('Unload what?')
elif name == bot.config.core.owner:
return bot.reply('What?')

Expand All @@ -166,6 +171,7 @@ def f_unload(bot, trigger):
else:
try:
bot.unregister_module(module)
ok_names.append(name)
except Exception as e:
filename, lineno = get_raising_file_and_line()
rel_path = os.path.relpath(filename, os.path.dirname(__file__))
Expand Down

0 comments on commit 6d66ab0

Please sign in to comment.