@@ -4627,7 +4627,7 @@ def do_ipy(self, _: argparse.Namespace) -> Optional[bool]: # pragma: no cover
4627
4627
)
4628
4628
4629
4629
# Start IPython
4630
- start_ipython (config = config , argv = [], user_ns = local_vars )
4630
+ start_ipython (config = config , argv = [], user_ns = local_vars ) # type: ignore[no-untyped-call]
4631
4631
self .poutput ("Now exiting IPython shell..." )
4632
4632
4633
4633
# The IPython application is a singleton and won't be recreated next time
@@ -4814,11 +4814,9 @@ def _initialize_history(self, hist_file: str) -> None:
4814
4814
previous sessions will be included. Additionally, all history will be written
4815
4815
to this file when the application exits.
4816
4816
"""
4817
- import json
4818
- import lzma
4819
-
4820
4817
self .history = History ()
4821
- # with no persistent history, nothing else in this method is relevant
4818
+
4819
+ # With no persistent history, nothing else in this method is relevant
4822
4820
if not hist_file :
4823
4821
self .persistent_history_file = hist_file
4824
4822
return
@@ -4839,28 +4837,60 @@ def _initialize_history(self, hist_file: str) -> None:
4839
4837
self .perror (f"Error creating persistent history file directory '{ hist_file_dir } ': { ex } " )
4840
4838
return
4841
4839
4842
- # Read and process history file
4840
+ # Read history file
4843
4841
try :
4844
4842
with open (hist_file , 'rb' ) as fobj :
4845
4843
compressed_bytes = fobj .read ()
4846
- history_json = lzma .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
4847
- self .history = History .from_json (history_json )
4848
4844
except FileNotFoundError :
4849
- # Just use an empty history
4850
- pass
4845
+ compressed_bytes = bytes ()
4851
4846
except OSError as ex :
4852
4847
self .perror (f"Cannot read persistent history file '{ hist_file } ': { ex } " )
4853
4848
return
4854
- except (json .JSONDecodeError , lzma .LZMAError , KeyError , UnicodeDecodeError , ValueError ) as ex :
4849
+
4850
+ # Register a function to write history at save
4851
+ import atexit
4852
+
4853
+ self .persistent_history_file = hist_file
4854
+ atexit .register (self ._persist_history )
4855
+
4856
+ # Empty or nonexistent history file. Nothing more to do.
4857
+ if not compressed_bytes :
4858
+ return
4859
+
4860
+ # Decompress history data
4861
+ try :
4862
+ import lzma as decompress_lib
4863
+
4864
+ decompress_exceptions : Tuple [type [Exception ]] = (decompress_lib .LZMAError ,)
4865
+ except ModuleNotFoundError : # pragma: no cover
4866
+ import bz2 as decompress_lib # type: ignore[no-redef]
4867
+
4868
+ decompress_exceptions : Tuple [type [Exception ]] = (OSError , ValueError ) # type: ignore[no-redef]
4869
+
4870
+ try :
4871
+ history_json = decompress_lib .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
4872
+ except decompress_exceptions as ex :
4873
+ self .perror (
4874
+ f"Error decompressing persistent history data '{ hist_file } ': { ex } \n "
4875
+ f"The history file will be recreated when this application exits."
4876
+ )
4877
+ return
4878
+
4879
+ # Decode history json
4880
+ import json
4881
+
4882
+ try :
4883
+ self .history = History .from_json (history_json )
4884
+ except (json .JSONDecodeError , KeyError , ValueError ) as ex :
4855
4885
self .perror (
4856
- f"Error processing persistent history file '{ hist_file } ': { ex } \n "
4886
+ f"Error processing persistent history data '{ hist_file } ': { ex } \n "
4857
4887
f"The history file will be recreated when this application exits."
4858
4888
)
4889
+ return
4859
4890
4860
4891
self .history .start_session ()
4861
- self .persistent_history_file = hist_file
4862
4892
4863
- # populate readline history
4893
+ # Populate readline history
4864
4894
if rl_type != RlType .NONE :
4865
4895
last = None
4866
4896
for item in self .history :
@@ -4872,25 +4902,21 @@ def _initialize_history(self, hist_file: str) -> None:
4872
4902
readline .add_history (line )
4873
4903
last = line
4874
4904
4875
- # register a function to write history at save
4876
- # if the history file is in plain text format from 0.9.12 or lower
4877
- # this will fail, and the history in the plain text file will be lost
4878
- import atexit
4879
-
4880
- atexit .register (self ._persist_history )
4881
-
4882
4905
def _persist_history (self ) -> None :
4883
4906
"""Write history out to the persistent history file as compressed JSON"""
4884
- import lzma
4885
-
4886
4907
if not self .persistent_history_file :
4887
4908
return
4888
4909
4889
- self .history .truncate (self ._persistent_history_length )
4890
4910
try :
4891
- history_json = self .history .to_json ()
4892
- compressed_bytes = lzma .compress (history_json .encode (encoding = 'utf-8' ))
4911
+ import lzma as compress_lib
4912
+ except ModuleNotFoundError : # pragma: no cover
4913
+ import bz2 as compress_lib # type: ignore[no-redef]
4914
+
4915
+ self .history .truncate (self ._persistent_history_length )
4916
+ history_json = self .history .to_json ()
4917
+ compressed_bytes = compress_lib .compress (history_json .encode (encoding = 'utf-8' ))
4893
4918
4919
+ try :
4894
4920
with open (self .persistent_history_file , 'wb' ) as fobj :
4895
4921
fobj .write (compressed_bytes )
4896
4922
except OSError as ex :
0 commit comments