Permalink
Browse files

Implement hard reset with '%reset -h' call.

Closes gh-49
  • Loading branch information...
1 parent 627e381 commit 14963367d54bfc94908efa5858593a7f8b5dd83f @takluyver takluyver committed Apr 7, 2011
Showing with 25 additions and 11 deletions.
  1. +3 −0 IPython/core/displayhook.py
  2. +7 −0 IPython/core/interactiveshell.py
  3. +15 −11 IPython/core/magic.py
@@ -319,6 +319,9 @@ def flush(self):
except: pass
self.shell.user_ns['_oh'].clear()
+ # Release our own references to objects:
+ self._, self.__, self.___ = '', '', ''
+
if '_' not in __builtin__.__dict__:
self.shell.user_ns.update({'_':None,'__':None, '___':None})
import gc
@@ -1044,6 +1044,9 @@ def reset(self, new_session=True):
"""
# Clear histories
self.history_manager.reset(new_session)
+
+ # Flush cached output items
+ self.displayhook.flush()
# Reset counter used to index all histories
self.execution_count = 0
@@ -1068,6 +1071,10 @@ def reset(self, new_session=True):
# Restore the default and user aliases
self.alias_manager.clear_aliases()
self.alias_manager.init_aliases()
+
+ # Flush the private list of module references kept for script
+ # execution protection
+ self.clear_main_mod_cache()
def reset_selective(self, regex=None):
"""Clear selective variables from internal namespaces based on a
View
@@ -967,12 +967,15 @@ def type_name(v):
def magic_reset(self, parameter_s=''):
"""Resets the namespace by removing all names defined by the user.
- Input/Output history are left around in case you need them.
-
Parameters
----------
-f : force reset without asking for confirmation.
-
+
+ -h : 'Hard' reset: gives you a new session and removes all
+ references to objects from the current session. By default, we
+ do a 'soft' reset, which only clears out your namespace, and
+ leaves input and output history around.
+
Examples
--------
In [6]: a = 1
@@ -988,22 +991,23 @@ def magic_reset(self, parameter_s=''):
In [10]: 'a' in _ip.user_ns
Out[10]: False
"""
-
- if parameter_s == '-f':
+ opts, args = self.parse_options(parameter_s,'fh')
+ if 'f' in opts:
ans = True
else:
ans = self.shell.ask_yes_no(
"Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
if not ans:
print 'Nothing done.'
return
- user_ns = self.shell.user_ns
- for i in self.magic_who_ls():
- del(user_ns[i])
- # Also flush the private list of module references kept for script
- # execution protection
- self.shell.clear_main_mod_cache()
+ if 'h' in opts: # Hard reset
+ self.shell.reset(new_session = True)
+
+ else: # Soft reset
+ user_ns = self.shell.user_ns
+ for i in self.magic_who_ls():
+ del(user_ns[i])
def magic_reset_selective(self, parameter_s=''):
"""Resets the namespace by removing names defined by the user.

0 comments on commit 1496336

Please sign in to comment.