Skip to content

Commit c8135f4

Browse files
committed
pass cell_meta from do_execute to run_cell
1 parent 41443c9 commit c8135f4

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

ipykernel/ipkernel.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ async def do_execute(
386386
if hasattr(shell, "run_cell_async") and hasattr(shell, "should_run_async"):
387387
run_cell = shell.run_cell_async
388388
should_run_async = shell.should_run_async
389-
accepts_params = _accepts_parameters(run_cell, ["cell_id"])
389+
accepts_params = _accepts_parameters(run_cell, ["cell_id", "cell_meta"])
390390
else:
391391
should_run_async = lambda cell: False # noqa: ARG005, E731
392392
# older IPython,
@@ -395,7 +395,7 @@ async def do_execute(
395395
async def run_cell(*args, **kwargs):
396396
return shell.run_cell(*args, **kwargs)
397397

398-
accepts_params = _accepts_parameters(shell.run_cell, ["cell_id"])
398+
accepts_params = _accepts_parameters(shell.run_cell, ["cell_id", "cell_meta"])
399399
try:
400400
# default case: runner is asyncio and asyncio is already running
401401
# TODO: this should check every case for "are we inside the runner",
@@ -406,6 +406,11 @@ async def run_cell(*args, **kwargs):
406406
except Exception:
407407
transformed_cell = code
408408
preprocessing_exc_tuple = sys.exc_info()
409+
do_execute_args = {}
410+
if accepts_params["cell_meta"]:
411+
do_execute_args["cell_meta"] = cell_meta
412+
if self._do_exec_accepted_params["cell_id"]:
413+
do_execute_args["cell_id"] = cell_id
409414

410415
if (
411416
_asyncio_runner # type:ignore[truthy-bool]
@@ -424,7 +429,7 @@ async def run_cell(*args, **kwargs):
424429
silent=silent,
425430
transformed_cell=transformed_cell,
426431
preprocessing_exc_tuple=preprocessing_exc_tuple,
427-
cell_id=cell_id,
432+
**do_execute_args,
428433
)
429434
else:
430435
coro = run_cell(
@@ -433,6 +438,7 @@ async def run_cell(*args, **kwargs):
433438
silent=silent,
434439
transformed_cell=transformed_cell,
435440
preprocessing_exc_tuple=preprocessing_exc_tuple,
441+
**do_execute_args,
436442
)
437443

438444
coro_future = asyncio.ensure_future(coro)
@@ -454,15 +460,7 @@ async def run_cell(*args, **kwargs):
454460
# runner isn't already running,
455461
# make synchronous call,
456462
# letting shell dispatch to loop runners
457-
if accepts_params["cell_id"]:
458-
res = shell.run_cell(
459-
code,
460-
store_history=store_history,
461-
silent=silent,
462-
cell_id=cell_id,
463-
)
464-
else:
465-
res = shell.run_cell(code, store_history=store_history, silent=silent)
463+
res = shell.run_cell(code, store_history=store_history, silent=silent, **do_execute_args)
466464
finally:
467465
self._restore_input()
468466

tests/inprocess/test_kernel.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,8 @@ async def test_do_execute(kc):
119119
kernel = InProcessKernel()
120120
await kernel.do_execute("a=1", True)
121121
assert kernel.shell.user_ns["a"] == 1
122+
123+
async def test_cell_meta_do_execute():
124+
kernel:InProcessKernel = InProcessKernel()
125+
await kernel.do_execute("a=1", True, cell_meta={"testkey": "testvalue"})
126+
assert kernel.shell.user_ns["a"] == 1

0 commit comments

Comments
 (0)