Skip to content

Commit

Permalink
Merge pull request #1309 from ivanov/inoculate-clear-magic
Browse files Browse the repository at this point in the history
Inoculate clearcmd extension into %reset functionality

Restored the functionality of this quarantined module, added new tests for it and polished it a bit, and made it available as part of the `%reset` magic.

Specifically, the `%clear` completions did not previously advertise the `array` argument option, and `clear in` did not clear `_i`, `_ii`, and `_iii` - both of these are now fixed.

Removing the `shadow_compress`  and `shadow_nuke` options, as they refer to functionality that does not appear to be used anywhere anymore.
  • Loading branch information
fperez committed Jan 26, 2012
2 parents 237e539 + d09084b commit 12b8a64
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 109 deletions.
5 changes: 4 additions & 1 deletion IPython/core/completerlib.py
Expand Up @@ -181,7 +181,6 @@ def do_complete(self, event):

get_ipython().set_hook('complete_command',do_complete, str_key = cmd)


def module_completion(line):
"""
Returns a list containing the completion possibilities for an import line.
Expand Down Expand Up @@ -316,3 +315,7 @@ def cd_completer(self, event):
raise TryNext

return [compress_user(p, tilde_expand, tilde_val) for p in found]

def reset_completer(self, event):
"A completer for %reset magic"
return '-f -s in out array dhist'.split()
3 changes: 2 additions & 1 deletion IPython/core/interactiveshell.py
Expand Up @@ -1840,7 +1840,7 @@ def init_completer(self):
"""
from IPython.core.completer import IPCompleter
from IPython.core.completerlib import (module_completer,
magic_run_completer, cd_completer)
magic_run_completer, cd_completer, reset_completer)

self.Completer = IPCompleter(shell=self,
namespace=self.user_ns,
Expand All @@ -1860,6 +1860,7 @@ def init_completer(self):
self.set_hook('complete_command', module_completer, str_key = 'from')
self.set_hook('complete_command', magic_run_completer, str_key = '%run')
self.set_hook('complete_command', cd_completer, str_key = '%cd')
self.set_hook('complete_command', reset_completer, str_key = '%reset')

# Only configure readline if we truly are using readline. IPython can
# do tab-completion over the network, in GUIs, etc, where readline
Expand Down
89 changes: 80 additions & 9 deletions IPython/core/magic.py
Expand Up @@ -25,6 +25,7 @@
import shutil
import re
import time
import gc
from StringIO import StringIO
from getopt import getopt,GetoptError
from pprint import pformat
Expand Down Expand Up @@ -959,16 +960,31 @@ def type_name(v):
print vstr[:25] + "<...>" + vstr[-25:]

def magic_reset(self, parameter_s=''):
"""Resets the namespace by removing all names defined by the user.
"""Resets the namespace by removing all names defined by the user, if
called without arguments, or by removing some types of objects, such
as everything currently in IPython's In[] and Out[] containers (see
the parameters for details).
Parameters
----------
-f : force reset without asking for confirmation.
-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.
in : reset input history
out : reset output history
dhist : reset directory history
array : reset only variables that are NumPy arrays
-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.
See Also
--------
%reset_selective
Examples
--------
Expand All @@ -985,13 +1001,20 @@ def magic_reset(self, parameter_s=''):
In [1]: 'a' in _ip.user_ns
Out[1]: False
In [2]: %reset -f in
Flushing input history
In [3]: %reset -f dhist in
Flushing directory history
Flushing input history
Notes
-----
Calling this magic from clients that do not implement standard input,
such as the ipython notebook interface, will reset the namespace
without confirmation.
"""
opts, args = self.parse_options(parameter_s,'sf')
opts, args = self.parse_options(parameter_s,'sf', mode='list')
if 'f' in opts:
ans = True
else:
Expand All @@ -1008,11 +1031,55 @@ def magic_reset(self, parameter_s=''):
user_ns = self.shell.user_ns
for i in self.magic_who_ls():
del(user_ns[i])

else: # Hard reset
elif len(args) == 0: # Hard reset
self.shell.reset(new_session = False)

# reset in/out/dhist/array: previously extensinions/clearcmd.py
ip = self.shell
user_ns = self.user_ns # local lookup, heavily used

for target in args:
target = target.lower() # make matches case insensitive
if target == 'out':
print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
self.displayhook.flush()

elif target == 'in':
print "Flushing input history"
pc = self.displayhook.prompt_count + 1
for n in range(1, pc):
key = '_i'+repr(n)
user_ns.pop(key,None)
user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
hm = ip.history_manager
# don't delete these, as %save and %macro depending on the length
# of these lists to be preserved
hm.input_hist_parsed[:] = [''] * pc
hm.input_hist_raw[:] = [''] * pc
# hm has internal machinery for _i,_ii,_iii, clear it out
hm._i = hm._ii = hm._iii = hm._i00 = u''

elif target == 'array':
# Support cleaning up numpy arrays
try:
from numpy import ndarray
# This must be done with items and not iteritems because we're
# going to modify the dict in-place.
for x,val in user_ns.items():
if isinstance(val,ndarray):
del user_ns[x]
except ImportError:
print "reset array only works if Numpy is available."

elif target == 'dhist':
print "Flushing directory history"
del user_ns['_dh'][:]

else:
print "Don't know how to reset ",
print target + ", please run `%reset?` for details"

gc.collect()

def magic_reset_selective(self, parameter_s=''):
"""Resets the namespace by removing names defined by the user.
Expand All @@ -1026,6 +1093,10 @@ def magic_reset_selective(self, parameter_s=''):
Options
-f : force reset without asking for confirmation.
See Also
--------
%reset
Examples
--------
Expand Down
48 changes: 39 additions & 9 deletions IPython/core/tests/test_magic.py
Expand Up @@ -183,20 +183,50 @@ def test_macro_run():
with tt.AssertPrints("13"):
ip.run_cell("test")


