Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions ipykernel/zmqshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ def publish( # type:ignore[override]
and HistoryOutput is not None
):
# Reference: github.com/ipython/ipython/pull/14998
exec_count = self.shell.execution_count
if getattr(self.shell.display_pub, "_in_post_execute", False):
exec_count -= 1
# run_cell increments execution_count before running the cell, so while a cell runs
# (and in its post_execute hooks, e.g. matplotlib's flush) the cell's own count is one less.
exec_count = self.shell.execution_count - 1
outputs = getattr(self.shell.history_manager, "outputs", None)
if outputs is not None:
outputs.setdefault(exec_count, []).append(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,3 +957,16 @@ def test_context_vars():
)
stdout, _ = assemble_output(kc.get_iopub_msg, parent_msg_id=msg_id)
assert stdout.strip() == "set"


def test_display_history_uses_the_cell_execution_count():
"""A display published by a cell is stored under that cell's execution count (for %notebook)."""
with new_kernel(["--ZMQDisplayPublisher.store_display_history=True"]) as kc:
msg_id, content = execute(kc=kc, code="display('shown by this cell')")
wait_for_idle(kc, msg_id)
stored = f"[o.bundle['text/plain'] for o in get_ipython().history_manager.outputs.get({content['execution_count']}, [])]"
_msg_id, reply = execute(kc=kc, code="", user_expressions={"stored": stored})
assert (
reply["user_expressions"]["stored"]["data"]["text/plain"]
== "[\"'shown by this cell'\"]"
)
44 changes: 23 additions & 21 deletions tests/test_zmq_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,27 +221,29 @@ def test_display_stored_in_history(self):
for %notebook magic support, and not stored when disabled.
"""
for enable in [False, True]:
# Mock shell with history manager
mock_shell = MagicMock()
mock_shell.execution_count = 1
mock_shell.history_manager.outputs = dict()
mock_shell.display_pub._in_post_execute = False

self.disp_pub.shell = mock_shell
self.disp_pub.store_display_history = enable

data = {"text/plain": "test output"}
self.disp_pub.publish(data)

if enable:
# Check that output was stored in history
stored_outputs = mock_shell.history_manager.outputs[1]
assert len(stored_outputs) == 1
assert stored_outputs[0].output_type == "display_data"
assert stored_outputs[0].bundle == data
else:
# Should not store anything in history
assert mock_shell.history_manager.outputs == {}
for in_post_execute in [False, True]:
# Mock shell with history manager, while cell 1 runs: run_cell has already
# incremented execution_count to 2.
mock_shell = MagicMock()
mock_shell.execution_count = 2
mock_shell.history_manager.outputs = dict()
mock_shell.display_pub._in_post_execute = in_post_execute

self.disp_pub.shell = mock_shell
self.disp_pub.store_display_history = enable

data = {"text/plain": "test output"}
self.disp_pub.publish(data)

if enable:
# Check that output was stored in history, under cell 1
stored_outputs = mock_shell.history_manager.outputs[1]
assert len(stored_outputs) == 1
assert stored_outputs[0].output_type == "display_data"
assert stored_outputs[0].bundle == data
else:
# Should not store anything in history
assert mock_shell.history_manager.outputs == {}


def test_magics(tmp_path):
Expand Down
Loading