FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-136595: Normalize surrogate pairs in REPL input to fix UnicodeEnco… by vedant713 · Pull Request #136639 · python/cpython · GitHub

Repository navigation

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 8 additions & 1 deletion Lib/_pyrepl/reader.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Back | FazBrowse Home | New Git URL