@@ -4446,9 +4446,8 @@ def _initialize_history(self, hist_file: str) -> None:
44464446 previous sessions will be included. Additionally, all history will be written
44474447 to this file when the application exits.
44484448 """
4449- from json import (
4450- JSONDecodeError ,
4451- )
4449+ import json
4450+ import lzma
44524451
44534452 self .history = History ()
44544453 # with no persistent history, nothing else in this method is relevant
@@ -4474,16 +4473,17 @@ def _initialize_history(self, hist_file: str) -> None:
44744473
44754474 # Read and process history file
44764475 try :
4477- with open (hist_file , 'r' ) as fobj :
4478- history_json = fobj .read ()
4476+ with open (hist_file , 'rb' ) as fobj :
4477+ compressed_bytes = fobj .read ()
4478+ history_json = lzma .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
44794479 self .history = History .from_json (history_json )
44804480 except FileNotFoundError :
44814481 # Just use an empty history
44824482 pass
44834483 except OSError as ex :
44844484 self .perror (f"Cannot read persistent history file '{ hist_file } ': { ex } " )
44854485 return
4486- except (JSONDecodeError , KeyError , ValueError ) as ex :
4486+ except (lzma . LZMAError , json . JSONDecodeError , KeyError , UnicodeDecodeError , ValueError ) as ex :
44874487 self .perror (f"Error processing persistent history file '{ hist_file } ': { ex } " )
44884488
44894489 self .history .start_session ()
@@ -4509,14 +4509,19 @@ def _initialize_history(self, hist_file: str) -> None:
45094509 atexit .register (self ._persist_history )
45104510
45114511 def _persist_history (self ) -> None :
4512- """Write history out to the persistent history file as JSON"""
4512+ """Write history out to the persistent history file as compressed JSON"""
4513+ import lzma
4514+
45134515 if not self .persistent_history_file :
45144516 return
45154517
45164518 self .history .truncate (self ._persistent_history_length )
45174519 try :
4518- with open (self .persistent_history_file , 'w' ) as fobj :
4519- fobj .write (self .history .to_json ())
4520+ history_json = self .history .to_json ()
4521+ compressed_bytes = lzma .compress (history_json .encode (encoding = 'utf-8' ))
4522+
4523+ with open (self .persistent_history_file , 'wb' ) as fobj :
4524+ fobj .write (compressed_bytes )
45204525 except OSError as ex :
45214526 self .perror (f"Cannot write persistent history file '{ self .persistent_history_file } ': { ex } " )
45224527
0 commit comments