##// END OF EJS Templates
Add drop_by_id method to shell, to remove variables added by extensions.
Thomas Kluyver -
Show More
@@ -1264,6 +1264,24 b' class InteractiveShell(SingletonConfigurable, Magic):'
1264 else:
1264 else:
1265 for name,val in vdict.iteritems():
1265 for name,val in vdict.iteritems():
1266 config_ns[name] = val
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 # Things related to object introspection
1287 # Things related to object introspection
@@ -179,4 +179,17 b' class InteractiveShellTestCase(unittest.TestCase):'
179 finally:
179 finally:
180 io.stderr = save_stderr
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()
General Comments 0
You need to be logged in to leave comments. Login now