@@ -4813,7 +4813,12 @@ def _initialize_history(self, hist_file: str) -> None:
4813
4813
to this file when the application exits.
4814
4814
"""
4815
4815
import json
4816
- import lzma
4816
+ try :
4817
+ import lzma
4818
+ bz2 = None
4819
+ except ModuleNotFoundError : # pragma: no cover
4820
+ lzma = None
4821
+ import bz2
4817
4822
4818
4823
self .history = History ()
4819
4824
# with no persistent history, nothing else in this method is relevant
@@ -4841,7 +4846,10 @@ def _initialize_history(self, hist_file: str) -> None:
4841
4846
try :
4842
4847
with open (hist_file , 'rb' ) as fobj :
4843
4848
compressed_bytes = fobj .read ()
4844
- history_json = lzma .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
4849
+ if lzma is not None :
4850
+ history_json = lzma .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
4851
+ else :
4852
+ history_json = bz2 .decompress (compressed_bytes ).decode (encoding = 'utf-8' )
4845
4853
self .history = History .from_json (history_json )
4846
4854
except FileNotFoundError :
4847
4855
# Just use an empty history
@@ -4879,15 +4887,23 @@ def _initialize_history(self, hist_file: str) -> None:
4879
4887
4880
4888
def _persist_history (self ) -> None :
4881
4889
"""Write history out to the persistent history file as compressed JSON"""
4882
- import lzma
4890
+ try :
4891
+ import lzma
4892
+ bz2 = None
4893
+ except ModuleNotFoundError : # pragma: no cover
4894
+ lzma = None
4895
+ import bz2
4883
4896
4884
4897
if not self .persistent_history_file :
4885
4898
return
4886
4899
4887
4900
self .history .truncate (self ._persistent_history_length )
4888
4901
try :
4889
4902
history_json = self .history .to_json ()
4890
- compressed_bytes = lzma .compress (history_json .encode (encoding = 'utf-8' ))
4903
+ if lzma is not None :
4904
+ compressed_bytes = lzma .compress (history_json .encode (encoding = 'utf-8' ))
4905
+ else :
4906
+ compressed_bytes = bz2 .compress (history_json .encode (encoding = 'utf-8' ))
4891
4907
4892
4908
with open (self .persistent_history_file , 'wb' ) as fobj :
4893
4909
fobj .write (compressed_bytes )
0 commit comments