Skip to content

Commit f9d7d40

Browse files
committed
restore-ipython-states-in-test
1 parent 2edd506 commit f9d7d40

2 files changed

Lines changed: 28 additions & 65 deletions

File tree

IPython/core/magics/basic.py

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -567,75 +567,35 @@ def notebook(self, s):
567567

568568
if(len(hist)<=1):
569569
raise ValueError('History is empty, cannot export')
570-
571570
for session, execution_count, source in hist[:-1]:
572571
cell = v4.new_code_cell(execution_count=execution_count, source=source)
573-
574572
for output in outputs[execution_count]:
575-
if output.output_type == "out_stream":
576-
text_data = []
577-
for mime_type, data in output.bundle.items():
578-
if isinstance(data, list):
579-
text_data.extend(data)
580-
else:
581-
text_data.append(data)
582-
full_text = "".join(text_data)
583-
# Replace literal \n with actual newlines
584-
full_text = full_text.replace("\\n", "\n")
585-
normalized_text = []
586-
lines = full_text.split("\n")
587-
for i, line in enumerate(lines):
588-
if i < len(lines) - 1:
589-
normalized_text.append(line + "\n")
590-
elif line: # Last line only if it's not empty
591-
normalized_text.append(line + "\n")
592-
cell.outputs.append(v4.new_output("stream", text=normalized_text))
593-
594-
elif output.output_type == "err_stream":
595-
text_data = []
596-
for mime_type, data in output.bundle.items():
597-
if isinstance(data, list):
598-
text_data.extend(data)
599-
else:
600-
text_data.append(data)
601-
full_text = "".join(text_data)
602-
full_text = full_text.replace("\\n", "\n")
603-
normalized_text = []
604-
lines = full_text.split("\n")
605-
for i, line in enumerate(lines):
606-
if i < len(lines) - 1:
607-
normalized_text.append(line + "\n")
608-
elif line:
609-
normalized_text.append(line + "\n")
610-
err_output = v4.new_output("stream", text=normalized_text)
611-
err_output.name = "stderr"
612-
cell.outputs.append(err_output)
613-
614-
elif output.output_type == "execute_result":
615-
data_dict = {}
616-
for mime_type, data in output.bundle.items():
617-
data_dict[mime_type] = data
618-
cell.outputs.append(
619-
v4.new_output(
620-
"execute_result",
621-
data=data_dict,
622-
execution_count=execution_count,
573+
for mime_type, data in output.bundle.items():
574+
if output.output_type == "out_stream":
575+
text = data if isinstance(data, list) else [data]
576+
cell.outputs.append(v4.new_output("stream", text=text))
577+
elif output.output_type == "err_stream":
578+
text = data if isinstance(data, list) else [data]
579+
err_output = v4.new_output("stream", text=text)
580+
err_output.name = "stderr"
581+
cell.outputs.append(err_output)
582+
elif output.output_type == "execute_result":
583+
cell.outputs.append(
584+
v4.new_output(
585+
"execute_result",
586+
data={mime_type: data},
587+
execution_count=execution_count,
588+
)
623589
)
624-
)
625-
626-
elif output.output_type == "display_data":
627-
# Collect all MIME types for this display_data into a single output
628-
data_dict = {}
629-
for mime_type, data in output.bundle.items():
630-
data_dict[mime_type] = data
631-
cell.outputs.append(
632-
v4.new_output(
633-
"display_data",
634-
data=data_dict,
590+
elif output.output_type == "display_data":
591+
cell.outputs.append(
592+
v4.new_output(
593+
"display_data",
594+
data={mime_type: data},
595+
)
635596
)
636-
)
637-
else:
638-
raise ValueError(f"Unknown output type: {output.output_type}")
597+
else:
598+
raise ValueError(f"Unknown output type: {output.output_type}")
639599

640600
# Check if this execution_count is in exceptions (current session)
641601
if execution_count in exceptions:

tests/test_magic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,8 @@ def test_notebook_export_single_display():
10911091
pytest.importorskip("nbformat")
10921092

10931093
_ip = get_ipython()
1094+
orig_outputs = _ip.history_manager.outputs.copy()
1095+
orig_execution_count = _ip.execution_count
10941096
_ip.history_manager.reset()
10951097

10961098
try:
@@ -1127,7 +1129,8 @@ def test_notebook_export_single_display():
11271129
assert output_data["text/html"] == ["<div>test</div>"]
11281130

11291131
finally:
1130-
_ip.history_manager.reset()
1132+
_ip.history_manager.outputs = orig_outputs
1133+
_ip.execution_count = orig_execution_count
11311134

11321135

11331136
class TestEnv(TestCase):

0 commit comments

Comments
 (0)