Skip to content

Commit 805db65

Browse files
committed
gh-119842: Honor PyOS_InputHook in the new REPL
1 parent b9965ef commit 805db65

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Lib/_pyrepl/reader.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from contextlib import contextmanager
2525
from dataclasses import dataclass, field, fields
2626
import unicodedata
27+
import ctypes
2728
from _colorize import can_colorize, ANSIColors # type: ignore[import-not-found]
2829

2930

@@ -239,6 +240,7 @@ class Reader:
239240
lxy: tuple[int, int] = field(init=False)
240241
calc_screen: CalcScreen = field(init=False)
241242
scheduled_commands: list[str] = field(default_factory=list)
243+
input_hook_addr: ctypes.c_void_p | None = None
242244

243245
def __post_init__(self) -> None:
244246
# Enable the use of `insert` without a `prepare` call - necessary to
@@ -643,6 +645,7 @@ def handle1(self, block: bool = True) -> bool:
643645
self.dirty = True
644646

645647
while True:
648+
self.call_PyOS_InputHook()
646649
event = self.console.get_event(block)
647650
if not event: # can only happen if we're not blocking
648651
return False
@@ -701,3 +704,10 @@ def bind(self, spec: KeySpec, command: CommandName) -> None:
701704
def get_unicode(self) -> str:
702705
"""Return the current buffer as a unicode string."""
703706
return "".join(self.buffer)
707+
708+
def call_PyOS_InputHook(self):
709+
if self.input_hook_addr is None:
710+
self.input_hook_addr = ctypes.c_void_p.in_dll(ctypes.pythonapi, 'PyOS_InputHook').value
711+
if not self.input_hook_addr:
712+
return
713+
ctypes.PYFUNCTYPE(ctypes.c_int)(self.input_hook_addr)()

Lib/test/test_pyrepl/test_reader.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import itertools
22
import functools
33
from unittest import TestCase
4+
from unittest.mock import MagicMock
45

56
from .support import handle_all_events, handle_events_narrow_console, code_to_events, prepare_reader
7+
from test.support import import_helper
68
from _pyrepl.console import Event
79

810

@@ -176,3 +178,24 @@ def test_newline_within_block_trailing_whitespace(self):
176178
)
177179
self.assert_screen_equals(reader, expected)
178180
self.assertTrue(reader.finished)
181+
182+
def test_input_hook_is_called_if_set(self):
183+
import_helper.import_module('ctypes')
184+
from ctypes import pythonapi, c_int, CFUNCTYPE, c_void_p, POINTER, cast
185+
186+
CMPFUNC = CFUNCTYPE(c_int)
187+
the_mock = MagicMock()
188+
189+
def input_hook():
190+
the_mock()
191+
return 0
192+
193+
the_input_hook = CMPFUNC(input_hook)
194+
prev_value = c_void_p.in_dll(pythonapi, "PyOS_InputHook").value
195+
try:
196+
c_void_p.in_dll(pythonapi, "PyOS_InputHook").value = cast(the_input_hook, c_void_p).value
197+
events = code_to_events("a")
198+
reader, _ = handle_all_events(events)
199+
the_mock.assert_called()
200+
finally:
201+
c_void_p.in_dll(pythonapi, "PyOS_InputHook").value = prev_value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Honor :c:func:`PyOS_InputHook` in the new REPL. Patch by Pablo Galindo

0 commit comments

Comments
 (0)