Skip to content

Commit 9ff5507

Browse files
committed
gh-156186: Join UTF-16 surrogate pairs on Windows
1 parent 006f06f commit 9ff5507

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎Lib/_pyrepl/windows_console.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ def getpending(self) -> Event:
674674
e.data += e2.data
675675
e.raw += e2.raw
676676

677+
text = ""
677678
recs, rec_count = self._read_input_bulk(1024)
678679
for i in range(rec_count):
679680
rec = recs[i]
@@ -690,8 +691,13 @@ def getpending(self) -> Event:
690691
continue
691692
if ch == "\r":
692693
ch = "\n"
693-
e.data += ch
694-
e.raw += ch.encode(self.event_queue.encoding, "replace")
694+
text += ch
695+
# Each record on Windows holds a UTF-16 code unit.
696+
# A surrogate pair must be joined before encoding,
697+
# an unpaired half becomes U+FFFD.
698+
text = text.encode("utf-16-le", "surrogatepass").decode("utf-16-le", "replace")
699+
e.data += text
700+
e.raw += text.encode(self.event_queue.encoding, "replace")
695701
return e
696702

697703
def wait_for_event(self, timeout: float | None) -> bool:

0 commit comments

Comments
 (0)