Skip to content

Commit 6058198

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 6058198

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ extend-ignore-re = [
121121
# Lorem ipsum.
122122
"Nam",
123123
"varius",
124+
"Writeable",
124125
]
125126

126127
locale = 'en-us' # US English.

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)