Skip to content

Commit dd91229

Browse files
committed
Only close actual Matplotlib Figure objects
1 parent d70a834 commit dd91229

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
0.9 (unreleased)
22
----------------
33

4+
- Fix compatibility with Matplotlib 2.1. [#54]
5+
46
- Allow baseline_dir to be comma-separated URL list to allow mirrors to
57
be specified. [#59]
68

7-
- Make sure figures get closed even if not running with the --mpl option.
9+
- Make sure figures get closed even if not running with the --mpl
10+
option, and only close actual Matplotlib Figure objects. [#60]
811

912
0.8 (2017-07-19)
1013
----------------

pytest_mpl/plugin.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def pytest_runtest_setup(self, item):
155155

156156
import matplotlib
157157
import matplotlib.pyplot as plt
158+
from matplotlib.figure import Figure
158159
from matplotlib.testing.compare import compare_images
159160
from matplotlib.testing.decorators import ImageComparisonTest as MplImageComparisonTest
160161
try:
@@ -223,7 +224,13 @@ def item_function_wrapper(*args, **kwargs):
223224
test_image = os.path.abspath(os.path.join(result_dir, filename))
224225

225226
fig.savefig(test_image, **savefig_kwargs)
226-
plt.close(fig)
227+
228+
# We only need to close actual Matplotlib figure objects. If
229+
# we are dealing with a figure-like object that provides
230+
# savefig but is not a real Matplotlib object, we shouldn't
231+
# try closing it here.
232+
if isinstance(fig, Figure):
233+
plt.close(fig)
227234

228235
# Find path to baseline image
229236
if baseline_remote:
@@ -280,6 +287,7 @@ def pytest_runtest_setup(self, item):
280287
return
281288

282289
import matplotlib.pyplot as plt
290+
from matplotlib.figure import Figure
283291

284292
original = item.function
285293

@@ -291,7 +299,12 @@ def item_function_wrapper(*args, **kwargs):
291299
else: # function
292300
fig = original(*args, **kwargs)
293301

294-
plt.close(fig)
302+
# We only need to close actual Matplotlib figure objects. If
303+
# we are dealing with a figure-like object that provides
304+
# savefig but is not a real Matplotlib object, we shouldn't
305+
# try closing it here.
306+
if isinstance(fig, Figure):
307+
plt.close(fig)
295308

296309
if item.cls is not None:
297310
setattr(item.cls, item.function.__name__, item_function_wrapper)

0 commit comments

Comments
 (0)