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

Refactor execute_request to reduce redundancy and improve consistency #1177

Merged
merged 3 commits into from
Dec 3, 2023
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
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
Loading