From 38045cc08f5de6a4b2faf9b0d4c54638b666a343 Mon Sep 17 00:00:00 2001 From: Ian Thomas Date: Fri, 16 Aug 2024 11:13:40 +0100 Subject: [PATCH 1/2] Support ipykernel's use of anyio in test_signal_kernel_subprocesses --- tests/signalkernel.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/signalkernel.py b/tests/signalkernel.py index 65fdb687..596928f1 100644 --- a/tests/signalkernel.py +++ b/tests/signalkernel.py @@ -30,7 +30,7 @@ async def shutdown_request(self, stream, ident, parent): if os.environ.get("NO_SHUTDOWN_REPLY") != "1": await super().shutdown_request(stream, ident, parent) - def do_execute( + async def do_execute( self, code, silent, store_history=True, user_expressions=None, allow_stdin=False ): code = code.strip() @@ -47,12 +47,30 @@ def do_execute( elif code == "env": reply["user_expressions"]["env"] = os.getenv("TEST_VARS", "") elif code == "sleep": - try: - time.sleep(10) - except KeyboardInterrupt: - reply["user_expressions"]["interrupted"] = True + import ipykernel + if ipykernel.version_info < (7, 0): + # ipykernel before anyio. + try: + time.sleep(10) + except KeyboardInterrupt: + reply["user_expressions"]["interrupted"] = True + else: + reply["user_expressions"]["interrupted"] = False else: + # ipykernel after anyio. + from anyio import open_signal_receiver, create_task_group, sleep + + async def signal_handler(cancel_scope, reply): + with open_signal_receiver(signal.SIGINT) as signals: + async for _ in signals: + reply["user_expressions"]["interrupted"] = True + cancel_scope.cancel() + return + reply["user_expressions"]["interrupted"] = False + async with create_task_group() as tg: + tg.start_soon(signal_handler, tg.cancel_scope, reply) + tg.start_soon(sleep, 10) else: reply["status"] = "error" reply["ename"] = "Error" From 2692565f263f767c5845d9f0330f63559b8e0b65 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Aug 2024 10:44:01 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/signalkernel.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/signalkernel.py b/tests/signalkernel.py index 596928f1..74e9fa42 100644 --- a/tests/signalkernel.py +++ b/tests/signalkernel.py @@ -48,6 +48,7 @@ async def do_execute( reply["user_expressions"]["env"] = os.getenv("TEST_VARS", "") elif code == "sleep": import ipykernel + if ipykernel.version_info < (7, 0): # ipykernel before anyio. try: @@ -57,8 +58,8 @@ async def do_execute( else: reply["user_expressions"]["interrupted"] = False else: - # ipykernel after anyio. - from anyio import open_signal_receiver, create_task_group, sleep + # ipykernel after anyio. + from anyio import create_task_group, open_signal_receiver, sleep async def signal_handler(cancel_scope, reply): with open_signal_receiver(signal.SIGINT) as signals: