diff --git a/IPython/Extensions/clearcmd.py b/IPython/Extensions/clearcmd.py new file mode 100644 index 0000000..3714e8f --- /dev/null +++ b/IPython/Extensions/clearcmd.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +""" IPython extension: add %clear magic """ + +import IPython.ipapi +ip = IPython.ipapi.get() + +def clear_list(l): + while l: + l.pop() + +def clear_f(self,arg): + """ Clear various data (e.g. stored history data) + + %clear out - clear output history + %clear in - clear input history + """ + + api = self.getapi() + for target in arg.split(): + if target == 'out': + print "Flushing output cache (%d entries)" % len(api.user_ns()['_oh']) + self.outputcache.flush() + elif target == 'in': + print "Flushing input history" + from IPython import iplib + clear_list(self.input_hist) + clear_list(self.input_hist_raw) + for n in range(1,self.outputcache.prompt_count + 1): + key = '_i'+`n` + try: + del self.user_ns[key] + except: pass + +ip.expose_magic("clear",clear_f) + + + + diff --git a/IPython/Extensions/ipy_system_conf.py b/IPython/Extensions/ipy_system_conf.py index aabbd56..1f4ae90 100644 --- a/IPython/Extensions/ipy_system_conf.py +++ b/IPython/Extensions/ipy_system_conf.py @@ -18,7 +18,7 @@ import sys import ext_rehashdir # %rehashdir magic import ext_rescapture # var = !ls and var = %magic import pspersistence # %store magic - +import clearcmd # %clear # Basic readline config o = ip.options() \ No newline at end of file diff --git a/IPython/Prompts.py b/IPython/Prompts.py index 127d8d0..891f578 100644 --- a/IPython/Prompts.py +++ b/IPython/Prompts.py @@ -2,7 +2,7 @@ """ Classes for handling input/output prompts. -$Id: Prompts.py 1076 2006-01-24 17:27:05Z vivainio $""" +$Id: Prompts.py 1261 2006-04-11 14:37:02Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez @@ -439,7 +439,6 @@ class CachedOutput: # other more normal stuff # b/c each call to the In[] prompt raises it by 1, even the first. self.prompt_count = 0 - self.cache_count = 1 # Store the last prompt string each time, we need it for aligning # continuation and auto-rewrite prompts self.last_prompt = '' @@ -545,7 +544,13 @@ class CachedOutput: def update(self,arg): #print '***cache_count', self.cache_count # dbg - if self.cache_count >= self.cache_size and self.do_full_cache: + if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache: + warn('Output cache limit (currently '+\ + `self.cache_count`+' entries) hit.\n' + 'Flushing cache and resetting history counter...\n' + 'The only history variables available will be _,__,___ and _1\n' + 'with the current result.') + self.flush() # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise # we cause buggy behavior for things like gettext). @@ -558,7 +563,6 @@ class CachedOutput: # hackish access to top-level namespace to create _1,_2... dynamically to_main = {} if self.do_full_cache: - self.cache_count += 1 self.entries.append(arg) new_result = '_'+`self.prompt_count` to_main[new_result] = self.entries[-1] @@ -569,16 +573,16 @@ class CachedOutput: if not self.do_full_cache: raise ValueError,"You shouldn't have reached the cache flush "\ "if full caching is not enabled!" - warn('Output cache limit (currently '+\ - `self.cache_count`+' entries) hit.\n' - 'Flushing cache and resetting history counter...\n' - 'The only history variables available will be _,__,___ and _1\n' - 'with the current result.') # delete auto-generated vars from global namespace + for n in range(1,self.prompt_count + 1): key = '_'+`n` try: del self.user_ns[key] except: pass - self.prompt_count = 1 - self.cache_count = 1 + self.user_ns['_oh'].clear() + + if '_' not in __builtin__.__dict__: + self.user_ns.update({'_':None,'__':None, '___':None}) + import gc + gc.collect() # xxx needed? diff --git a/doc/ChangeLog b/doc/ChangeLog index 9cbc75b..8f3929a 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -3,6 +3,11 @@ * iplib.py, ipmaker.py: .ipy extension now means "ipython batch file" in command line. E.g. "ipython test.ipy" runs test.ipy with ipython prefilters, allowing stuff like magics and aliases in the file. + + * Prompts.py, Extensions/clearcmd.py, ipy_system_conf.py: %clear magic + added. Supported now are "%clear in" and "%clear out" (clear input and + output history, respectively). Also fixed CachedOutput.flush to + properly flush the output cache. 2006-03-28 Ville Vainio