From 9eb04a6539459358d7f89fb4a5a25fce21ee2bf5 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 6 Nov 2026 07:02:15 -0500 Subject: [PATCH 1/2] Add test: show() closes figures when matplotlib has no pyplot attribute (#24) --- tests/test_import.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_import.py b/tests/test_import.py index c816b29..56e3285 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -2,3 +2,17 @@ def test_import(): from matplotlib_inline.backend_inline import show show() + + +def test_show_closes_figures_without_pyplot_attribute(monkeypatch): + # gh-24: lazy loaders (e.g. librosa < 0.10.1) can leave matplotlib.pyplot + # in sys.modules without setting it as an attribute of matplotlib. + import matplotlib + import matplotlib.pyplot as plt + + from matplotlib_inline.backend_inline import show + + plt.figure() + monkeypatch.delattr(matplotlib, "pyplot") + show(close=True) + assert plt.get_fignums() == [] From 30f5f7e0314e093deb8914f109d52f2c3069780a Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 6 Nov 2026 07:02:16 -0500 Subject: [PATCH 2/2] Import pyplot explicitly before closing figures in show() (fixes #24) --- matplotlib_inline/backend_inline.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/matplotlib_inline/backend_inline.py b/matplotlib_inline/backend_inline.py index 0e148ac..cc1bfd5 100644 --- a/matplotlib_inline/backend_inline.py +++ b/matplotlib_inline/backend_inline.py @@ -96,7 +96,9 @@ def show(close=None, block=None): # only call close('all') if any to close # close triggers gc.collect, which can be slow if close and Gcf.get_all_fig_managers(): - matplotlib.pyplot.close("all") + from matplotlib import pyplot + + pyplot.close("all") # This flag will be reset by draw_if_interactive when called