Permalink
Browse files

Minor improvements to how namespaces are handled.

  • Loading branch information...
1 parent d9748b0 commit f8787d813be32a9d419bba1fdb76651a0ff8d6e1 @takluyver committed Aug 16, 2011
Showing with 23 additions and 15 deletions.
  1. +4 −4 IPython/core/displayhook.py
  2. +2 −1 IPython/core/history.py
  3. +16 −7 IPython/core/interactiveshell.py
  4. +1 −3 IPython/core/tests/test_iplib.py
@@ -265,16 +265,16 @@ def update_user_ns(self, result):
self.___ = self.__
self.__ = self._
self._ = result
- self.shell.user_ns.update({'_':self._,
- '__':self.__,
- '___':self.___})
+ self.shell.push({'_':self._,
+ '__':self.__,
+ '___':self.___}, interactive=False)
# hackish access to top-level namespace to create _1,_2... dynamically
to_main = {}
if self.do_full_cache:
new_result = '_'+`self.prompt_count`
to_main[new_result] = result
- self.shell.user_ns.update(to_main)
+ self.shell.push(to_main, interactive=False)
self.shell.user_ns['_oh'][self.prompt_count] = result
def log_output(self, format_dict):
View
@@ -559,7 +559,8 @@ def store_inputs(self, line_num, source, source_raw=None):
'_ii': self._ii,
'_iii': self._iii,
new_i : self._i00 }
- self.shell.user_ns.update(to_main)
+
+ self.shell.push(to_main, interactive=False)
def store_output(self, line_num):
"""If database output logging is enabled, this saves all the
@@ -1073,6 +1073,16 @@ def init_user_ns(self):
# Finally, update the real user's namespace
self.user_ns.update(ns)
+
+ @property
+ def all_ns_refs(self):
+ """Get a list of references to all the namespace dictionaries in which
+ IPython might store a user-created object.
+
+ Note that this does not include the displayhook, which also caches
+ objects from the output."""
+ return [self.user_ns, self.user_global_ns,
+ self._user_main_module.__dict__] + self._main_ns_cache.values()
def reset(self, new_session=True):
"""Clear all internal namespaces, and attempt to release references to
@@ -1103,6 +1113,8 @@ def reset(self, new_session=True):
for k in drop_keys:
del ns[k]
+ self.user_ns_hidden.clear()
+
# Restore the user namespaces to minimal usability
self.init_user_ns()
@@ -1133,8 +1145,7 @@ def del_var(self, varname, by_name=False):
if varname in ('__builtin__', '__builtins__'):
raise ValueError("Refusing to delete %s" % varname)
- ns_refs = [self.user_ns, self.user_global_ns,
- self._user_main_module.__dict__] + self._main_ns_cache.values()
+ ns_refs = self.all_ns_refs
if by_name: # Delete by name
for ns in ns_refs:
@@ -1176,7 +1187,7 @@ def reset_selective(self, regex=None):
raise TypeError('regex must be a string or compiled pattern')
# Search for keys in each namespace that match the given regex
# If a match is found, delete the key/value pair.
- for ns in [self.user_ns, self.user_global_ns]:
+ for ns in self.all_ns_refs:
for var in ns:
if m.search(var):
del ns[var]
@@ -1224,11 +1235,9 @@ def push(self, variables, interactive=True):
# And configure interactive visibility
user_ns_hidden = self.user_ns_hidden
if interactive:
- for name in vdict:
- user_ns_hidden.discard(name)
+ user_ns_hidden.difference_update(vdict)
else:
- for name in vdict:
- user_ns_hidden.add(name)
+ user_ns_hidden.update(vdict)
def drop_by_id(self, variables):
"""Remove a dict of variables from the user namespace, if they are the
@@ -30,16 +30,14 @@
def test_reset():
"""reset must clear most namespaces."""
- # The number of variables in the private user_ns_hidden is not zero, but it
- # should be constant regardless of what we do
- nvars_hidden = len(ip.user_ns_hidden)
# Check that reset runs without error
ip.reset()
# Once we've reset it (to clear of any junk that might have been there from
# other tests, we can count how many variables are in the user's namespace
nvars_user_ns = len(ip.user_ns)
+ nvars_hidden = len(ip.user_ns_hidden)
# Now add a few variables to user_ns, and check that reset clears them
ip.user_ns['x'] = 1

0 comments on commit f8787d8

Please sign in to comment.