From d17c77f453f69ef3de963ed4783c8165ebcba49a Mon Sep 17 00:00:00 2001 From: Ian Thomas Date: Wed, 2 Oct 2024 14:05:59 +0100 Subject: [PATCH] Small changes --- ipykernel/iostream.py | 8 ++++---- ipykernel/kernelbase.py | 18 ++++++++---------- ipykernel/subshell_manager.py | 8 ++++---- ipykernel/zmqshell.py | 9 +++++---- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/ipykernel/iostream.py b/ipykernel/iostream.py index b83f8120..beca44b1 100644 --- a/ipykernel/iostream.py +++ b/ipykernel/iostream.py @@ -170,10 +170,10 @@ async def _handle_event(self): for _ in range(n_events): event_f = self._events.popleft() event_f() - except Exception as e: + except Exception: if self.thread.__stop.is_set(): return - raise e + raise def _setup_pipe_in(self): """setup listening pipe for IOPub from forked subprocesses""" @@ -202,10 +202,10 @@ async def _handle_pipe_msgs(self): try: while True: await self._handle_pipe_msg() - except Exception as e: + except Exception: if self.thread.__stop.is_set(): return - raise e + raise async def _handle_pipe_msg(self, msg=None): """handle a pipe message from a subprocess""" diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index 157e2dc2..84db8660 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -262,10 +262,10 @@ async def process_control(self): try: while True: await self.process_control_message() - except BaseException as e: + except BaseException: if self.control_stop.is_set(): return - raise e + raise async def process_control_message(self, msg=None): """dispatch control requests""" @@ -392,10 +392,10 @@ async def shell_channel_thread_main(self): socket.send_multipart(msg, copy=False) except Exception: self.log.error("Invalid message", exc_info=True) # noqa: G201 - except BaseException as e: + except BaseException: if self.shell_stop.is_set(): return - raise e + raise async def shell_main(self, subshell_id: str | None): """Main loop for a single subshell.""" @@ -426,10 +426,10 @@ async def process_shell(self, socket=None): try: while True: await self.process_shell_message(socket=socket) - except BaseException as e: + except BaseException: if self.shell_stop.is_set(): return - raise e + raise async def process_shell_message(self, msg=None, socket=None): # If socket is None kernel subshells are not supported so use socket=shell_socket. @@ -718,8 +718,7 @@ async def execute_request(self, socket, ident, parent): cell_meta = parent.get("metadata", {}) cell_id = cell_meta.get("cellId") except Exception: - self.log.error("Got bad msg: ") - self.log.error("%s", parent) + self.log.error("Got bad msg from parent: %s", parent) return stop_on_error = content.get("stop_on_error", True) @@ -1101,8 +1100,7 @@ async def delete_subshell_request(self, socket, ident, parent): content = parent["content"] subshell_id = content["subshell_id"] except Exception: - self.log.error("Got bad msg: ") - self.log.error("%s", parent) + self.log.error("Got bad msg from parent: %s", parent) return # This should only be called in the control thread if it exists. diff --git a/ipykernel/subshell_manager.py b/ipykernel/subshell_manager.py index 7c64190e..3e4a9fd9 100644 --- a/ipykernel/subshell_manager.py +++ b/ipykernel/subshell_manager.py @@ -154,7 +154,7 @@ def subshell_id_from_thread_id(self, thread_id) -> str | None: for id, subshell in self._cache.items(): if subshell.thread.ident == thread_id: return id - msg = f"Thread id '{thread_id} does not correspond to a subshell of this kernel" + msg = f"Thread id {thread_id!r} does not correspond to a subshell of this kernel" raise RuntimeError(msg) def _create_inproc_pair_socket( @@ -235,11 +235,11 @@ async def _listen_for_subshell_reply(self, subshell_id: str | None) -> None: msg = await shell_channel_socket.recv_multipart(copy=False) with self._lock_shell_socket: await self._shell_socket.send_multipart(msg) - except BaseException as e: + except BaseException: if not self._is_subshell(subshell_id): # Subshell no longer exists so exit gracefully return - raise e + raise async def _process_control_request(self, request, subshell_task) -> dict[str, t.Any]: """Process a control request message received on the control inproc @@ -259,7 +259,7 @@ async def _process_control_request(self, request, subshell_task) -> dict[str, t. elif type == "list": reply["subshell_id"] = self.list_subshell() else: - msg = f"Unrecognised message type {type}" + msg = f"Unrecognised message type {type!r}" raise RuntimeError(msg) except BaseException as err: reply = { diff --git a/ipykernel/zmqshell.py b/ipykernel/zmqshell.py index 6c1273cd..3f97e817 100644 --- a/ipykernel/zmqshell.py +++ b/ipykernel/zmqshell.py @@ -16,9 +16,9 @@ import os import sys +import threading import warnings from pathlib import Path -from threading import local from IPython.core import page, payloadpage from IPython.core.autocall import ZMQExitAutocall @@ -69,7 +69,7 @@ def _flush_streams(self): @default("_thread_local") def _default_thread_local(self): """Initialize our thread local storage""" - return local() + return threading.local() @property def _hooks(self): @@ -441,6 +441,9 @@ def autosave(self, arg_s): @line_magic def subshell(self, arg_s): + """ + List all current subshells + """ from ipykernel.kernelapp import IPKernelApp if not IPKernelApp.initialized(): @@ -454,8 +457,6 @@ def subshell(self, arg_s): print("Kernel does not support subshells") return - import threading - thread_id = threading.current_thread().ident manager = kernel.shell_channel_thread.manager try: