From dd9ca73c5b47502b52efdedf450bb1b2d78ec060 2012-05-26 03:20:02 From: Fernando Perez Date: 2012-05-26 03:20:02 Subject: [PATCH] Fix %pylab magic. For a clean solution, added a .registry attribute to magics manager that will record all magic instances recorded. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 352bbda..961c4ba 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2004,9 +2004,9 @@ class InteractiveShell(SingletonConfigurable): self.function_as_magic = self.magics_manager.function_as_magic self.define_magic = self.magics_manager._define_magic - self.register_magics(mf.BasicMagics, mf.CodeMagics, - mf.ConfigMagics, mf.NamespaceMagics, mf.ExecutionMagics, - mf.AutoMagics, mf.OSMagics, mf.LoggingMagics, mf.ExtensionsMagics, + self.register_magics(mf.BasicMagics, mf.CodeMagics, mf.ConfigMagics, + mf.ExecutionMagics, mf.NamespaceMagics, mf.AutoMagics, + mf.OSMagics, mf.LoggingMagics, mf.ExtensionsMagics, mf.PylabMagics, mf.DeprecatedMagics) # FIXME: Move the color initialization to the DisplayHook, which @@ -2695,7 +2695,8 @@ class InteractiveShell(SingletonConfigurable): # Now we must activate the gui pylab wants to use, and fix %run to take # plot updates into account self.enable_gui(gui) - self._magic.default_runner = mpl_runner(self.safe_execfile) + self.magics_manager.registry['ExecutionMagics'].default_runner = \ + mpl_runner(self.safe_execfile) #------------------------------------------------------------------------- # Utilities diff --git a/IPython/core/magic.py b/IPython/core/magic.py index b90eb5a..3f48202 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -144,8 +144,15 @@ class MagicsManager(Configurable): """Object that handles all magic-related functionality for IPython. """ # Non-configurable class attributes + + # A two-level dict, first keyed by magic type, then by magic function, and + # holding the actual callable object as value. This is the dict used for + # magic function dispatch magics = Dict + # A registry of the original objects that we've been given holding magics. + registry = Dict + shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') auto_magic = Bool @@ -161,6 +168,9 @@ class MagicsManager(Configurable): super(MagicsManager, self).__init__(shell=shell, config=config, user_magics=user_magics, **traits) self.magics = dict(line={}, cell={}) + # Let's add the user_magics to the registry for uniformity, so *all* + # registered magic containers can be found there. + self.registry[user_magics.__class__.__name__] = user_magics def auto_status(self): """Return descriptive string with automagic status.""" @@ -187,6 +197,9 @@ class MagicsManager(Configurable): # If we're given an uninstantiated class m = m(self.shell) + # Now that we have an instance, we can register it and update the + # table of callables + self.registry[m.__class__.__name__] = m for mtype in magic_types: self.magics[mtype].update(m.magics[mtype])