From 36ca482e4a92e988028c212997b862b64fa3b59b Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Mon, 20 May 2013 02:03:19 +0300 Subject: [PATCH] Unregister and unbind modules from willie.bot when they are reloaded. -Adds methods unregister and is_callable to willie.bot. -Adds code required to remove references to willie callables, in modules themselves and willie, to willie.modules.reload. --- reload.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/reload.py b/reload.py index 28306bcc36..c6566135c2 100644 --- a/reload.py +++ b/reload.py @@ -32,8 +32,22 @@ def f_reload(willie, trigger): if not name in sys.modules: return willie.reply('%s: no such module!' % name) + old_module = sys.modules[name] + + old_callables = {} + for obj_name, obj in vars(old_module).iteritems(): + if willie.is_callable(obj): + old_callables[obj_name] = obj + + willie.unregister(old_callables) + # Also remove all references to willie callables from top level of the + # module, so that they will not get loaded again if reloading the + # module does not override them. + for obj_name in old_callables.keys(): + delattr(old_module, obj_name) + # Thanks to moot for prodding me on this - path = sys.modules[name].__file__ + path = old_module.__file__ if path.endswith('.pyc') or path.endswith('.pyo'): path = path[:-1] if not os.path.isfile(path):