Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit805db65

Browse files
committed
gh-119842: Honor PyOS_InputHook in the new REPL
1 parentb9965ef commit805db65

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
fromcontextlibimportcontextmanager
2525
fromdataclassesimportdataclass,field,fields
2626
importunicodedata
27+
importctypes
2728
from_colorizeimportcan_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
whileTrue:
648+
self.call_PyOS_InputHook()
646649
event=self.console.get_event(block)
647650
ifnotevent:# can only happen if we're not blocking
648651
returnFalse
@@ -701,3 +704,10 @@ def bind(self, spec: KeySpec, command: CommandName) -> None:
701704
defget_unicode(self)->str:
702705
"""Return the current buffer as a unicode string."""
703706
return"".join(self.buffer)
707+
708+
defcall_PyOS_InputHook(self):
709+
ifself.input_hook_addrisNone:
710+
self.input_hook_addr=ctypes.c_void_p.in_dll(ctypes.pythonapi,'PyOS_InputHook').value
711+
ifnotself.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
importitertools
22
importfunctools
33
fromunittestimportTestCase
4+
fromunittest.mockimportMagicMock
45

56
from .supportimporthandle_all_events,handle_events_narrow_console,code_to_events,prepare_reader
7+
fromtest.supportimportimport_helper
68
from_pyrepl.consoleimportEvent
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+
deftest_input_hook_is_called_if_set(self):
183+
import_helper.import_module('ctypes')
184+
fromctypesimportpythonapi,c_int,CFUNCTYPE,c_void_p,POINTER,cast
185+
186+
CMPFUNC=CFUNCTYPE(c_int)
187+
the_mock=MagicMock()
188+
189+
definput_hook():
190+
the_mock()
191+
return0
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)

[8]ページ先頭

©2009-2025 Movatter.jp