Skip to content

Commit 1c7e2d9

Browse files
committed
fixup!
1 parent 9d31898 commit 1c7e2d9

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

Lib/_pyrepl/readline.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -428,18 +428,14 @@ def get_current_history_length(self) -> int:
428428
return len(self.get_reader().history)
429429

430430
@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:
433432
if isinstance(filename, str):
434433
if not os.path.exists(filename):
435-
return False, "utf-8"
434+
return False
436435
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)
443439

444440
def read_history_file(self, filename: str = gethistoryfile()) -> None:
445441
# multiline extension (really a hack) for the end of lines that
@@ -449,9 +445,11 @@ def read_history_file(self, filename: str = gethistoryfile()) -> None:
449445
history = self.get_reader().history
450446

451447
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:
454451
f.seek(0)
452+
encoding = "utf-8"
455453

456454
lines = [line.decode(encoding, errors='replace') for line in f.read().split(b'\n')]
457455
buffer = []
@@ -472,8 +470,8 @@ def write_history_file(self, filename: str = gethistoryfile()) -> None:
472470
history = self.get_reader().get_trimmed_history(maxlength)
473471

474472
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:
477475
if is_editline:
478476
f.write(f"{_EDITLINE_MARKER}\n")
479477
for entry in history:
@@ -487,8 +485,7 @@ def append_history_file(self, filename: str = gethistoryfile()) -> None:
487485
history = reader.get_trimmed_history(length)
488486

489487
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:
492489
for entry in history:
493490
entry = entry.replace("\n", "\r\n") # multiline history support
494491
f.write(entry + "\n")

0 commit comments

Comments
 (0)