@@ -4446,9 +4446,8 @@ def _initialize_history(self, hist_file: str) -> None:
4446
4446
previous sessions will be included. Additionally, all history will be written
4447
4447
to this file when the application exits.
4448
4448
"""
4449
- from json import (
4450
- JSONDecodeError ,
4451
- )
4449
+ import json
4450
+ import lzma
4452
4451
4453
4452
self .history = History ()
4454
4453
# with no persistent history, nothing else in this method is relevant
@@ -4474,16 +4473,17 @@ def _initialize_history(self, hist_file: str) -> None:
4474
4473
4475
4474
# Read and process history file
4476
4475
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' )
4479
4479
self .history = History .from_json (history_json )
4480
4480
except FileNotFoundError :
4481
4481
# Just use an empty history
4482
4482
pass
4483
4483
except OSError as ex :
4484
4484
self .perror (f"Cannot read persistent history file '{ hist_file } ': { ex } " )
4485
4485
return
4486
- except (JSONDecodeError , KeyError , ValueError ) as ex :
4486
+ except (lzma . LZMAError , json . JSONDecodeError , KeyError , UnicodeDecodeError , ValueError ) as ex :
4487
4487
self .perror (f"Error processing persistent history file '{ hist_file } ': { ex } " )
4488
4488
4489
4489
self .history .start_session ()
@@ -4509,14 +4509,19 @@ def _initialize_history(self, hist_file: str) -> None:
4509
4509
atexit .register (self ._persist_history )
4510
4510
4511
4511
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
+
4513
4515
if not self .persistent_history_file :
4514
4516
return
4515
4517
4516
4518
self .history .truncate (self ._persistent_history_length )
4517
4519
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 )
4520
4525
except OSError as ex :
4521
4526
self .perror (f"Cannot write persistent history file '{ self .persistent_history_file } ': { ex } " )
4522
4527
0 commit comments