@@ -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