Skip to content

Commit

Permalink
Merge pull request #1964 from minrk/magics_class
Browse files Browse the repository at this point in the history
allow multiple instances of a Magic by tracking all instances of any Magics subclass rather than having a mapping that only tracks the first instance.
  • Loading branch information
fperez committed Jun 20, 2012
2 parents b7e8ad8 + 139d6c0 commit 6331c3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 10 additions & 4 deletions IPython/core/magic.py
Expand Up @@ -469,13 +469,19 @@ def __init__(self, shell):
# grab. Only now, that the instance exists, can we create the proper
# mapping to bound methods. So we read the info off the original names
# table and replace each method name by the actual bound method.
# But we mustn't clobber the *class* mapping, in case of multiple instances.
class_magics = self.magics
self.magics = {}
for mtype in magic_kinds:
tab = self.magics[mtype]
# must explicitly use keys, as we're mutating this puppy
for magic_name in tab.keys():
meth_name = tab[magic_name]
tab = self.magics[mtype] = {}
cls_tab = class_magics[mtype]
for magic_name, meth_name in cls_tab.iteritems():
if isinstance(meth_name, basestring):
# it's a method name, grab it
tab[magic_name] = getattr(self, meth_name)
else:
# it's the real thing
tab[magic_name] = meth_name

def arg_err(self,func):
"""Print docstring if incorrect arguments were passed"""
Expand Down
11 changes: 11 additions & 0 deletions IPython/core/tests/test_magic.py
Expand Up @@ -711,3 +711,14 @@ def test_line_cell_info():
nt.assert_true(oinfo['found'])
nt.assert_true(oinfo['ismagic'])
nt.assert_equals(oinfo['docstring'], FooFoo.line_foo.__doc__)

def test_multiple_magics():
ip = get_ipython()
foo1 = FooFoo(ip)
foo2 = FooFoo(ip)
mm = ip.magics_manager
mm.register(foo1)
nt.assert_true(mm.magics['line']['foo'].im_self is foo1)
mm.register(foo2)
nt.assert_true(mm.magics['line']['foo'].im_self is foo2)

0 comments on commit 6331c3d

Please sign in to comment.