Repository: ipython/ipython

Issue:
storemagic needs to use self.shell
As reported [on the mailing list](http://mail.scipy.org/pipermail/ipython-user/2012-July/010551.html)

```
AttributeError                            Traceback (most recent call last)
<ipython-input-3-6795157a19c8> in <module>()
----> 1 get_ipython().magic(u'store')

C:\Python26\ArcGIS10.0\lib\site-packages\IPython\core\interactiveshell.pyc in magic(self, arg_s)
   2159         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2160         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2161         return self.run_line_magic(magic_name, magic_arg_s)
   2162
   2163     #-------------------------------------------------------------------------

C:\Python26\ArcGIS10.0\lib\site-packages\IPython\core\interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2085                 args.append(sys._getframe(stack_depth).f_locals)
   2086             with self.builtin_trap:
-> 2087                 result = fn(*args)
   2088             return result
   2089

C:\Python26\ArcGIS10.0\lib\site-packages\IPython\extensions\storemagic.pyc in store(self, parameter_s)

C:\Python26\ArcGIS10.0\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)

    189     # but it's overkill for just that one bit of state.
    190     def magic_deco(arg):
--> 191         call = lambda f, *a, **k: f(*a, **k)
    192
    193         if callable(arg):

C:\Python26\ArcGIS10.0\lib\site-packages\IPython\extensions\storemagic.pyc in store(self, parameter_s)

    141         # run without arguments -> list variables & values
    142         elif not args:
--> 143             vars = self.db.keys('autorestore/*')
    144             vars.sort(
    145             if vars:

AttributeError: 'StoreMagics' object has no attribute 'db'
```



Discussion:
Ouch, let's add a test as well so we catch it in the future.  Thanks for the quick followup with a report.


Resolving patch:
diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py
--- 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__)
 
 


Test patch:
diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py
--- a/IPython/core/tests/test_magic.py
+++ b/IPython/core/tests/test_magic.py
@@ -777,3 +777,22 @@ def test_save():
             content = f.read()
             nt.assert_equal(content.count(cmds[0]), 2)
             nt.assert_true('coding: utf-8' in content)
+
+
+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)