From 934bce78d896ece19210c5056bf308606864dd22 2014-03-03 20:30:47 From: Thomas Kluyver Date: 2014-03-03 20:30:47 Subject: [PATCH] Use callbacks system for autoreload --- diff --git a/IPython/extensions/autoreload.py b/IPython/extensions/autoreload.py index 2f0de25..ac68826 100644 --- a/IPython/extensions/autoreload.py +++ b/IPython/extensions/autoreload.py @@ -392,7 +392,6 @@ def superreload(module, reload=reload, old_objects={}): # IPython connectivity #------------------------------------------------------------------------------ -from IPython.core.hooks import TryNext from IPython.core.magic import Magics, magics_class, line_magic @magics_class @@ -491,17 +490,16 @@ class AutoreloadMagics(Magics): # Inject module to user namespace self.shell.push({top_name: top_module}) - def pre_run_code_hook(self, ip): - if not self._reloader.enabled: - raise TryNext - try: - self._reloader.check() - except: - pass + def pre_execute_explicit(self): + if self._reloader.enabled: + try: + self._reloader.check() + except: + pass def load_ipython_extension(ip): """Load the extension in IPython.""" auto_reload = AutoreloadMagics(ip) ip.register_magics(auto_reload) - ip.set_hook('pre_run_code_hook', auto_reload.pre_run_code_hook) + ip.callbacks.register('pre_execute_explicit', auto_reload.pre_execute_explicit) diff --git a/IPython/extensions/tests/test_autoreload.py b/IPython/extensions/tests/test_autoreload.py index 8ada638..036ec40 100644 --- a/IPython/extensions/tests/test_autoreload.py +++ b/IPython/extensions/tests/test_autoreload.py @@ -23,7 +23,7 @@ import nose.tools as nt import IPython.testing.tools as tt from IPython.extensions.autoreload import AutoreloadMagics -from IPython.core.hooks import TryNext +from IPython.core.callbacks import CallbackManager, pre_execute_explicit from IPython.utils.py3compat import PY3 if PY3: @@ -41,15 +41,14 @@ class FakeShell(object): def __init__(self): self.ns = {} + self.callbacks = CallbackManager(self, {'pre_execute_explicit', pre_execute_explicit}) self.auto_magics = AutoreloadMagics(shell=self) + self.callbacks.register('pre_execute_explicit', self.auto_magics.pre_execute_explicit) register_magics = set_hook = noop def run_code(self, code): - try: - self.auto_magics.pre_run_code_hook(self) - except TryNext: - pass + self.callbacks.fire('pre_execute_explicit') exec(code, self.ns) def push(self, items):