Skip to content
This repository

Add drop_by_id method to shell, to remove variables added by extensions. #889

Closed
wants to merge 1 commit into from

2 participants

Thomas Kluyver Fernando Perez
Thomas Kluyver
Collaborator

This was prompted by the revived physics extension. Extensions can drop a lot of variables into the user namespace. Ideally, if the extension is unloaded, those variables should disappear, but only if the names haven't been rebound.

This works as a counterpart to ip.push(). The idea is that an extension can work like this:

myvars = dict(a=foo(), b=bar(), c=baz())

def load_ipython_extension(ip):
    ip.push(myvars)

def unload_ipython_extension(ip):
    ip.drop_by_id(myvars)
Fernando Perez
Owner

merged with rebase to avoid recursive merge for just one commit. thx!

Fernando Perez fperez closed this October 17, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
18  IPython/core/interactiveshell.py
@@ -1264,6 +1264,24 @@ def push(self, variables, interactive=True):
1264 1264
         else:
1265 1265
             for name,val in vdict.iteritems():
1266 1266
                 config_ns[name] = val
  1267
+    
  1268
+    def drop_by_id(self, variables):
  1269
+        """Remove a dict of variables from the user namespace, if they are the
  1270
+        same as the values in the dictionary.
  1271
+        
  1272
+        This is intended for use by extensions: variables that they've added can
  1273
+        be taken back out if they are unloaded, without removing any that the
  1274
+        user has overwritten.
  1275
+        
  1276
+        Parameters
  1277
+        ----------
  1278
+        variables : dict
  1279
+          A dictionary mapping object names (as strings) to the objects.
  1280
+        """
  1281
+        for name, obj in variables.iteritems():
  1282
+            if name in self.user_ns and self.user_ns[name] is obj:
  1283
+                del self.user_ns[name]
  1284
+                self.user_ns_hidden.pop(name, None)
1267 1285
 
1268 1286
     #-------------------------------------------------------------------------
1269 1287
     # Things related to object introspection
15  IPython/core/tests/test_interactiveshell.py
@@ -179,4 +179,17 @@ def test_bad_custom_tb_return(self):
179 179
         finally:
180 180
             io.stderr = save_stderr
181 181
 
182  
-
  182
+    def test_drop_by_id(self):
  183
+        ip = get_ipython()
  184
+        myvars = {"a":object(), "b":object(), "c": object()}
  185
+        ip.push(myvars, interactive=False)
  186
+        for name in myvars:
  187
+            assert name in ip.user_ns, name
  188
+            assert name in ip.user_ns_hidden, name
  189
+        ip.user_ns['b'] = 12
  190
+        ip.drop_by_id(myvars)
  191
+        for name in ["a", "c"]:
  192
+            assert name not in ip.user_ns, name
  193
+            assert name not in ip.user_ns_hidden, name
  194
+        assert ip.user_ns['b'] == 12
  195
+        ip.reset()
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.