Skip to content

Commit bc732bf

Browse files
committed
try-fix
1 parent 2962392 commit bc732bf

File tree

2 files changed

+57
-47
lines changed

2 files changed

+57
-47
lines changed

tests/test_display_2.py

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -215,52 +215,63 @@ def test_set_matplotlib_formats_kwargs():
215215
cfg.print_figure_kwargs.clear()
216216

217217

218-
# @dec.skip_without("matplotlib")
219-
# def test_matplotlib_positioning():
220-
# _ip = get_ipython()
221-
# _ip.history_manager.reset()
222-
# prev_active_types = _ip.display_formatter.active_types
223-
# _ip.display_formatter.active_types = ["text/plain", "image/png"]
224-
225-
# _ip.run_cell("import matplotlib")
226-
# prev_mpl_backend = _ip.run_cell("matplotlib.get_backend()").result
227-
# try:
228-
# _ip.run_line_magic("matplotlib", "inline")
229-
# _ip.execution_count = 1
230-
# _ip.run_cell("'no plot here'", store_history=True)
231-
232-
# # Cell 2: No manual flush
233-
# _ip.run_cell(
234-
# "import matplotlib.pyplot as plt;plt.plot([0, 1])", store_history=True
235-
# )
236-
237-
# _ip.run_cell("'no plot here'", store_history=True)
238-
239-
# # Cell 4: Manual flush
240-
# _ip.run_cell("plt.plot([1, 0])\nplt.show()", store_history=True)
241-
242-
# _ip.run_cell("'no plot here'", store_history=True)
243-
244-
# outputs = _ip.history_manager.outputs
245-
246-
# # Only cells 2 and 4 should have plots
247-
# for cell_num in [1, 3, 5]:
248-
# assert not any(
249-
# "image/png" in out.bundle for out in outputs.get(cell_num, [])
250-
# ), f"Cell {cell_num} should not have plot"
251-
252-
# cell_2_has_plot = any("image/png" in out.bundle for out in outputs.get(2, []))
253-
# cell_4_has_plot = any("image/png" in out.bundle for out in outputs.get(4, []))
254-
255-
# assert cell_2_has_plot, "Cell 2 should have plot (auto-flush)"
256-
# assert cell_4_has_plot, "Cell 4 should have plot (manual flush)"
257-
258-
# finally:
259-
# _ip.run_cell("plt.close('all')")
260-
# _ip.run_line_magic("matplotlib", prev_mpl_backend)
261-
# _ip.history_manager.reset()
262-
# _ip.display_formatter.active_types = prev_active_types
263-
# _ip.displayhook.flush()
218+
@dec.skip_without("matplotlib")
219+
def test_matplotlib_positioning():
220+
_ip = get_ipython()
221+
222+
prev_active_types = _ip.display_formatter.active_types.copy()
223+
prev_execution_count = _ip.execution_count
224+
prev_user_ns_underscore = _ip.user_ns.get("_", None)
225+
226+
_ip.history_manager.reset()
227+
_ip.display_formatter.active_types = ["text/plain", "image/png"]
228+
229+
_ip.run_cell("import matplotlib")
230+
prev_mpl_backend = _ip.run_cell("matplotlib.get_backend()").result
231+
232+
try:
233+
_ip.run_line_magic("matplotlib", "inline")
234+
_ip.execution_count = 1
235+
_ip.run_cell("'no plot here'", store_history=True)
236+
237+
# Cell 2: No manual flush
238+
_ip.run_cell(
239+
"import matplotlib.pyplot as plt;plt.plot([0, 1])", store_history=True
240+
)
241+
242+
_ip.run_cell("'no plot here'", store_history=True)
243+
244+
# Cell 4: Manual flush
245+
_ip.run_cell("plt.plot([1, 0])\nplt.show()", store_history=True)
246+
247+
_ip.run_cell("'no plot here'", store_history=True)
248+
249+
outputs = _ip.history_manager.outputs
250+
251+
# Only cells 2 and 4 should have plots
252+
for cell_num in [1, 3, 5]:
253+
assert not any(
254+
"image/png" in out.bundle for out in outputs.get(cell_num, [])
255+
), f"Cell {cell_num} should not have plot"
256+
257+
cell_2_has_plot = any("image/png" in out.bundle for out in outputs.get(2, []))
258+
cell_4_has_plot = any("image/png" in out.bundle for out in outputs.get(4, []))
259+
260+
assert cell_2_has_plot, "Cell 2 should have plot (auto-flush)"
261+
assert cell_4_has_plot, "Cell 4 should have plot (manual flush)"
262+
263+
finally:
264+
_ip.run_cell("plt.close('all')")
265+
_ip.run_line_magic("matplotlib", prev_mpl_backend)
266+
_ip.history_manager.reset()
267+
_ip.display_formatter.active_types = prev_active_types
268+
_ip.displayhook.flush()
269+
270+
_ip.execution_count = prev_execution_count
271+
if prev_user_ns_underscore is not None:
272+
_ip.user_ns["_"] = prev_user_ns_underscore
273+
elif "_" in _ip.user_ns:
274+
del _ip.user_ns["_"]
264275

265276

266277
def test_display_available():

tests/test_displayhook.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def test_output_quiet():
3131

3232

3333
def test_underscore_no_overwrite_user():
34-
ip.displayhook.flush()
3534
ip.run_cell("_ = 42", store_history=True)
3635
ip.run_cell("1+1", store_history=True)
3736

0 commit comments

Comments
 (0)