diff --git a/IPython/Extensions/ipy_autoreload.py b/IPython/Extensions/ipy_autoreload.py index 1e1d410..a171c91 100644 --- a/IPython/Extensions/ipy_autoreload.py +++ b/IPython/Extensions/ipy_autoreload.py @@ -129,21 +129,26 @@ reloader = ModuleReloader() import IPython.iplib -if 'runcode_old' not in globals(): - # safeguard against reloading *this* module - runcode_old = IPython.iplib.InteractiveShell.runcode +autoreload_enabled = False + +def runcode_hook(self): + if not autoreload_enabled: + raise IPython.ipapi.TryNext + try: + reloader.check() + except: + pass -def runcode_new(*a, **kw): - try: reloader.check() - except: pass - return runcode_old(*a, **kw) def enable_autoreload(): - IPython.iplib.InteractiveShell.runcode = runcode_new + global autoreload_enabled + autoreload_enabled = True + def disable_autoreload(): - IPython.iplib.InteractiveShell.runcode = runcode_old - + global autoreload_enabled + autoreload_enabled = False + #------------------------------------------------------------------------------ # IPython connectivity #------------------------------------------------------------------------------ @@ -226,5 +231,9 @@ def aimport_f(self, parameter_s=''): mod = __import__(modname) ip.to_user_ns({modname: mod}) -ip.expose_magic('autoreload', autoreload_f) -ip.expose_magic('aimport', aimport_f) +def init(): + ip.expose_magic('autoreload', autoreload_f) + ip.expose_magic('aimport', aimport_f) + ip.set_hook('pre_runcode_hook', runcode_hook) + +init() \ No newline at end of file diff --git a/IPython/hooks.py b/IPython/hooks.py index 348e3e8..777f308 100644 --- a/IPython/hooks.py +++ b/IPython/hooks.py @@ -56,7 +56,7 @@ from pprint import PrettyPrinter __all__ = ['editor', 'fix_error_editor', 'result_display', 'input_prefilter', 'shutdown_hook', 'late_startup_hook', 'generate_prompt', 'generate_output_prompt','shell_hook', - 'show_in_pager','pre_prompt_hook'] + 'show_in_pager','pre_prompt_hook', 'pre_runcode_hook'] pformat = PrettyPrinter().pformat @@ -236,7 +236,8 @@ def pre_prompt_hook(self): return None -def post_command_hook(self,cmd): - """ Executed after executing a command """ +def pre_runcode_hook(self): + """ Executed before running the (prefiltered) code in IPython """ + return None diff --git a/IPython/iplib.py b/IPython/iplib.py index b63ac4e..a737855 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -2056,6 +2056,7 @@ want to merge them back into the new files.""" % locals() outflag = 1 # happens in more places, so it's easier as default try: try: + self.hooks.pre_runcode_hook() # Embedded instances require separate global/local namespaces # so they can see both the surrounding (local) namespace and # the module-level globals when called inside another function.