gh-119310: Fix encoding when reading old history file (#121779)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
aorcajo 2024-09-06 15:40:29 +02:00 committed by GitHub
parent eca3fe40c2
commit e95984826e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 59 additions and 6 deletions

View file

@ -427,12 +427,16 @@ class _ReadlineWrapper:
history = self.get_reader().history
with open(os.path.expanduser(filename), 'rb') as f:
lines = [line.decode('utf-8', errors='replace') for line in f.read().split(b'\n')]
is_editline = f.readline().startswith(b"_HiStOrY_V2_")
if is_editline:
encoding = "unicode-escape"
else:
f.seek(0)
encoding = "utf-8"
lines = [line.decode(encoding, errors='replace') for line in f.read().split(b'\n')]
buffer = []
for line in lines:
# Ignore readline history file header
if line.startswith("_HiStOrY_V2_"):
continue
if line.endswith("\r"):
buffer.append(line+'\n')
else: