Skip to content

Commit

Permalink
reload: fix issue with reloading modules within subpackage
Browse files Browse the repository at this point in the history
sopel-irl#1314 used recursion to reload nested modules. However, in Python 2 (maybe python 3 as well), this approach leads to odd behavior with callables being unloaded/reloaded twice. The loop detects my_module.my_callable and my_callable as two separate modules to reload. The first time it detects it, the rule is added to bot._callables as a string.
I combined patched dgw branch 1056-no-dupes onto 1056-fix-with-recursion, to provide a more optimal solution to issue #1056.
  • Loading branch information
HumorBaby committed Feb 1, 2019
1 parent 5e8f2f7 commit f1956f8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions sopel/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,13 @@ def clean_callable(func, config):
func.rule = getattr(func, 'rule', [])
for command in getattr(func, 'commands', []):
regexp = get_command_regexp(prefix, command)
func.rule.append(regexp)
if regexp not in func.rule:
# TODO: Maybe func.rule should be a set() instead?
func.rule.append(regexp)
for command in getattr(func, 'nickname_commands', []):
regexp = get_nickname_command_regexp(nick, command, alias_nicks)
func.rule.append(regexp)
if regexp not in func.rule:
func.rule.append(regexp)
if hasattr(func, 'example'):
example = func.example[0]["example"]
example = example.replace('$nickname', nick)
Expand Down
4 changes: 2 additions & 2 deletions sopel/modules/reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def f_reload(bot, trigger):

return bot.reply('done')

if (name not in sys.modules and name not in sopel.loader.enumerate_modules(bot.config)):
if name not in sys.modules or name not in sopel.loader.enumerate_modules(bot.config):
return bot.reply('"%s" not loaded, try the `load` command' % name)

reload_module_tree(bot, name)
Expand All @@ -66,7 +66,7 @@ def reload_module_tree(bot, name, seen=None, silent=False):

old_callables = {}
for obj_name, obj in iteritems(vars(old_module)):
if callable(obj):
if callable(obj) and '.' not in name:
bot.unregister(obj)
elif (type(obj) is ModuleType and
obj.__name__.startswith(name + '.') and
Expand Down

0 comments on commit f1956f8

Please sign in to comment.