Skip to content

Commit 2e9062f

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 2e9062f

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
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"):

src/prompt_toolkit/widgets/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def __init__(
209209
if input_processors is None:
210210
input_processors = []
211211

212-
# Writeable attributes.
212+
# Writable attributes.
213213
self.completer = completer
214214
self.complete_while_typing = complete_while_typing
215215
self.lexer = lexer

0 commit comments

Comments
 (0)