Skip to content

Commit

Permalink
Refactor execute_request to reduce redundancy and improve consistency (
Browse files Browse the repository at this point in the history
  • Loading branch information
jjvraw authored Dec 3, 2023
1 parent 5b4b7a0 commit e35e2f5
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ def __init__(self, **kwargs):

self.control_queue: Queue[t.Any] = Queue()

# Storing the accepted parameters for do_execute, used in execute_request
self._do_exec_accepted_params = _accepts_parameters(
self.do_execute, ["cell_meta", "cell_id"]
)

def dispatch_control(self, msg):
self.control_queue.put_nowait(msg)

Expand Down Expand Up @@ -724,6 +729,8 @@ async def execute_request(self, stream, ident, parent):
store_history = content.get("store_history", not silent)
user_expressions = content.get("user_expressions", {})
allow_stdin = content.get("allow_stdin", False)
cell_meta = parent.get("metadata", {})
cell_id = cell_meta.get("cellId")
except Exception:
self.log.error("Got bad msg: ")
self.log.error("%s", parent)
Expand All @@ -739,12 +746,6 @@ async def execute_request(self, stream, ident, parent):
self.execution_count += 1
self._publish_execute_input(code, parent, self.execution_count)

cell_meta = parent.get("metadata", {})
cell_id = cell_meta.get("cellId")

# Check which parameters do_execute can accept
accepts_params = _accepts_parameters(self.do_execute, ["cell_meta", "cell_id"])

# Arguments based on the do_execute signature
do_execute_args = {
"code": code,
Expand All @@ -754,9 +755,9 @@ async def execute_request(self, stream, ident, parent):
"allow_stdin": allow_stdin,
}

if accepts_params["cell_meta"]:
if self._do_exec_accepted_params["cell_meta"]:
do_execute_args["cell_meta"] = cell_meta
if accepts_params["cell_id"]:
if self._do_exec_accepted_params["cell_id"]:
do_execute_args["cell_id"] = cell_id

# Call do_execute with the appropriate arguments
Expand Down

0 comments on commit e35e2f5

Please sign in to comment.