From d92b1678fe2b27984995c18ea564d6bfae2d0d57 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 25 Feb 2017 19:35:29 +1100 Subject: [PATCH 1/2] Corrected ImageDraw floodfill exception type --- PIL/ImageDraw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/ImageDraw.py b/PIL/ImageDraw.py index 838983652..ddf669f78 100644 --- a/PIL/ImageDraw.py +++ b/PIL/ImageDraw.py @@ -334,7 +334,7 @@ def floodfill(image, xy, value, border=None): if background == value: return # seed point already has fill color pixel[x, y] = value - except IndexError: + except (ValueError, IndexError): return # seed point outside image edge = [(x, y)] if border is None: From 257bc8bd4f719e9848f778cd2d9c891d925c09da Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 1 Mar 2017 20:20:18 +1100 Subject: [PATCH 2/2] Added tests --- PIL/PyAccess.py | 6 +++--- Tests/test_file_bufrstub.py | 8 +++++++- Tests/test_file_container.py | 21 +++++++++++++++++++++ Tests/test_file_eps.py | 7 ++++++- Tests/test_file_gribstub.py | 8 +++++++- Tests/test_file_iptc.py | 10 ++++++++++ Tests/test_file_pcx.py | 4 ++++ Tests/test_file_wmf.py | 9 ++++++++- Tests/test_file_xvthumb.py | 17 +++++++++++++++++ Tests/test_image_access.py | 5 ++++- Tests/test_imagedraw.py | 18 ++++++++++++++---- Tests/test_imagemorph.py | 8 ++++++++ Tests/test_imageshow.py | 7 +++++++ 13 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 Tests/test_file_container.py create mode 100644 Tests/test_file_xvthumb.py diff --git a/PIL/PyAccess.py b/PIL/PyAccess.py index 58d20ca38..d5acfd0fe 100644 --- a/PIL/PyAccess.py +++ b/PIL/PyAccess.py @@ -165,7 +165,7 @@ class _PyAccess8(PyAccess): try: # integer self.pixels[y][x] = min(color, 255) - except: + except TypeError: # tuple self.pixels[y][x] = min(color[0], 255) @@ -182,7 +182,7 @@ class _PyAccessI16_N(PyAccess): try: # integer self.pixels[y][x] = min(color, 65535) - except: + except TypeError: # tuple self.pixels[y][x] = min(color[0], 65535) @@ -270,7 +270,7 @@ class _PyAccessF(PyAccess): try: # not a tuple self.pixels[y][x] = color - except: + except TypeError: # tuple self.pixels[y][x] = color[0] diff --git a/Tests/test_file_bufrstub.py b/Tests/test_file_bufrstub.py index 6872ca339..21d5c6ee5 100644 --- a/Tests/test_file_bufrstub.py +++ b/Tests/test_file_bufrstub.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper from PIL import BufrStubImagePlugin @@ -12,6 +12,12 @@ class TestFileBufrStub(PillowTestCase): lambda: BufrStubImagePlugin.BufrStubImageFile(invalid_file)) + def test_save(self): + im = hopper() + + tmpfile = self.tempfile("temp.bufr") + self.assertRaises(IOError, lambda: im.save(tmpfile)) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_container.py b/Tests/test_file_container.py new file mode 100644 index 000000000..56d53fc61 --- /dev/null +++ b/Tests/test_file_container.py @@ -0,0 +1,21 @@ +from helper import unittest, PillowTestCase, hopper + +from PIL import Image +from PIL import ContainerIO + + +class TestFileContainer(PillowTestCase): + + def test_sanity(self): + dir(Image) + dir(ContainerIO) + + def test_isatty(self): + im = hopper() + container = ContainerIO.ContainerIO(im, 0, 0) + + self.assertEqual(container.isatty(), 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index c16d2669c..eaf4df899 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper from PIL import Image, EpsImagePlugin import io @@ -94,6 +94,11 @@ class TestFileEps(PillowTestCase): image1_scale1_compare.load() self.assert_image_similar(img, image1_scale1_compare, 5) + def test_image_mode_not_supported(self): + im = hopper("RGBA") + tmpfile = self.tempfile('temp.eps') + self.assertRaises(ValueError, lambda: im.save(tmpfile)) + def test_render_scale1(self): # We need png support for these render test codecs = dir(Image.core) diff --git a/Tests/test_file_gribstub.py b/Tests/test_file_gribstub.py index 786ce42dd..79025d3b4 100644 --- a/Tests/test_file_gribstub.py +++ b/Tests/test_file_gribstub.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper from PIL import GribStubImagePlugin @@ -12,6 +12,12 @@ class TestFileGribStub(PillowTestCase): lambda: GribStubImagePlugin.GribStubImageFile(invalid_file)) + def test_save(self): + im = hopper() + + tmpfile = self.tempfile("temp.grib") + self.assertRaises(IOError, lambda: im.save(tmpfile)) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_iptc.py b/Tests/test_file_iptc.py index a29e1a4a6..e08d994a2 100644 --- a/Tests/test_file_iptc.py +++ b/Tests/test_file_iptc.py @@ -29,6 +29,16 @@ class TestFileIptc(PillowTestCase): self.assertEqual(iptc[(2, 90)], b"Budapest") self.assertEqual(iptc[(2, 101)], b"Hungary") + def test_getiptcinfo_tiff_none(self): + # Arrange + im = Image.open("Tests/images/hopper.tif") + + # Act + iptc = IptcImagePlugin.getiptcinfo(im) + + # Assert + self.assertIsNone(iptc) + def test_i(self): # Arrange c = b"a" diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index 7621c1cc6..9766318e6 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -19,6 +19,10 @@ class TestFilePcx(PillowTestCase): for mode in ('1', 'L', 'P', 'RGB'): self._roundtrip(hopper(mode)) + # Test an unsupported mode + f = self.tempfile("temp.pcx") + self.assertRaises(ValueError, lambda: hopper("RGBA").save(f)) + def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 9f2f893cb..73ef2a2fc 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper from PIL import Image from io import BytesIO @@ -26,6 +26,13 @@ class TestFileWmf(PillowTestCase): imref.load() self.assert_image_similar(im, imref, 2.0) + def test_save(self): + im = hopper() + + for ext in [".wmf", ".emf"]: + tmpfile = self.tempfile("temp"+ext) + self.assertRaises(IOError, lambda: im.save(tmpfile)) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_xvthumb.py b/Tests/test_file_xvthumb.py new file mode 100644 index 000000000..a4e1e6554 --- /dev/null +++ b/Tests/test_file_xvthumb.py @@ -0,0 +1,17 @@ +from helper import unittest, PillowTestCase + +from PIL import XVThumbImagePlugin + + +class TestFileXVThumb(PillowTestCase): + + def test_invalid_file(self): + invalid_file = "Tests/images/flower.jpg" + + self.assertRaises(SyntaxError, + lambda: + XVThumbImagePlugin.XVThumbImageFile(invalid_file)) + + +if __name__ == '__main__': + unittest.main() diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 900f39eb4..005bd5d35 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -84,7 +84,7 @@ class TestImageGetPixel(AccessTest): im.putpixel((0, 0), c) with self.assertRaises(IndexError): im.getpixel((0, 0)) - + # check initial color im = Image.new(mode, (1, 1), c) self.assertEqual( @@ -234,6 +234,9 @@ class TestCffi(AccessTest): # im = Image.new('I;32B', (10, 10), 2**10) # self._test_set_access(im, 2**13-1) + def test_not_implemented(self): + self.assertIsNone(PyAccess.new(hopper("BGR;15"))) + # ref https://github.com/python-pillow/Pillow/pull/2009 def test_reference_counting(self): size = 10 diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 187a1fdcb..b0a4f2f1e 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -292,14 +292,24 @@ class TestImageDraw(PillowTestCase): draw = ImageDraw.Draw(im) draw.rectangle(BBOX2, outline="yellow", fill="green") centre_point = (int(W/2), int(H/2)) + red = ImageColor.getrgb("red") + im_floodfill = Image.open("Tests/images/imagedraw_floodfill.png") # Act - ImageDraw.floodfill(im, centre_point, ImageColor.getrgb("red")) - del draw + ImageDraw.floodfill(im, centre_point, red) # Assert - self.assert_image_equal( - im, Image.open("Tests/images/imagedraw_floodfill.png")) + self.assert_image_equal(im, im_floodfill) + + # Test that using the same colour does not change the image + ImageDraw.floodfill(im, centre_point, red) + self.assert_image_equal(im, im_floodfill) + + # Test that filling outside the image does not change the image + ImageDraw.floodfill(im, (W, H), red) + self.assert_image_equal(im, im_floodfill) + del draw + @unittest.skipIf(hasattr(sys, 'pypy_version_info'), "Causes fatal RPython error on PyPy") diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index ea54417a2..f2247bdca 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -73,10 +73,18 @@ class MorphTests(PillowTestCase): 'corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'): lb = ImageMorph.LutBuilder(op_name=op) + self.assertIsNone(lb.get_lut()) + lut = lb.build_lut() with open('Tests/images/%s.lut' % op, 'rb') as f: self.assertEqual(lut, bytearray(f.read())) + def test_no_operator_loaded(self): + mop = ImageMorph.MorphOp() + self.assertRaises(Exception, lambda: mop.apply(None)) + self.assertRaises(Exception, lambda: mop.match(None)) + self.assertRaises(Exception, lambda: mop.save_lut(None)) + # Test the named patterns def test_erosion8(self): # erosion8 diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index 4424fa0eb..342c63851 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -10,6 +10,13 @@ class TestImageShow(PillowTestCase): dir(Image) dir(ImageShow) + def test_viewer(self): + viewer = ImageShow.Viewer() + + self.assertIsNone(viewer.get_format(None)) + + self.assertRaises(NotImplementedError, lambda: viewer.get_command(None)) + if __name__ == '__main__': unittest.main()