Skip to content

Commit 2483efa

Browse files
Fix: handle surrogate pairs in FileHistory.store_string
FileHistory.store_string crashes with UnicodeEncodeError when the input string contains surrogate pairs (e.g., from Windows clipboard containing emoji). Add errors='replace' to gracefully handle invalid surrogates by replacing them with the Unicode replacement character. Fixes #2061
1 parent 940af53 commit 2483efa

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/prompt_toolkit/history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def store_string(self, string: str) -> None:
300300
with open(self.filename, "ab") as f:
301301

302302
def write(t: str) -> None:
303-
f.write(t.encode("utf-8"))
303+
f.write(t.encode("utf-8", errors="replace"))
304304

305305
write(f"\n# {datetime.datetime.now()}\n")
306306
for line in string.split("\n"):

0 commit comments

Comments
 (0)