Skip to content

Commit

Permalink
Support ipykernel's use of anyio in test_signal_kernel_subprocesses (#…
Browse files Browse the repository at this point in the history
…1034)

* Support ipykernel's use of anyio in test_signal_kernel_subprocesses

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Min RK <benjaminrk@gmail.com>
  • Loading branch information
3 people authored Sep 17, 2024
1 parent 5d48881 commit b5cb375
Showing 1 changed file with 24 additions and 5 deletions.
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

0 comments on commit b5cb375

Please sign in to comment.