Skip to content

Commit

Permalink
Merge branch 'hard-reset' of https://github.com/takluyver/ipython int…
Browse files Browse the repository at this point in the history
…o takluyver-hard-reset
  • Loading branch information
takluyver committed Apr 7, 2011
2 parents ca434b7 + 3818b41 commit c45fd85
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
3 changes: 3 additions & 0 deletions IPython/core/displayhook.py
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions IPython/core/interactiveshell.py
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 19 additions & 13 deletions IPython/core/magic.py
Expand Up @@ -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
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions IPython/core/tests/test_magic.py
Expand Up @@ -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
Expand Down

0 comments on commit c45fd85

Please sign in to comment.