Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 013fd3f

Browse files
authored
Merge pull request #8006 from tacaswell/fix_imshow_segfault
FIX: do not try to render empty images
2 parents d63c95d + babdf73 commit 013fd3f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/matplotlib/image.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
301301
if A is None:
302302
raise RuntimeError('You must first set the image'
303303
' array or the image attribute')
304+
if any(s == 0 for s in A.shape):
305+
raise RuntimeError("_make_image must get a non-empty image. "
306+
"Your Artist's draw method must filter before "
307+
"this method is called.")
304308

305309
clipped_bbox = Bbox.intersection(out_bbox, clip_bbox)
306310

@@ -478,9 +482,17 @@ def _check_unsampled_image(self, renderer):
478482

479483
@allow_rasterization
480484
def draw(self, renderer, *args, **kwargs):
485+
# if not visible, declare victory and return
481486
if not self.get_visible():
487+
self.stale = False
482488
return
483489

490+
# for empty images, there is nothing to draw!
491+
if self.get_array().size == 0:
492+
self.stale = False
493+
return
494+
495+
# actually render the image.
484496
gc = renderer.new_gc()
485497
self._set_gc_clip(gc)
486498
gc.set_alpha(self.get_alpha())

lib/matplotlib/tests/test_image.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import numpy as np
3333

3434
import nose
35+
import nose.tools
3536

3637
try:
3738
from PIL import Image
@@ -765,5 +766,15 @@ def test_imshow_no_warn_invalid():
765766
assert len(warns) == 0
766767

767768

769+
@cleanup
770+
def test_empty_imshow():
771+
fig, ax = plt.subplots()
772+
im = ax.imshow([[]])
773+
im.set_extent([-5, 5, -5, 5])
774+
fig.canvas.draw()
775+
776+
nose.tools.assert_raises(RuntimeError, im.make_image, fig._cachedRenderer)
777+
778+
768779
if __name__ == '__main__':
769780
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)