# XXX failing for now, until we get clearcmd out of quarantine. But we should
# fix this and revert the skip to happen only if numpy is not around.
#@dec.skipif_not_numpy
@dec.skip_known_failure
def test_numpy_clear_array_undec():
from IPython.extensions import clearcmd

@dec.skipif_not_numpy
def test_numpy_reset_array_undec():
"Test '%reset array' functionality"
_ip.ex('import numpy as np')
_ip.ex('a = np.empty(2)')
yield (nt.assert_true, 'a' in _ip.user_ns)
_ip.magic('clear array')
_ip.magic('reset -f array')
yield (nt.assert_false, 'a' in _ip.user_ns)


def test_reset_out():
"Test '%reset out' magic"
_ip.run_cell("parrot = 'dead'", store_history=True)
# test '%reset -f out', make an Out prompt
_ip.run_cell("parrot", store_history=True)
nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
_ip.magic('reset -f out')
nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
nt.assert_true(len(_ip.user_ns['Out']) == 0)

def test_reset_in():
"Test '%reset in' magic"
# test '%reset -f in'
_ip.run_cell("parrot", store_history=True)
nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
_ip.magic('%reset -f in')
nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
nt.assert_true(len(set(_ip.user_ns['In'])) == 1)

def test_reset_dhist():
"Test '%reset dhist' magic"
_ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
_ip.magic('cd ' + os.path.dirname(nt.__file__))
_ip.magic('cd -')
nt.assert_true(len(_ip.user_ns['_dh']) > 0)
_ip.magic('reset -f dhist')
nt.assert_true(len(_ip.user_ns['_dh']) == 0)
_ip.run_cell("_dh = [d for d in tmp]") #restore

def test_reset_in_length():
"Test that '%reset in' preserves In[] length"
_ip.run_cell("print 'foo'")
_ip.run_cell("reset -f in")
nt.assert_true(len(_ip.user_ns['In']) == _ip.displayhook.prompt_count+1)

def test_time():
_ip.magic('time None')
Expand Down
87 changes: 0 additions & 87 deletions IPython/quarantine/clearcmd.py

This file was deleted.

2 changes: 0 additions & 2 deletions IPython/quarantine/ipy_jot.py
Expand Up @@ -116,8 +116,6 @@ def jot_obj(self, obj, name, comment=''):

uname = 'jot/'+name+suffix

# which one works better?
#all = ip.shadowhist.all()
all = ip.shell.history_manager.input_hist_parsed

# We may actually want to make snapshot of files that are run-ned.
Expand Down

0 comments on commit 12b8a64

Please sign in to comment.