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

jupyter-run: avoid traceback for NoSuchKernel #994

Merged
merged 2 commits into from
Sep 29, 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
5 changes: 4 additions & 1 deletion jupyter_client/consoleapp.py
Original file line number Diff line number Diff line change
@@ -306,8 +306,11 @@ def init_kernel_manager(self) -> None:
parent=self,
data_dir=self.data_dir,
)
# access kernel_spec to ensure the NoSuchKernel error is raised
# if it's going to be
kernel_spec = self.kernel_manager.kernel_spec # noqa: F841
except NoSuchKernel:
self.log.critical("Could not find kernel %s", self.kernel_name)
self.log.critical("Could not find kernel %r", self.kernel_name)
self.exit(1) # type:ignore[attr-defined]

self.kernel_manager = t.cast(KernelManager, self.kernel_manager)
1 change: 0 additions & 1 deletion jupyter_client/kernelspec.py
Original file line number Diff line number Diff line change
@@ -281,7 +281,6 @@ def get_kernel_spec(self, kernel_name: str) -> KernelSpec:

resource_dir = self._find_spec_directory(kernel_name.lower())
if resource_dir is None:
self.log.warning("Kernelspec name %s cannot be found!", kernel_name)
raise NoSuchKernel(kernel_name)

return self._get_kernel_spec_by_name(kernel_name, resource_dir)
29 changes: 7 additions & 22 deletions jupyter_client/runapp.py
Original file line number Diff line number Diff line change
@@ -3,10 +3,9 @@
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import queue
import atexit
import signal
import sys
import time
import typing as t

from jupyter_core.application import JupyterApp, base_aliases, base_flags
@@ -73,7 +72,8 @@ def initialize(self, argv: list[str] | None = None) -> None: # type:ignore[over
super().initialize(argv)
JupyterConsoleApp.initialize(self)
signal.signal(signal.SIGINT, self.handle_sigint)
self.init_kernel_info()
if self.kernel_manager:
atexit.register(self.kernel_manager.shutdown_kernel)

def handle_sigint(self, *args: t.Any) -> None:
"""Handle SIGINT."""
@@ -82,28 +82,11 @@ def handle_sigint(self, *args: t.Any) -> None:
else:
self.log.error("Cannot interrupt kernels we didn't start.\n")

def init_kernel_info(self) -> None:
"""Wait for a kernel to be ready, and store kernel info"""
timeout = self.kernel_timeout
tic = time.time()
self.kernel_client.hb_channel.unpause()
msg_id = self.kernel_client.kernel_info()
while True:
try:
reply = self.kernel_client.get_shell_msg(timeout=1)
except queue.Empty as e:
if (time.time() - tic) > timeout:
msg = "Kernel didn't respond to kernel_info_request"
raise RuntimeError(msg) from e
else:
if reply["parent_header"].get("msg_id") == msg_id:
self.kernel_info = reply["content"]
return

def start(self) -> None:
"""Start the application."""
self.log.debug("jupyter run: starting...")
super().start()
self.kernel_client.wait_for_ready(timeout=self.kernel_timeout)
if self.filenames_to_run:
for filename in self.filenames_to_run:
self.log.debug("jupyter run: executing `%s`", filename)
@@ -112,8 +95,10 @@ def start(self) -> None:
reply = self.kernel_client.execute_interactive(code, timeout=OUTPUT_TIMEOUT)
return_code = 0 if reply["content"]["status"] == "ok" else 1
if return_code:
raise Exception("jupyter-run error running '%s'" % filename)
msg = f"jupyter-run error running '{filename}'"
raise Exception(msg)
else:
self.log.debug("jupyter run: executing from stdin")
code = sys.stdin.read()
reply = self.kernel_client.execute_interactive(code, timeout=OUTPUT_TIMEOUT)
return_code = 0 if reply["content"]["status"] == "ok" else 1
34 changes: 34 additions & 0 deletions tests/test_runapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys
from subprocess import run


def test_runapp(tmp_path):
test_py = tmp_path / "test.py"
with test_py.open("w") as f:
f.write("print('hello')")

p = run(
[sys.executable, "-m", "jupyter_client.runapp", str(test_py)],
capture_output=True,
text=True,
check=True,
)
assert p.stdout.strip() == "hello"


def test_no_such_kernel(tmp_path):
test_py = tmp_path / "test.py"
with test_py.open("w") as f:
f.write("print('hello')")
kernel_name = "nosuchkernel"
p = run(
[sys.executable, "-m", "jupyter_client.runapp", "--kernel", kernel_name, str(test_py)],
capture_output=True,
text=True,
check=False,
)
assert p.returncode
assert "Could not find kernel" in p.stderr
assert kernel_name in p.stderr
# shouldn't show a traceback
assert "Traceback" not in p.stderr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!