Permalink
Browse files

Merge branch 'hard-reset' of https://github.com/takluyver/ipython int…

…o takluyver-hard-reset
  • Loading branch information...
2 parents ca434b7 + 3818b41 commit c45fd8581a7b8bd1221de3b98be77134e15430b1 @takluyver takluyver committed Apr 7, 2011
Showing with 44 additions and 13 deletions.
  1. +3 −0 IPython/core/displayhook.py
  2. +7 −0 IPython/core/interactiveshell.py
  3. +19 −13 IPython/core/magic.py
  4. +15 −0 IPython/core/tests/test_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
@@ -1045,6 +1045,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
@@ -1069,6 +1072,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.
-
+
+ -s : 'Soft' reset: Only clears your namespace, leaving history intact.
+ References to objects may be kept. By default (without this option),
+ we do a 'hard' reset, giving you a new session and removing all
+ references to objects from the current session.
+
Examples
--------
In [6]: a = 1
@@ -985,25 +988,28 @@ def magic_reset(self, parameter_s=''):
In [9]: %reset -f
- In [10]: 'a' in _ip.user_ns
- Out[10]: False
+ In [1]: 'a' in _ip.user_ns
+ Out[1]: False
"""
-
- if parameter_s == '-f':
+ opts, args = self.parse_options(parameter_s,'sh')
+ 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])
+
+ if 's' in opts: # Soft reset
+ 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()
+ else: # Hard reset
+ self.shell.reset(new_session = True)
+
+
def magic_reset_selective(self, parameter_s=''):
"""Resets the namespace by removing names defined by the user.
@@ -379,6 +379,21 @@ def test_xmode():
for i in range(3):
_ip.magic("xmode")
nt.assert_equal(_ip.InteractiveTB.mode, xmode)
+
+def test_reset_hard():
+ monitor = []
+ class A(object):
+ def __del__(self):
+ monitor.append(1)
+ def __repr__(self):
+ return "<A instance>"
+
+ _ip.user_ns["a"] = A()
+ _ip.run_cell("a")
+
+ nt.assert_equal(monitor, [])
+ _ip.magic_reset("-f")
+ nt.assert_equal(monitor, [1])
def doctest_who():
"""doctest for %who

0 comments on commit c45fd85

Please sign in to comment.