diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2024-06-04 18:32:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-04 18:32:43 (GMT) |
commit | d9095194dde27eaabfc0b86a11989cdb9a2acfe1 (patch) | |
tree | 5e06d170d8bb14998dc326c1fa6719631764478b /Lib/_pyrepl | |
parent | bf5e1065f4ec2077c6ca352fc1ad940a76d1f6c9 (diff) | |
download | cpython-d9095194dde27eaabfc0b86a11989cdb9a2acfe1.zip cpython-d9095194dde27eaabfc0b86a11989cdb9a2acfe1.tar.gz cpython-d9095194dde27eaabfc0b86a11989cdb9a2acfe1.tar.bz2 |
gh-119842: Honor PyOS_InputHook in the new REPL (GH-119843)
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Co-authored-by: Michael Droettboom <mdboom@gmail.com>
Diffstat (limited to 'Lib/_pyrepl')
-rw-r--r-- | Lib/_pyrepl/console.py | 12 | ||||
-rw-r--r-- | Lib/_pyrepl/reader.py | 10 | ||||
-rw-r--r-- | Lib/_pyrepl/unix_console.py | 22 | ||||
-rw-r--r-- | Lib/_pyrepl/windows_console.py | 22 |
4 files changed, 56 insertions, 10 deletions
diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index aa0bde8..a8d3f52 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -33,6 +33,7 @@ TYPE_CHECKING = False if TYPE_CHECKING: from typing import IO + from typing import Callable @dataclass @@ -134,8 +135,15 @@ class Console(ABC): ... @abstractmethod - def wait(self) -> None: - """Wait for an event.""" + def wait(self, timeout: float | None) -> bool: + """Wait for an event. The return value is True if an event is + available, False if the timeout has been reached. If timeout is + None, wait forever. The timeout is in milliseconds.""" + ... + + @property + def input_hook(self) -> Callable[[], int] | None: + """Returns the current input hook.""" ... @abstractmethod diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py index f2e68ef..beee776 100644 --- a/Lib/_pyrepl/reader.py +++ b/Lib/_pyrepl/reader.py @@ -650,7 +650,15 @@ class Reader: self.dirty = True while True: - event = self.console.get_event(block) + input_hook = self.console.input_hook + if input_hook: + input_hook() + # We use the same timeout as in readline.c: 100ms + while not self.console.wait(100): + input_hook() + event = self.console.get_event(block=False) + else: + event = self.console.get_event(block) if not event: # can only happen if we're not blocking return False diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py index 4bdb022..2f73a59 100644 --- a/Lib/_pyrepl/unix_console.py +++ b/Lib/_pyrepl/unix_console.py @@ -118,9 +118,12 @@ except AttributeError: def register(self, fd, flag): self.fd = fd - - def poll(self): # note: a 'timeout' argument would be *milliseconds* - r, w, e = select.select([self.fd], [], []) + # note: The 'timeout' argument is received as *milliseconds* + def poll(self, timeout: float | None = None) -> list[int]: + if timeout is None: + r, w, e = select.select([self.fd], [], []) + else: + r, w, e = select.select([self.fd], [], [], timeout/1000) return r poll = MinimalPoll # type: ignore[assignment] @@ -385,11 +388,11 @@ class UnixConsole(Console): break return self.event_queue.get() - def wait(self): + def wait(self, timeout: float | None = None) -> bool: """ Wait for events on the console. """ - self.pollob.poll() + return bool(self.pollob.poll(timeout)) def set_cursor_vis(self, visible): """ @@ -527,6 +530,15 @@ class UnixConsole(Console): self.__posxy = 0, 0 self.screen = [] + @property + def input_hook(self): + try: + import posix + except ImportError: + return None + if posix._is_inputhook_installed(): + return posix._inputhook + def __enable_bracketed_paste(self) -> None: os.write(self.output_fd, b"\x1b[?2004h") diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py index 2277865..f691ca3 100644 --- a/Lib/_pyrepl/windows_console.py +++ b/Lib/_pyrepl/windows_console.py @@ -23,6 +23,8 @@ import io from multiprocessing import Value import os import sys +import time +import msvcrt from abc import ABC, abstractmethod from collections import deque @@ -202,6 +204,15 @@ class WindowsConsole(Console): self.screen = screen self.move_cursor(cx, cy) + @property + def input_hook(self): + try: + import nt + except ImportError: + return None + if nt._is_inputhook_installed(): + return nt._inputhook + def __write_changed_line( self, y: int, oldline: str, newline: str, px_coord: int ) -> None: @@ -460,9 +471,16 @@ class WindowsConsole(Console): processed.""" return Event("key", "", b"") - def wait(self) -> None: + def wait(self, timeout: float | None) -> bool: """Wait for an event.""" - raise NotImplementedError("No wait support") + # Poor man's Windows select loop + start_time = time.time() + while True: + if msvcrt.kbhit(): # type: ignore[attr-defined] + return True + if timeout and time.time() - start_time > timeout: + return False + time.sleep(0.01) def repaint(self) -> None: raise NotImplementedError("No repaint support") |