From 033510e11dff742d9626b9fd895925ac77f566f1 Mon Sep 17 00:00:00 2001 From: Ɓukasz Langa Date: Fri, 6 Sep 2024 21:28:29 +0200 Subject: gh-120221: Support KeyboardInterrupt in asyncio REPL (#123795) This switches the main pyrepl event loop to always be non-blocking so that it can listen to incoming interruptions from other threads. This also resolves invalid display of exceptions from other threads (gh-123178). This also fixes freezes with pasting and an active input hook. --- Lib/asyncio/__main__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'Lib/asyncio/__main__.py') diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py index 111b7d92367..5120140e061 100644 --- a/Lib/asyncio/__main__.py +++ b/Lib/asyncio/__main__.py @@ -127,6 +127,15 @@ class REPLThread(threading.Thread): loop.call_soon_threadsafe(loop.stop) + def interrupt(self) -> None: + if not CAN_USE_PYREPL: + return + + from _pyrepl.simple_interact import _get_reader + r = _get_reader() + if r.threading_hook is not None: + r.threading_hook.add("") # type: ignore + if __name__ == '__main__': sys.audit("cpython.run_stdin") @@ -184,6 +193,7 @@ if __name__ == '__main__': keyboard_interrupted = True if repl_future and not repl_future.done(): repl_future.cancel() + repl_thread.interrupt() continue else: break -- cgit v1.2.3