@@ -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,16 +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
- try :
4819
- import lzma
4820
- bz2 = None
4821
- except ModuleNotFoundError : # pragma: no cover
4822
- lzma = None
4823
- import bz2
4824
-
4825
4817
self .history = History ()
4826
- # with no persistent history, nothing else in this method is relevant
4818
+
4819
+ # With no persistent history, nothing else in this method is relevant
4827
4820
if not hist_file :
4828
4821
self .persistent_history_file = hist_file
4829
4822
return
@@ -4844,31 +4837,60 @@ def _initialize_history(self, hist_file: str) -> None:
4844
4837
self .perror (f"Error creating persistent history file directory '{ hist_file_dir } ': { ex } " )
4845
4838
return
4846
4839
4847
- # Read and process history file
4840
+ # Read history file
4848
4841
try :
4849
4842
with open (hist_file , 'rb' ) as fobj :
4850
4843
compressed_bytes = fobj .read ()
4851
- if lzma is not None :
4852
- history_json = lzma .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
4853
- else :
4854
- history_json = bz2 .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
4855
- self .history = History .from_json (history_json )
4856
4844
except FileNotFoundError :
4857
- # Just use an empty history
4858
- pass
4845
+ compressed_bytes = bytes ()
4859
4846
except OSError as ex :
4860
4847
self .perror (f"Cannot read persistent history file '{ hist_file } ': { ex } " )
4861
4848
return
4862
- 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 :
4863
4885
self .perror (
4864
- f"Error processing persistent history file '{ hist_file } ': { ex } \n "
4886
+ f"Error processing persistent history data '{ hist_file } ': { ex } \n "
4865
4887
f"The history file will be recreated when this application exits."
4866
4888
)
4889
+ return
4867
4890
4868
4891
self .history .start_session ()
4869
- self .persistent_history_file = hist_file
4870
4892
4871
- # populate readline history
4893
+ # Populate readline history
4872
4894
if rl_type != RlType .NONE :
4873
4895
last = None
4874
4896
for item in self .history :
@@ -4880,33 +4902,21 @@ def _initialize_history(self, hist_file: str) -> None:
4880
4902
readline .add_history (line )
4881
4903
last = line
4882
4904
4883
- # register a function to write history at save
4884
- # if the history file is in plain text format from 0.9.12 or lower
4885
- # this will fail, and the history in the plain text file will be lost
4886
- import atexit
4887
-
4888
- atexit .register (self ._persist_history )
4889
-
4890
4905
def _persist_history (self ) -> None :
4891
4906
"""Write history out to the persistent history file as compressed JSON"""
4892
- try :
4893
- import lzma
4894
- bz2 = None
4895
- except ModuleNotFoundError : # pragma: no cover
4896
- lzma = None
4897
- import bz2
4898
-
4899
4907
if not self .persistent_history_file :
4900
4908
return
4901
4909
4902
- self .history .truncate (self ._persistent_history_length )
4903
4910
try :
4904
- history_json = self .history .to_json ()
4905
- if lzma is not None :
4906
- compressed_bytes = lzma .compress (history_json .encode (encoding = 'utf-8' ))
4907
- else :
4908
- compressed_bytes = bz2 .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]
4909
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' ))
4918
+
4919
+ try :
4910
4920
with open (self .persistent_history_file , 'wb' ) as fobj :
4911
4921
fobj .write (compressed_bytes )
4912
4922
except OSError as ex :
0 commit comments