@@ -428,18 +428,14 @@ def get_current_history_length(self) -> int:
428
428
return len (self .get_reader ().history )
429
429
430
430
@staticmethod
431
- def _analyze_history_file (filename : str | IO [bytes ]) -> tuple [bool , str ]:
432
- is_editline = False
431
+ def _is_editline_history (filename : str | IO [bytes ]) -> bool :
433
432
if isinstance (filename , str ):
434
433
if not os .path .exists (filename ):
435
- return False , "utf-8"
434
+ return False
436
435
with open (filename , "rb" ) as f :
437
- is_editline = f .readline ().startswith (_EDITLINE_BYTES_MARKER )
438
- else :
439
- is_editline = f .readline ().startswith (_EDITLINE_BYTES_MARKER )
440
- if is_editline :
441
- return True , "unicode-escape"
442
- return False , "utf-8"
436
+ return f .readline ().startswith (_EDITLINE_BYTES_MARKER )
437
+ return False
438
+ return filename .readline ().startswith (_EDITLINE_BYTES_MARKER )
443
439
444
440
def read_history_file (self , filename : str = gethistoryfile ()) -> None :
445
441
# multiline extension (really a hack) for the end of lines that
@@ -449,9 +445,11 @@ def read_history_file(self, filename: str = gethistoryfile()) -> None:
449
445
history = self .get_reader ().history
450
446
451
447
with open (os .path .expanduser (filename ), 'rb' ) as f :
452
- is_editline , encoding = self ._analyze_history_file (f )
453
- if not is_editline :
448
+ if self ._is_editline_history (f ):
449
+ encoding = "unicode-escape"
450
+ else :
454
451
f .seek (0 )
452
+ encoding = "utf-8"
455
453
456
454
lines = [line .decode (encoding , errors = 'replace' ) for line in f .read ().split (b'\n ' )]
457
455
buffer = []
@@ -472,8 +470,8 @@ def write_history_file(self, filename: str = gethistoryfile()) -> None:
472
470
history = self .get_reader ().get_trimmed_history (maxlength )
473
471
474
472
filename = os .path .expanduser (filename )
475
- is_editline , encoding = self ._analyze_history_file (filename )
476
- with open (filename , "w" , encoding = encoding , newline = "\n " ) as f :
473
+ is_editline = self ._is_editline_history (filename )
474
+ with open (filename , "w" , encoding = "utf-8" , newline = "\n " ) as f :
477
475
if is_editline :
478
476
f .write (f"{ _EDITLINE_MARKER } \n " )
479
477
for entry in history :
@@ -487,8 +485,7 @@ def append_history_file(self, filename: str = gethistoryfile()) -> None:
487
485
history = reader .get_trimmed_history (length )
488
486
489
487
filename = os .path .expanduser (filename )
490
- _ , encoding = self ._analyze_history_file (filename )
491
- with open (filename , "a" , encoding = encoding , newline = "\n " ) as f :
488
+ with open (filename , "a" , encoding = "utf-8" , newline = "\n " ) as f :
492
489
for entry in history :
493
490
entry = entry .replace ("\n " , "\r \n " ) # multiline history support
494
491
f .write (entry + "\n " )
0 commit comments