@@ -404,9 +404,8 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
404404
405405 # Commands to exclude from the history command
406406 # initialize history
407- self .persistent_history_file = persistent_history_file
408407 self .persistent_history_length = persistent_history_length
409- self ._initialize_history ()
408+ self ._initialize_history (persistent_history_file )
410409 self .exclude_from_history = '''history edit eof eos''' .split ()
411410
412411 # Command aliases and macros
@@ -3448,7 +3447,7 @@ def do_history(self, args: argparse.Namespace) -> None:
34483447 for hi in history :
34493448 self .poutput (hi .pr (script = args .script , expanded = args .expanded , verbose = args .verbose ))
34503449
3451- def _initialize_history (self ):
3450+ def _initialize_history (self , hist_file ):
34523451 """Initialize history using history related attributes
34533452
34543453 This function can determine whether history is saved in the prior text-based
@@ -3462,43 +3461,46 @@ def _initialize_history(self):
34623461 """
34633462 self .history = History ()
34643463 # with no persistent history, nothing else in this method is relevant
3465- if not self .persistent_history_file :
3464+ if not hist_file :
3465+ self .persistent_history_file = hist_file
34663466 return
34673467
3468- self . persistent_history_file = os .path .expanduser (self . persistent_history_file )
3468+ hist_file = os .path .expanduser (hist_file )
34693469
34703470 # first we try and unpickle the history file
34713471 history = History ()
34723472 try :
3473- with open (self . persistent_history_file , 'rb' ) as fobj :
3473+ with open (hist_file , 'rb' ) as fobj :
34743474 history = pickle .load (fobj )
34753475 except (FileNotFoundError , KeyError , EOFError ):
34763476 pass
34773477 except IsADirectoryError :
34783478 msg = "persistent history file '{}' is a directory"
3479- self .perror (msg .format (self .persistent_history_file ))
3479+ self .perror (msg .format (hist_file ))
3480+ return
34803481 except OSError as ex :
34813482 msg = "can not read persistent history file '{}': {}"
3482- self .perror (msg .format (self .persistent_history_file , ex ), traceback_war = False )
3483+ self .perror (msg .format (hist_file , ex ), traceback_war = False )
3484+ return
34833485
34843486 self .history = history
3485-
3486- # trim history to length and ensure it's writable
3487- # history.truncate(maxlen)
3488- # try:
3489- # # open with append so it doesn't truncate the file
3490- # with open(histfile, 'ab') as fobj:
3491- # self.persistent_history_file = histfile
3492- # except OSError as ex:
3493- # msg = "can not write persistent history file '{}': {}"
3494- # self.perror(msg.format(histfile, ex), traceback_war=False)
3487+ self .persistent_history_file = hist_file
3488+
3489+ # populate readline history
3490+ if rl_type != RlType .NONE :
3491+ last = None
3492+ for item in history :
3493+ # readline only adds a single entry for multiple sequential identical commands
3494+ # so we emulate that behavior here
3495+ if item .statement .raw != last :
3496+ readline .add_history (item .statement .raw )
3497+ last = item .statement .raw
34953498
34963499 # register a function to write history at save
34973500 # if the history file is in plain text format from 0.9.12 or lower
34983501 # this will fail, and the history in the plain text file will be lost
3499-
3500- #import atexit
3501- #atexit.register(self._persist_history_on_exit)
3502+ import atexit
3503+ atexit .register (self ._persist_history_on_exit )
35023504
35033505 def _persist_history_on_exit (self ):
35043506 """write history out to the history file"""
@@ -3509,7 +3511,6 @@ def _persist_history_on_exit(self):
35093511 try :
35103512 with open (self .persistent_history_file , 'wb' ) as fobj :
35113513 pickle .dump (self .history , fobj )
3512-
35133514 except OSError as ex :
35143515 msg = "can not write persistent history file '{}': {}"
35153516 self .perror (msg .format (self .persistent_history_file , ex ), traceback_war = False )
0 commit comments