GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-131878: Fix input of unicode characters with two or more code points in new pyrepl on Windows by sergey-miryanov · Pull Request #131901 · python/cpython

gh-131878: Fix input of unicode characters with two or more code points in new pyrepl on Windows #131901

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.

Sign up for GitHub

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

Merged
ambv merged 13 commits into python:main from sergey-miryanov:gh-131878-fix-unicode-input-in-new-pyrepl-for-long-unicode-chars
May 5, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-131878: Fix input of unicode characters with two or more code points in new pyrepl on Windows #131901

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
04812d4
Fix input of long unicode characters
sergey-miryanov Mar 30, 2025
9203df4
Add new entry
sergey-miryanov Mar 30, 2025
9d7e3e5
Do not change simple_interact
sergey-miryanov Mar 30, 2025
7fd3418
Do not use bytearray to convert char to bytes
sergey-miryanov Apr 26, 2025
a2fb7ee
Merge branch 'main' into gh-131878-fix-unicode-input-in-new-pyrepl-fo…
sergey-miryanov Apr 26, 2025
b1fa88d
Push char one by one in windows_console
sergey-miryanov Apr 27, 2025
925f3a2
Apply Chris Eibl suggestionы and use only int and bytes in eventqueue…
sergey-miryanov Apr 27, 2025
aa56581
Do not use finally to reset keymap
sergey-miryanov Apr 27, 2025
1eaeca3
Rename tests along Chris Eibl suggestions
sergey-miryanov Apr 27, 2025
68d978e
Fix tests
sergey-miryanov Apr 28, 2025
7f8fb7f
Add comment
sergey-miryanov Apr 28, 2025
9519208
Update comments in the test
sergey-miryanov Apr 29, 2025
a855ba8
Use to_bytes
sergey-miryanov May 5, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
Fix input of long unicode characters
- with two codepoints or more
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@sergey-miryanov
sergey-miryanov committed Mar 30, 2025
commit 04812d4750da7c7b52072f6cdf64e14c4e0717f5

There are no files selected for viewing

23 changes: 15 additions & 8 deletions 23 Lib/_pyrepl/base_eventqueue.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ def push(self, char: int | bytes | str) -> None:
"""
Processes a character by updating the buffer and handling special key mappings.
"""
ord_char = char if isinstance(char, int) else ord(char)
if ord_char > 255:
assert isinstance(char, str)
char = bytes(char.encode(self.encoding, "replace"))

if isinstance(char, bytes):
self.buf.extend(char)
else:
char = bytes(bytearray((ord_char,)))
self.buf.append(ord_char)

ord_char = char if isinstance(char, int) else ord(char)
if ord_char > 255:
assert isinstance(char, str)
char = bytes(char.encode(self.encoding, "replace"))
self.buf.extend(char)
else:
char = bytes(bytearray((ord_char,)))
Comment thread
sergey-miryanov marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

self.buf.append(ord_char)

if char in self.keymap:
if self.keymap is self.compiled_keymap:
Expand Down Expand Up @@ -108,7 +113,9 @@ def push(self, char: int | bytes | str) -> None:
try:
decoded = bytes(self.buf).decode(self.encoding)
except UnicodeError:
return
self.flush_buf()
raise
Comment thread
sergey-miryanov marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

else:
self.insert(Event('key', decoded, self.flush_buf()))
self.keymap = self.compiled_keymap
finally:
self.keymap = self.compiled_keymap
5 changes: 5 additions & 0 deletions 5 Lib/_pyrepl/simple_interact.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,8 @@ def maybe_run_command(statement: str) -> bool:
except MemoryError:
console.write("\nMemoryError\n")
console.resetbuffer()
# except SystemExit:
# break
# except:
# console.showtraceback()
# console.resetbuffer()
3 changes: 2 additions & 1 deletion 3 Lib/_pyrepl/windows_console.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,8 @@ def get_event(self, block: bool = True) -> Event | None:
return None
elif self.__vt_support:
# If virtual terminal is enabled, scanning VT sequences
self.event_queue.push(rec.Event.KeyEvent.uChar.UnicodeChar)
key_bytes = raw_key.encode(self.event_queue.encoding)
self.event_queue.push(key_bytes)
Comment thread
sergey-miryanov marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

continue

if key_event.dwControlKeyState & ALT_ACTIVE:
Expand Down
71 changes: 71 additions & 0 deletions 71 Lib/test/test_pyrepl/test_eventqueue.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,77 @@ def test_push_unicode_character(self):
self.assertEqual(eq.events[0].evt, "key")
self.assertEqual(eq.events[0].data, "ч")

def test_push_unicode_character_as_bytes(self):
eq = self.make_eventqueue()
eq.keymap = {}

eq.push("ч".encode(eq.encoding, "replace"))
e = eq.get()
self.assertEqual(e.evt, "key")
self.assertEqual(e.data, "ч")

def test_push_long_unicode_character_as_bytes(self):
eq = self.make_eventqueue()
eq.keymap = {}

def _event(evt, data, raw=None):
r = raw if raw is not None else data.encode(eq.encoding)
e = Event(evt, data, r)
return e

def _push(keys):
for k in keys:
eq.push(k)

_push("\x1b[200")
eq.push("ñ".encode(eq.encoding, "replace"))
_push("\x1b[201")

self.assertEqual(eq.get(), _event("key", "\x1b"))
self.assertEqual(eq.get(), _event("key", "["))
self.assertEqual(eq.get(), _event("key", "2"))
self.assertEqual(eq.get(), _event("key", "0"))
self.assertEqual(eq.get(), _event("key", "0"))

self.assertEqual(eq.get(), _event("key", "ñ", bytearray(b'\xc3\xb1')))
Comment thread
sergey-miryanov marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


self.assertEqual(eq.get(), _event("key", "\x1b"))
self.assertEqual(eq.get(), _event("key", "["))
self.assertEqual(eq.get(), _event("key", "2"))
self.assertEqual(eq.get(), _event("key", "0"))
self.assertEqual(eq.get(), _event("key", "1"))

def test_push_long_unicode_character(self):
eq = self.make_eventqueue()
eq.keymap = {}

def _event(evt, data, raw=None):
r = raw if raw is not None else data.encode(eq.encoding)
e = Event(evt, data, r)
return e

def _push(keys):
for k in keys:
eq.push(k)

_push("\x1b[200")
msg = "'utf-8' codec can't decode byte 0xf1 in position 0: unexpected end of data"
with self.assertRaisesRegex(UnicodeDecodeError, msg):
eq.push("ñ")
_push("\x1b[201")

self.assertEqual(eq.get(), _event("key", "\x1b"))
self.assertEqual(eq.get(), _event("key", "["))
self.assertEqual(eq.get(), _event("key", "2"))
self.assertEqual(eq.get(), _event("key", "0"))
self.assertEqual(eq.get(), _event("key", "0"))

self.assertEqual(eq.get(), _event("key", "\x1b"))
self.assertEqual(eq.get(), _event("key", "["))
self.assertEqual(eq.get(), _event("key", "2"))
self.assertEqual(eq.get(), _event("key", "0"))
self.assertEqual(eq.get(), _event("key", "1"))


@unittest.skipIf(support.MS_WINDOWS, "No Unix event queue on Windows")
class TestUnixEventQueue(EventQueueTestBase, unittest.TestCase):
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.