##// END OF EJS Templates
Minor improvements to how namespaces are handled.
Thomas Kluyver -
Show More
@@ -265,16 +265,16 b' class DisplayHook(Configurable):'
265 self.___ = self.__
265 self.___ = self.__
266 self.__ = self._
266 self.__ = self._
267 self._ = result
267 self._ = result
268 self.shell.user_ns.update({'_':self._,
268 self.shell.push({'_':self._,
269 '__':self.__,
269 '__':self.__,
270 '___':self.___})
270 '___':self.___}, interactive=False)
271
271
272 # hackish access to top-level namespace to create _1,_2... dynamically
272 # hackish access to top-level namespace to create _1,_2... dynamically
273 to_main = {}
273 to_main = {}
274 if self.do_full_cache:
274 if self.do_full_cache:
275 new_result = '_'+`self.prompt_count`
275 new_result = '_'+`self.prompt_count`
276 to_main[new_result] = result
276 to_main[new_result] = result
277 self.shell.user_ns.update(to_main)
277 self.shell.push(to_main, interactive=False)
278 self.shell.user_ns['_oh'][self.prompt_count] = result
278 self.shell.user_ns['_oh'][self.prompt_count] = result
279
279
280 def log_output(self, format_dict):
280 def log_output(self, format_dict):
@@ -559,7 +559,8 b' class HistoryManager(HistoryAccessor):'
559 '_ii': self._ii,
559 '_ii': self._ii,
560 '_iii': self._iii,
560 '_iii': self._iii,
561 new_i : self._i00 }
561 new_i : self._i00 }
562 self.shell.user_ns.update(to_main)
562
563 self.shell.push(to_main, interactive=False)
563
564
564 def store_output(self, line_num):
565 def store_output(self, line_num):
565 """If database output logging is enabled, this saves all the
566 """If database output logging is enabled, this saves all the
@@ -1073,6 +1073,16 b' class InteractiveShell(SingletonConfigurable, Magic):'
1073
1073
1074 # Finally, update the real user's namespace
1074 # Finally, update the real user's namespace
1075 self.user_ns.update(ns)
1075 self.user_ns.update(ns)
1076
1077 @property
1078 def all_ns_refs(self):
1079 """Get a list of references to all the namespace dictionaries in which
1080 IPython might store a user-created object.
1081
1082 Note that this does not include the displayhook, which also caches
1083 objects from the output."""
1084 return [self.user_ns, self.user_global_ns,
1085 self._user_main_module.__dict__] + self._main_ns_cache.values()
1076
1086
1077 def reset(self, new_session=True):
1087 def reset(self, new_session=True):
1078 """Clear all internal namespaces, and attempt to release references to
1088 """Clear all internal namespaces, and attempt to release references to
@@ -1103,6 +1113,8 b' class InteractiveShell(SingletonConfigurable, Magic):'
1103 for k in drop_keys:
1113 for k in drop_keys:
1104 del ns[k]
1114 del ns[k]
1105
1115
1116 self.user_ns_hidden.clear()
1117
1106 # Restore the user namespaces to minimal usability
1118 # Restore the user namespaces to minimal usability
1107 self.init_user_ns()
1119 self.init_user_ns()
1108
1120
@@ -1133,8 +1145,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
1133 if varname in ('__builtin__', '__builtins__'):
1145 if varname in ('__builtin__', '__builtins__'):
1134 raise ValueError("Refusing to delete %s" % varname)
1146 raise ValueError("Refusing to delete %s" % varname)
1135
1147
1136 ns_refs = [self.user_ns, self.user_global_ns,
1148 ns_refs = self.all_ns_refs
1137 self._user_main_module.__dict__] + self._main_ns_cache.values()
1138
1149
1139 if by_name: # Delete by name
1150 if by_name: # Delete by name
1140 for ns in ns_refs:
1151 for ns in ns_refs:
@@ -1176,7 +1187,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
1176 raise TypeError('regex must be a string or compiled pattern')
1187 raise TypeError('regex must be a string or compiled pattern')
1177 # Search for keys in each namespace that match the given regex
1188 # Search for keys in each namespace that match the given regex
1178 # If a match is found, delete the key/value pair.
1189 # If a match is found, delete the key/value pair.
1179 for ns in [self.user_ns, self.user_global_ns]:
1190 for ns in self.all_ns_refs:
1180 for var in ns:
1191 for var in ns:
1181 if m.search(var):
1192 if m.search(var):
1182 del ns[var]
1193 del ns[var]
@@ -1224,11 +1235,9 b' class InteractiveShell(SingletonConfigurable, Magic):'
1224 # And configure interactive visibility
1235 # And configure interactive visibility
1225 user_ns_hidden = self.user_ns_hidden
1236 user_ns_hidden = self.user_ns_hidden
1226 if interactive:
1237 if interactive:
1227 for name in vdict:
1238 user_ns_hidden.difference_update(vdict)
1228 user_ns_hidden.discard(name)
1229 else:
1239 else:
1230 for name in vdict:
1240 user_ns_hidden.update(vdict)
1231 user_ns_hidden.add(name)
1232
1241
1233 def drop_by_id(self, variables):
1242 def drop_by_id(self, variables):
1234 """Remove a dict of variables from the user namespace, if they are the
1243 """Remove a dict of variables from the user namespace, if they are the
@@ -30,9 +30,6 b' ip = get_ipython()'
30
30
31 def test_reset():
31 def test_reset():
32 """reset must clear most namespaces."""
32 """reset must clear most namespaces."""
33 # The number of variables in the private user_ns_hidden is not zero, but it
34 # should be constant regardless of what we do
35 nvars_hidden = len(ip.user_ns_hidden)
36
33
37 # Check that reset runs without error
34 # Check that reset runs without error
38 ip.reset()
35 ip.reset()
@@ -40,6 +37,7 b' def test_reset():'
40 # Once we've reset it (to clear of any junk that might have been there from
37 # Once we've reset it (to clear of any junk that might have been there from
41 # other tests, we can count how many variables are in the user's namespace
38 # other tests, we can count how many variables are in the user's namespace
42 nvars_user_ns = len(ip.user_ns)
39 nvars_user_ns = len(ip.user_ns)
40 nvars_hidden = len(ip.user_ns_hidden)
43
41
44 # Now add a few variables to user_ns, and check that reset clears them
42 # Now add a few variables to user_ns, and check that reset clears them
45 ip.user_ns['x'] = 1
43 ip.user_ns['x'] = 1
General Comments 0
You need to be logged in to leave comments. Login now