def __exit__(self, ex_tp, ex_val, ex_tb):
if not haveReadline:
return False
try:
# update the history for this project
readline.write_history_file(self.historyFile)
except Exception as E:
print "Failed to write history to {0} - {1}".format(self.historyFile, E)
# put back the original history!
readline.clear_history()
readline.read_history_file(self.oldhistFile)
os.unlink(self.oldhistFile)
# and put back the old completer
readline.set_completer(self.oldCompleter)
# linegenerators, pass one of these to the "run"
# method of the CommandLineInterface object
#import ctypes
#rllib = ctypes.cdll.LoadLibrary("libreadline.so")
#rl_line_buffer = ctypes.c_char_p.in_dll(rllib, "rl_line_buffer")
#rl_done = ctypes.c_int.in_dll(rllib, "rl_done")
python类clear_history()的实例源码
def remove_item(self, index):
# readline.remove_history_item() doesn't seem to work. Do it the
# hard way.
#try:
# readline.remove_history_item(i)
#except ValueError:
# pass
buf = []
for i in range(1, self.total + 1):
if i != index:
buf += self.get_item(i)
self.clear_history()
for s in buf:
readline.add_history(s)
def clear_history():
pass
def __enter__(self):
# we only do something if we have readline
if not haveReadline:
return self
## Set up the new history context
self.historyFile = os.path.join( os.getenv('HOME'), ".{0}.history".format(self.basename))
(h, self.oldhistFile) = tempfile.mkstemp(prefix=self.basename, suffix=".hist", dir="/tmp")
# only need the filename, really
os.close(h)
readline.write_history_file(self.oldhistFile)
readline.clear_history()
# if reading the old history fails, fail silently
# (the file might not exist yet)
try:
readline.read_history_file(self.historyFile)
except:
pass
# store the old completer, install our own one
readline.parse_and_bind("tab: complete")
#readline.parse_and_bind("C-c: backward-kill-line")
self.oldCompleter = readline.get_completer()
readline.set_completer(self.completer)
return self
# clean up the context
def clear_history():
pass
def clear_history(self):
"""
Clear the history buffer. Subclasses *must* provide an
implementation of this method.
"""
pass
def replace_history(self, commands):
"""
Replace the entire contents of the history with another set of values
:Parameters:
commands : list
List of strings to put in the history after clearing it of any
existing entries
"""
self.clear_history()
for command in commands:
self.add_item(command, force=True)
def clear_history(self):
try:
readline.clear_history()
except AttributeError:
len = self.get_max_length()
readline.set_history_length(0)
readline.set_history_length(len)
def remove_item(self, index):
buf = copy.deepcopy(self.__get_buf())
self.clear_history()
for s in buf:
readline.add_history(s)
def clear_history(self):
pass
def clear_history():
pass
def clear_history():
pass
def clear_history():
pass