From 92309bb477e9ef2c1f912691285c985c8c35fdb5 Mon Sep 17 00:00:00 2001 From: MinRK Date: Fri, 27 Jul 2012 14:58:55 -0700 Subject: [PATCH] Backport PR #2185: added test for %store, fixed storemagic closes #2099 --- IPython/core/tests/test_magic.py | 19 +++++++++++++++++++ IPython/extensions/storemagic.py | 8 ++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 797b04e..f393faa 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -741,3 +741,22 @@ def test_alias_magic(): ip.run_line_magic('alias_magic', '--line env_alias env') nt.assert_equal(ip.run_line_magic('env', ''), ip.run_line_magic('env_alias', '')) + + +def test_store(): + """Test %store.""" + ip = get_ipython() + ip.run_line_magic('load_ext', 'storemagic') + + # make sure the storage is empty + ip.run_line_magic('store', '-z') + ip.user_ns['var'] = 42 + ip.run_line_magic('store', 'var') + ip.user_ns['var'] = 39 + ip.run_line_magic('store', '-r') + nt.assert_equal(ip.user_ns['var'], 42) + + ip.run_line_magic('store', '-d var') + ip.user_ns['var'] = 39 + ip.run_line_magic('store' , '-r') + nt.assert_equal(ip.user_ns['var'], 39) diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py index a30e1e2..d222cd5 100644 --- a/IPython/extensions/storemagic.py +++ b/IPython/extensions/storemagic.py @@ -140,7 +140,7 @@ def store(self, parameter_s=''): # run without arguments -> list variables & values elif not args: - vars = self.db.keys('autorestore/*') + vars = db.keys('autorestore/*') vars.sort() if vars: size = max(map(len, vars)) @@ -186,9 +186,9 @@ def store(self, parameter_s=''): except KeyError: # it might be an alias # This needs to be refactored to use the new AliasManager stuff. - if args[0] in self.alias_manager: + if args[0] in ip.alias_manager: name = args[0] - nargs, cmd = self.alias_manager.alias_table[ name ] + nargs, cmd = ip.alias_manager.alias_table[ name ] staliases = db.get('stored_aliases',{}) staliases[ name ] = cmd db['stored_aliases'] = staliases @@ -207,7 +207,7 @@ def store(self, parameter_s=''): """ % (args[0], obj) ) return #pickled = pickle.dumps(obj) - self.db[ 'autorestore/' + args[0] ] = obj + db[ 'autorestore/' + args[0] ] = obj print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) -- 1.7.8.6