Skip to content

gh-136595: Normalize surrogate pairs in REPL input to fix UnicodeEnco… #136639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
# syntax classes
SYNTAX_WHITESPACE, SYNTAX_WORD, SYNTAX_SYMBOL = range(3)

def normalize_surrogates(s: str) -> str:
# Encode with surrogatepass, decode to normalize surrogate pairs
try:
return s.encode('utf-16', 'surrogatepass').decode('utf-16')
except UnicodeEncodeError:
return s # fallback if encoding somehow fails

def make_default_syntax_table() -> dict[str, int]:
# XXX perhaps should use some unicodedata here?
Expand Down Expand Up @@ -759,4 +765,5 @@ def bind(self, spec: KeySpec, command: CommandName) -> None:

def get_unicode(self) -> str:
"""Return the current buffer as a unicode string."""
return "".join(self.buffer)
text = "".join(self.buffer)
return normalize_surrogates(text)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a crash in the REPL on Windows when typing Unicode characters outside the Basic Multilingual Plane (≥ U+10000), such as emoji. These characters are now properly handled as surrogate pairs.
Loading