diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py index a8a8a92..7f0817f 100644 --- a/IPython/core/displayhook.py +++ b/IPython/core/displayhook.py @@ -319,6 +319,9 @@ class DisplayHook(Configurable): 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 diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index cd9595e..0c796b7 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1045,6 +1045,9 @@ class InteractiveShell(Configurable, Magic): """ # 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 @@ class InteractiveShell(Configurable, Magic): # 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 diff --git a/IPython/core/magic.py b/IPython/core/magic.py index f6c2c65..ce0ba05 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -967,12 +967,15 @@ Currently the magic system has the following functions:\n""" 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,11 +988,11 @@ Currently the magic system has the following functions:\n""" 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( @@ -997,13 +1000,16 @@ Currently the magic system has the following functions:\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. diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 9328c81..671d7ea 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -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 "" + + _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