Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ipykernel's use of anyio in test_signal_kernel_subprocesses #1034

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions tests/signalkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -47,12 +47,31 @@ 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 create_task_group, open_signal_receiver, 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"
Expand Down
Loading