From 751a6d1c2d13144e240ac2b0b5cd1c46ce9ca3fd Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 19 Aug 2017 18:53:09 +1000 Subject: [PATCH 1/7] Fixed WMF save handler check --- PIL/WmfImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/WmfImagePlugin.py b/PIL/WmfImagePlugin.py index 584523fc7..c6c002ffd 100644 --- a/PIL/WmfImagePlugin.py +++ b/PIL/WmfImagePlugin.py @@ -153,7 +153,7 @@ class WmfStubImageFile(ImageFile.StubImageFile): def _save(im, fp, filename): - if _handler is None or not hasattr("_handler", "save"): + if _handler is None or not hasattr(_handler, "save"): raise IOError("WMF save handler not installed") _handler.save(im, fp, filename) From c10ad19a2b1a25d00c845c71f0b27c6738be4bdb Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 26 Aug 2017 10:20:30 +1000 Subject: [PATCH 2/7] Changed file opening in tests to use with --- Tests/test_file_png.py | 22 +++++++++++----------- Tests/test_file_tiff.py | 12 ++++++------ Tests/test_font_bdf.py | 4 ++-- Tests/test_font_pcf.py | 4 ++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 7ee2c9fb7..d4970d2df 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -505,17 +505,17 @@ class TestFilePng(PillowTestCase): im.convert("P").save(test_file, dpi=(100, 100)) chunks = [] - fp = open(test_file, "rb") - fp.read(8) - png = PngImagePlugin.PngStream(fp) - while True: - cid, pos, length = png.read() - chunks.append(cid) - try: - s = png.call(cid, pos, length) - except EOFError: - break - png.crc(cid, s) + with open(test_file, "rb") as fp: + fp.read(8) + png = PngImagePlugin.PngStream(fp) + while True: + cid, pos, length = png.read() + chunks.append(cid) + try: + s = png.call(cid, pos, length) + except EOFError: + break + png.crc(cid, s) # https://www.w3.org/TR/PNG/#5ChunkOrdering # IHDR - shall be first diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index bfc3bf120..8dcd518b7 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -495,12 +495,12 @@ class TestFileTiff(PillowTestCase): with Image.open("Tests/images/uint16_1_4660.tif") as im: im.save(tmpfile) - f = open(tmpfile, 'rb') - im = Image.open(f) - fp = im.fp - self.assertFalse(fp.closed) - im.load() - self.assertFalse(fp.closed) + with open(tmpfile, 'rb') as f: + im = Image.open(f) + fp = im.fp + self.assertFalse(fp.closed) + im.load() + self.assertFalse(fp.closed) @unittest.skipUnless(sys.platform.startswith('win32'), "Windows only") class TestFileTiffW32(PillowTestCase): diff --git a/Tests/test_font_bdf.py b/Tests/test_font_bdf.py index e3b7fbbd3..20007215b 100644 --- a/Tests/test_font_bdf.py +++ b/Tests/test_font_bdf.py @@ -9,8 +9,8 @@ class TestFontBdf(PillowTestCase): def test_sanity(self): - test_file = open(filename, "rb") - font = BdfFontFile.BdfFontFile(test_file) + with open(filename, "rb") as test_file: + font = BdfFontFile.BdfFontFile(test_file) self.assertIsInstance(font, FontFile.FontFile) self.assertEqual(len([_f for _f in font.glyph if _f]), 190) diff --git a/Tests/test_font_pcf.py b/Tests/test_font_pcf.py index 25c5d7001..4c0333050 100644 --- a/Tests/test_font_pcf.py +++ b/Tests/test_font_pcf.py @@ -17,8 +17,8 @@ class TestFontPcf(PillowTestCase): self.skipTest("zlib support not available") def save_font(self): - test_file = open(fontname, "rb") - font = PcfFontFile.PcfFontFile(test_file) + with open(fontname, "rb") as test_file: + font = PcfFontFile.PcfFontFile(test_file) self.assertIsInstance(font, FontFile.FontFile) self.assertEqual(len([_f for _f in font.glyph if _f]), 192) From 371933a5973305de5dcff39df6d1b6df7c5065c2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 1 Sep 2017 20:36:51 +1000 Subject: [PATCH 3/7] Added tests --- Tests/test_file_gif.py | 26 +++++++++++---------- Tests/test_file_icns.py | 20 +++++++++++++++- Tests/test_file_im.py | 19 +++++++++++---- Tests/test_file_png.py | 6 +++++ Tests/test_file_tiff.py | 4 ++++ Tests/test_file_wmf.py | 19 +++++++++++++++ Tests/test_image.py | 22 ++++++++++++++++++ Tests/test_image_frombytes.py | 3 +++ Tests/test_image_resize.py | 4 ++++ Tests/test_imagecms.py | 14 +++++++++++ Tests/test_imagepalette.py | 44 +++++++++++++++++------------------ Tests/test_imageshow.py | 20 +++++++++++++++- 12 files changed, 159 insertions(+), 42 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index bb2aa8a0d..9f40485a5 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -375,29 +375,31 @@ class TestFileGif(PillowTestCase): def test_version(self): out = self.tempfile('temp.gif') + def assertVersionAfterSave(im, version): + im.save(out) + reread = Image.open(out) + self.assertEqual(reread.info["version"], version) + # Test that GIF87a is used by default im = Image.new('L', (100, 100), '#000') - im.save(out) - reread = Image.open(out) - self.assertEqual(reread.info["version"], b"GIF87a") + assertVersionAfterSave(im, b"GIF87a") + + # Test setting the version to 89a + im = Image.new('L', (100, 100), '#000') + im.info["version"] = b"89a" + assertVersionAfterSave(im, b"GIF89a") # Test that adding a GIF89a feature changes the version im.info["transparency"] = 1 - im.save(out) - reread = Image.open(out) - self.assertEqual(reread.info["version"], b"GIF89a") + assertVersionAfterSave(im, b"GIF89a") # Test that a GIF87a image is also saved in that format im = Image.open("Tests/images/test.colors.gif") - im.save(out) - reread = Image.open(out) - self.assertEqual(reread.info["version"], b"GIF87a") + assertVersionAfterSave(im, b"GIF87a") # Test that a GIF89a image is also saved in that format im.info["version"] = b"GIF89a" - im.save(out) - reread = Image.open(out) - self.assertEqual(reread.info["version"], b"GIF87a") + assertVersionAfterSave(im, b"GIF87a") def test_append_images(self): out = self.tempfile('temp.gif') diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index 92fe136f2..913d71586 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -1,7 +1,8 @@ from helper import unittest, PillowTestCase -from PIL import Image +from PIL import Image, IcnsImagePlugin +import io import sys # sample icon file @@ -82,6 +83,23 @@ class TestFileIcns(PillowTestCase): self.assertEqual(im2.mode, 'RGBA') self.assertEqual(im2.size, (wr, hr)) + def test_getimage(self): + with open(TEST_FILE, 'rb') as fp: + icns_file = IcnsImagePlugin.IcnsFile(fp) + + im = icns_file.getimage() + self.assertEqual(im.mode, "RGBA") + self.assertEqual(im.size, (1024, 1024)) + + im = icns_file.getimage((512, 512)) + self.assertEqual(im.mode, "RGBA") + self.assertEqual(im.size, (512, 512)) + + def test_not_an_icns_file(self): + with io.BytesIO(b'invalid\n') as fp: + self.assertRaises(SyntaxError, + lambda: IcnsImagePlugin.IcnsFile(fp)) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_im.py b/Tests/test_file_im.py index 19720dffb..5347d1536 100644 --- a/Tests/test_file_im.py +++ b/Tests/test_file_im.py @@ -43,12 +43,18 @@ class TestFileIm(PillowTestCase): self.assertLess(im.tell(), n_frames) def test_roundtrip(self): - out = self.tempfile('temp.im') - im = hopper() - im.save(out) - reread = Image.open(out) + for mode in ["RGB", "P"]: + out = self.tempfile('temp.im') + im = hopper(mode) + im.save(out) + reread = Image.open(out) - self.assert_image_equal(reread, im) + self.assert_image_equal(reread, im) + + def test_save_unsupported_mode(self): + out = self.tempfile('temp.im') + im = hopper("HSV") + self.assertRaises(ValueError, lambda: im.save(out)) def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" @@ -56,6 +62,9 @@ class TestFileIm(PillowTestCase): self.assertRaises(SyntaxError, lambda: ImImagePlugin.ImImageFile(invalid_file)) + def test_number(self): + self.assertEqual(1.2, ImImagePlugin.number("1.2")) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index d4970d2df..53d67a7f6 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -531,6 +531,12 @@ class TestFilePng(PillowTestCase): # pHYs - before IDAT self.assertLess(chunks.index(b"pHYs"), chunks.index(b"IDAT")) + def test_getchunks(self): + im = hopper() + + chunks = PngImagePlugin.getchunks(im) + self.assertEqual(len(chunks), 3) + @unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") class TestTruncatedPngPLeaks(PillowTestCase): diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 8dcd518b7..fff1028a6 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -85,6 +85,10 @@ class TestFileTiff(PillowTestCase): im = Image.open("Tests/images/copyleft.tiff") self.assertEqual(im.mode, 'RGB') + def test_set_legacy_api(self): + with self.assertRaises(Exception): + ImageFileDirectory_v2.legacy_api = None + def test_xyres_tiff(self): filename = "Tests/images/pil168.tif" im = Image.open(filename) diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index a4bc80913..6a6ad8f23 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -1,5 +1,7 @@ from helper import unittest, PillowTestCase, hopper + from PIL import Image +from PIL import WmfImagePlugin class TestFileWmf(PillowTestCase): @@ -26,6 +28,23 @@ class TestFileWmf(PillowTestCase): imref.load() self.assert_image_similar(im, imref, 2.0) + def test_register_handler(self): + class TestHandler: + methodCalled = False + + def save(self, im, fp, filename): + self.methodCalled = True + handler = TestHandler() + WmfImagePlugin.register_handler(handler) + + im = hopper() + tmpfile = self.tempfile("temp.wmf") + im.save(tmpfile) + self.assertTrue(handler.methodCalled) + + # Restore the state before this test + WmfImagePlugin.register_handler(None) + def test_save(self): im = hopper() diff --git a/Tests/test_image.py b/Tests/test_image.py index c6eaa7906..8f8fe70ca 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -70,6 +70,9 @@ class TestImage(PillowTestCase): im = io.BytesIO(b'') self.assertRaises(IOError, lambda: Image.open(im)) + def test_bad_mode(self): + self.assertRaises(ValueError, lambda: Image.open("filename", "bad mode")) + @unittest.skipIf(sys.version_info < (3, 4), "pathlib only available in Python 3.4 or later") def test_pathlib(self): @@ -273,6 +276,20 @@ class TestImage(PillowTestCase): target.crop((32, 32, 96, 96))) self.assertEqual(source.size, (128, 128)) + # errors + self.assertRaises(ValueError, + lambda: source.alpha_composite(over, "invalid source")) + self.assertRaises(ValueError, + lambda: source.alpha_composite(over, (0, 0), "invalid destination")) + self.assertRaises(ValueError, + lambda: source.alpha_composite(over, (0))) + self.assertRaises(ValueError, + lambda: source.alpha_composite(over, (0, 0), (0))) + self.assertRaises(ValueError, + lambda: source.alpha_composite(over, (0, -1))) + self.assertRaises(ValueError, + lambda: source.alpha_composite(over, (0, 0), (0, -1))) + def test_registered_extensions_uninitialized(self): # Arrange Image._initialized = 0 @@ -445,6 +462,11 @@ class TestImage(PillowTestCase): target = Image.open(target_file).convert(mode) self.assert_image_equal(im, target) + def test_remap_palette(self): + # Test illegal image mode + im = hopper() + self.assertRaises(ValueError, lambda: im.remap_palette(None)) + class MockEncoder(object): pass diff --git a/Tests/test_image_frombytes.py b/Tests/test_image_frombytes.py index 61ed079d6..2d48bb6b8 100644 --- a/Tests/test_image_frombytes.py +++ b/Tests/test_image_frombytes.py @@ -11,6 +11,9 @@ class TestImageFromBytes(PillowTestCase): self.assert_image_equal(im1, im2) + def test_not_implemented(self): + self.assertRaises(NotImplementedError, Image.fromstring) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index c62f494e0..e08c8f502 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -112,6 +112,10 @@ class TestImageResize(PillowTestCase): resize(mode, (112, 103)) resize(mode, (188, 214)) + # Test unknown resampling filter + im = hopper() + self.assertRaises(ValueError, lambda: im.resize((10, 10), "unknown")) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index aa592c398..105ca9f16 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -144,6 +144,12 @@ class TestImageCms(PillowTestCase): 'IEC 61966-2.1 Default RGB colour space - sRGB') def test_exceptions(self): + # Test mode mismatch + psRGB = ImageCms.createProfile("sRGB") + pLab = ImageCms.createProfile("LAB") + t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") + self.assertRaises(ValueError, lambda: t.apply_in_place(hopper("RGBA"))) + # the procedural pyCMS API uses PyCMSError for all sorts of errors self.assertRaises( ImageCms.PyCMSError, @@ -167,6 +173,14 @@ class TestImageCms(PillowTestCase): ImageCms.createProfile("LAB", 5000) ImageCms.createProfile("LAB", 6500) + def test_unsupported_color_space(self): + self.assertRaises(ImageCms.PyCMSError, + lambda: ImageCms.createProfile("unsupported")) + + def test_invalid_color_temperature(self): + self.assertRaises(ImageCms.PyCMSError, + lambda: ImageCms.createProfile("LAB", "invalid")) + def test_simple_lab(self): i = Image.new('RGB', (10, 10), (128, 128, 128)) diff --git a/Tests/test_imagepalette.py b/Tests/test_imagepalette.py index 248138377..6afd09113 100644 --- a/Tests/test_imagepalette.py +++ b/Tests/test_imagepalette.py @@ -2,20 +2,18 @@ from helper import unittest, PillowTestCase from PIL import ImagePalette, Image -ImagePalette = ImagePalette.ImagePalette - class TestImagePalette(PillowTestCase): def test_sanity(self): - ImagePalette("RGB", list(range(256))*3) - self.assertRaises( - ValueError, lambda: ImagePalette("RGB", list(range(256))*2)) + ImagePalette.ImagePalette("RGB", list(range(256))*3) + self.assertRaises(ValueError, + lambda: ImagePalette.ImagePalette("RGB", list(range(256))*2)) def test_getcolor(self): - palette = ImagePalette() + palette = ImagePalette.ImagePalette() test_map = {} for i in range(256): @@ -24,34 +22,34 @@ class TestImagePalette(PillowTestCase): self.assertEqual(len(test_map), 256) self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3))) + # Test unknown color specifier + self.assertRaises(ValueError, lambda: palette.getcolor("unknown")) + def test_file(self): - palette = ImagePalette("RGB", list(range(256))*3) + palette = ImagePalette.ImagePalette("RGB", list(range(256))*3) f = self.tempfile("temp.lut") palette.save(f) - from PIL.ImagePalette import load, raw - - p = load(f) + p = ImagePalette.load(f) # load returns raw palette information self.assertEqual(len(p[0]), 768) self.assertEqual(p[1], "RGB") - p = raw(p[1], p[0]) - self.assertIsInstance(p, ImagePalette) + p = ImagePalette.raw(p[1], p[0]) + self.assertIsInstance(p, ImagePalette.ImagePalette) self.assertEqual(p.palette, palette.tobytes()) def test_make_linear_lut(self): # Arrange - from PIL.ImagePalette import make_linear_lut black = 0 white = 255 # Act - lut = make_linear_lut(black, white) + lut = ImagePalette.make_linear_lut(black, white) # Assert self.assertIsInstance(lut, list) @@ -63,22 +61,20 @@ class TestImagePalette(PillowTestCase): def test_make_linear_lut_not_yet_implemented(self): # Update after FIXME # Arrange - from PIL.ImagePalette import make_linear_lut black = 1 white = 255 # Act self.assertRaises( NotImplementedError, - lambda: make_linear_lut(black, white)) + lambda: ImagePalette.make_linear_lut(black, white)) def test_make_gamma_lut(self): # Arrange - from PIL.ImagePalette import make_gamma_lut exp = 5 # Act - lut = make_gamma_lut(exp) + lut = ImagePalette.make_gamma_lut(exp) # Assert self.assertIsInstance(lut, list) @@ -92,8 +88,7 @@ class TestImagePalette(PillowTestCase): def test_rawmode_valueerrors(self): # Arrange - from PIL.ImagePalette import raw - palette = raw("RGB", list(range(256))*3) + palette = ImagePalette.raw("RGB", list(range(256))*3) # Act / Assert self.assertRaises(ValueError, palette.tobytes) @@ -104,7 +99,7 @@ class TestImagePalette(PillowTestCase): def test_getdata(self): # Arrange data_in = list(range(256))*3 - palette = ImagePalette("RGB", data_in) + palette = ImagePalette.ImagePalette("RGB", data_in) # Act mode, data_out = palette.getdata() @@ -114,9 +109,8 @@ class TestImagePalette(PillowTestCase): def test_rawmode_getdata(self): # Arrange - from PIL.ImagePalette import raw data_in = list(range(256))*3 - palette = raw("RGB", data_in) + palette = ImagePalette.raw("RGB", data_in) # Act rawmode, data_out = palette.getdata() @@ -138,6 +132,10 @@ class TestImagePalette(PillowTestCase): self.assert_image_equal(img, reloaded) + def test_invalid_palette(self): + self.assertRaises(IOError, + lambda: ImagePalette.load("Tests/images/hopper.jpg")) + if __name__ == '__main__': unittest.main() diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index 342c63851..fe07473bc 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper from PIL import Image from PIL import ImageShow @@ -10,6 +10,24 @@ class TestImageShow(PillowTestCase): dir(Image) dir(ImageShow) + def test_register(self): + # Test registering a viewer that is not a class + ImageShow.register("not a class") + + def test_show(self): + class TestViewer: + methodCalled = False + + def show(self, image, title=None, **options): + self.methodCalled = True + return True + viewer = TestViewer() + ImageShow.register(viewer, -1) + + im = hopper() + self.assertTrue(ImageShow.show(im)) + self.assertTrue(viewer.methodCalled) + def test_viewer(self): viewer = ImageShow.Viewer() From 9e843a2d9bb3636c5a58bbc603355eab535580d1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 1 Sep 2017 21:05:40 +1000 Subject: [PATCH 4/7] Changed format of lambda calls --- Tests/test_decompression_bomb.py | 4 +-- Tests/test_features.py | 8 +++--- Tests/test_file_bmp.py | 2 +- Tests/test_file_bufrstub.py | 5 ++-- Tests/test_file_cur.py | 2 +- Tests/test_file_dcx.py | 4 +-- Tests/test_file_eps.py | 4 +-- Tests/test_file_fitsstub.py | 7 +++--- Tests/test_file_fli.py | 4 +-- Tests/test_file_fpx.py | 4 +-- Tests/test_file_gbr.py | 2 +- Tests/test_file_gif.py | 2 +- Tests/test_file_gimppalette.py | 6 ++--- Tests/test_file_gribstub.py | 5 ++-- Tests/test_file_hdf5stub.py | 7 +++--- Tests/test_file_icns.py | 2 +- Tests/test_file_ico.py | 2 +- Tests/test_file_im.py | 4 +-- Tests/test_file_jpeg.py | 16 ++++++------ Tests/test_file_jpeg2k.py | 3 +-- Tests/test_file_libtiff.py | 13 +++++----- Tests/test_file_mcidas.py | 3 +-- Tests/test_file_mic.py | 6 ++--- Tests/test_file_msp.py | 6 ++--- Tests/test_file_palm.py | 2 +- Tests/test_file_pcx.py | 5 ++-- Tests/test_file_pdf.py | 2 +- Tests/test_file_pixar.py | 2 +- Tests/test_file_png.py | 6 ++--- Tests/test_file_ppm.py | 2 +- Tests/test_file_psd.py | 4 +-- Tests/test_file_sgi.py | 7 +++--- Tests/test_file_spider.py | 4 +-- Tests/test_file_sun.py | 2 +- Tests/test_file_tiff.py | 10 ++++---- Tests/test_file_tiff_metadata.py | 2 +- Tests/test_file_webp.py | 3 ++- Tests/test_file_wmf.py | 2 +- Tests/test_file_xpm.py | 2 +- Tests/test_file_xvthumb.py | 6 ++--- Tests/test_font_bdf.py | 2 +- Tests/test_font_pcf.py | 2 +- Tests/test_image.py | 43 ++++++++++++++++---------------- Tests/test_image_convert.py | 8 +++--- Tests/test_image_filter.py | 4 +-- Tests/test_image_load.py | 4 +-- Tests/test_image_point.py | 11 ++++---- Tests/test_image_putpalette.py | 12 ++++----- Tests/test_image_quantize.py | 2 +- Tests/test_image_resize.py | 2 +- Tests/test_image_transform.py | 4 +-- Tests/test_imagecms.py | 14 +++++------ Tests/test_imagedraw.py | 3 +-- Tests/test_imagefile.py | 6 ++--- Tests/test_imagefont.py | 10 ++++---- Tests/test_imagemorph.py | 20 +++++++-------- Tests/test_imageops_usm.py | 16 ++++++------ Tests/test_imagepalette.py | 17 ++++++------- Tests/test_imagesequence.py | 4 +-- Tests/test_imageshow.py | 2 +- Tests/test_imagestat.py | 2 +- Tests/test_lib_image.py | 4 +-- Tests/test_numpy.py | 10 ++++---- 63 files changed, 186 insertions(+), 198 deletions(-) diff --git a/Tests/test_decompression_bomb.py b/Tests/test_decompression_bomb.py index 5598fd9c8..2dc84950a 100644 --- a/Tests/test_decompression_bomb.py +++ b/Tests/test_decompression_bomb.py @@ -36,7 +36,7 @@ class TestDecompressionBomb(PillowTestCase): # Act / Assert self.assert_warning(Image.DecompressionBombWarning, - lambda: Image.open(TEST_FILE)) + Image.open, TEST_FILE) class TestDecompressionCrop(PillowTestCase): @@ -52,7 +52,7 @@ class TestDecompressionCrop(PillowTestCase): # same decompression bomb warnings on them. box = (0, 0, self.src.width * 2, self.src.height * 2) self.assert_warning(Image.DecompressionBombWarning, - lambda: self.src.crop(box)) + self.src.crop, box) if __name__ == '__main__': unittest.main() diff --git a/Tests/test_features.py b/Tests/test_features.py index d14aaca69..cdabcad5e 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -7,7 +7,7 @@ try: HAVE_WEBP = True except: HAVE_WEBP = False - + class TestFeatures(PillowTestCase): @@ -34,7 +34,7 @@ class TestFeatures(PillowTestCase): def check_webp_mux(self): self.assertEqual(features.check('webp_mux'), _webp.HAVE_WEBPMUX) - + def test_check_modules(self): for feature in features.modules: self.assertIn(features.check_module(feature), [True, False]) @@ -51,13 +51,13 @@ class TestFeatures(PillowTestCase): # Arrange codec = "unsupported_codec" # Act / Assert - self.assertRaises(ValueError, lambda: features.check_codec(codec)) + self.assertRaises(ValueError, features.check_codec, codec) def test_unsupported_module(self): # Arrange module = "unsupported_module" # Act / Assert - self.assertRaises(ValueError, lambda: features.check_module(module)) + self.assertRaises(ValueError, features.check_module, module) if __name__ == '__main__': diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index 36bc84d84..bfd97016f 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -28,7 +28,7 @@ class TestFileBmp(PillowTestCase): def test_invalid_file(self): with open("Tests/images/flower.jpg", "rb") as fp: self.assertRaises(SyntaxError, - lambda: BmpImagePlugin.BmpImageFile(fp)) + BmpImagePlugin.BmpImageFile, fp) def test_save_to_bytes(self): output = io.BytesIO() diff --git a/Tests/test_file_bufrstub.py b/Tests/test_file_bufrstub.py index 4932930ff..08980a996 100644 --- a/Tests/test_file_bufrstub.py +++ b/Tests/test_file_bufrstub.py @@ -24,8 +24,7 @@ class TestFileBufrStub(PillowTestCase): # Act / Assert self.assertRaises(SyntaxError, - lambda: - BufrStubImagePlugin.BufrStubImageFile(invalid_file)) + BufrStubImagePlugin.BufrStubImageFile, invalid_file) def test_load(self): # Arrange @@ -40,7 +39,7 @@ class TestFileBufrStub(PillowTestCase): tmpfile = self.tempfile("temp.bufr") # Act / Assert: stub cannot save without an implemented handler - self.assertRaises(IOError, lambda: im.save(tmpfile)) + self.assertRaises(IOError, im.save, tmpfile) if __name__ == '__main__': diff --git a/Tests/test_file_cur.py b/Tests/test_file_cur.py index e6d13ef30..23055a0ad 100644 --- a/Tests/test_file_cur.py +++ b/Tests/test_file_cur.py @@ -21,7 +21,7 @@ class TestFileCur(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: CurImagePlugin.CurImageFile(invalid_file)) + CurImagePlugin.CurImageFile, invalid_file) no_cursors_file = "Tests/images/no_cursors.cur" diff --git a/Tests/test_file_dcx.py b/Tests/test_file_dcx.py index 3da146d01..5e73fd10f 100644 --- a/Tests/test_file_dcx.py +++ b/Tests/test_file_dcx.py @@ -23,7 +23,7 @@ class TestFileDcx(PillowTestCase): def test_invalid_file(self): with open("Tests/images/flower.jpg", "rb") as fp: self.assertRaises(SyntaxError, - lambda: DcxImagePlugin.DcxImageFile(fp)) + DcxImagePlugin.DcxImageFile, fp) def test_tell(self): # Arrange @@ -58,7 +58,7 @@ class TestFileDcx(PillowTestCase): frame = 999 # too big on purpose # Act / Assert - self.assertRaises(EOFError, lambda: im.seek(frame)) + self.assertRaises(EOFError, im.seek, frame) if __name__ == '__main__': diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index eaf4df899..ecb34357a 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -55,7 +55,7 @@ class TestFileEps(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: EpsImagePlugin.EpsImageFile(invalid_file)) + EpsImagePlugin.EpsImageFile, invalid_file) def test_cmyk(self): cmyk_image = Image.open("Tests/images/pil_sample_cmyk.eps") @@ -97,7 +97,7 @@ class TestFileEps(PillowTestCase): def test_image_mode_not_supported(self): im = hopper("RGBA") tmpfile = self.tempfile('temp.eps') - self.assertRaises(ValueError, lambda: im.save(tmpfile)) + self.assertRaises(ValueError, im.save, tmpfile) def test_render_scale1(self): # We need png support for these render test diff --git a/Tests/test_file_fitsstub.py b/Tests/test_file_fitsstub.py index f875a2ce3..d74e983ce 100644 --- a/Tests/test_file_fitsstub.py +++ b/Tests/test_file_fitsstub.py @@ -24,8 +24,7 @@ class TestFileFitsStub(PillowTestCase): # Act / Assert self.assertRaises(SyntaxError, - lambda: - FitsStubImagePlugin.FITSStubImageFile(invalid_file)) + FitsStubImagePlugin.FITSStubImageFile, invalid_file) def test_load(self): # Arrange @@ -41,10 +40,10 @@ class TestFileFitsStub(PillowTestCase): dummy_filename = "dummy.filename" # Act / Assert: stub cannot save without an implemented handler - self.assertRaises(IOError, lambda: im.save(dummy_filename)) + self.assertRaises(IOError, im.save, dummy_filename) self.assertRaises( IOError, - lambda: FitsStubImagePlugin._save(im, dummy_fp, dummy_filename)) + FitsStubImagePlugin._save, im, dummy_fp, dummy_filename) if __name__ == '__main__': diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index 0da197155..09dd8d8a7 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -41,7 +41,7 @@ class TestFileFli(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: FliImagePlugin.FliImageFile(invalid_file)) + FliImagePlugin.FliImageFile, invalid_file) def test_n_frames(self): im = Image.open(static_test_file) @@ -71,7 +71,7 @@ class TestFileFli(PillowTestCase): self.assertEqual(im.tell(), 0) # Test seek past end of file - self.assertRaises(EOFError, lambda: im.seek(2)) + self.assertRaises(EOFError, im.seek, 2) def test_seek_tell(self): im = Image.open(animated_test_file) diff --git a/Tests/test_file_fpx.py b/Tests/test_file_fpx.py index e2b472ca6..a7c2f0c3b 100644 --- a/Tests/test_file_fpx.py +++ b/Tests/test_file_fpx.py @@ -9,12 +9,12 @@ class TestFileFpx(PillowTestCase): # Test an invalid OLE file invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: FpxImagePlugin.FpxImageFile(invalid_file)) + FpxImagePlugin.FpxImageFile, invalid_file) # Test a valid OLE file, but not an FPX file ole_file = "Tests/images/test-ole-file.doc" self.assertRaises(SyntaxError, - lambda: FpxImagePlugin.FpxImageFile(ole_file)) + FpxImagePlugin.FpxImageFile, ole_file) if __name__ == '__main__': diff --git a/Tests/test_file_gbr.py b/Tests/test_file_gbr.py index 97b2e97b3..aacc193f4 100644 --- a/Tests/test_file_gbr.py +++ b/Tests/test_file_gbr.py @@ -9,7 +9,7 @@ class TestFileGbr(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: GbrImagePlugin.GbrImageFile(invalid_file)) + GbrImagePlugin.GbrImageFile, invalid_file) def test_gbr_file(self): im = Image.open('Tests/images/gbr.gbr') diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 9f40485a5..cb0035948 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -31,7 +31,7 @@ class TestFileGif(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: GifImagePlugin.GifImageFile(invalid_file)) + GifImagePlugin.GifImageFile, invalid_file) def test_optimize(self): def test_grayscale(optimize): diff --git a/Tests/test_file_gimppalette.py b/Tests/test_file_gimppalette.py index d04458726..4ee5323bc 100644 --- a/Tests/test_file_gimppalette.py +++ b/Tests/test_file_gimppalette.py @@ -10,13 +10,13 @@ class TestImage(PillowTestCase): GimpPaletteFile(fp) with open('Tests/images/hopper.jpg', 'rb') as fp: - self.assertRaises(SyntaxError, lambda: GimpPaletteFile(fp)) + self.assertRaises(SyntaxError, GimpPaletteFile, fp) with open('Tests/images/bad_palette_file.gpl', 'rb') as fp: - self.assertRaises(SyntaxError, lambda: GimpPaletteFile(fp)) + self.assertRaises(SyntaxError, GimpPaletteFile, fp) with open('Tests/images/bad_palette_entry.gpl', 'rb') as fp: - self.assertRaises(ValueError, lambda: GimpPaletteFile(fp)) + self.assertRaises(ValueError, GimpPaletteFile, fp) def test_get_palette(self): # Arrange diff --git a/Tests/test_file_gribstub.py b/Tests/test_file_gribstub.py index dc0fa22d7..b3a6f1a5a 100644 --- a/Tests/test_file_gribstub.py +++ b/Tests/test_file_gribstub.py @@ -24,8 +24,7 @@ class TestFileGribStub(PillowTestCase): # Act / Assert self.assertRaises(SyntaxError, - lambda: - GribStubImagePlugin.GribStubImageFile(invalid_file)) + GribStubImagePlugin.GribStubImageFile, invalid_file) def test_load(self): # Arrange @@ -40,7 +39,7 @@ class TestFileGribStub(PillowTestCase): tmpfile = self.tempfile("temp.grib") # Act / Assert: stub cannot save without an implemented handler - self.assertRaises(IOError, lambda: im.save(tmpfile)) + self.assertRaises(IOError, im.save, tmpfile) if __name__ == '__main__': diff --git a/Tests/test_file_hdf5stub.py b/Tests/test_file_hdf5stub.py index cc2b80aa2..6cddd8d7b 100644 --- a/Tests/test_file_hdf5stub.py +++ b/Tests/test_file_hdf5stub.py @@ -24,8 +24,7 @@ class TestFileHdf5Stub(PillowTestCase): # Act / Assert self.assertRaises(SyntaxError, - lambda: - Hdf5StubImagePlugin.HDF5StubImageFile(invalid_file)) + Hdf5StubImagePlugin.HDF5StubImageFile, invalid_file) def test_load(self): # Arrange @@ -41,10 +40,10 @@ class TestFileHdf5Stub(PillowTestCase): dummy_filename = "dummy.filename" # Act / Assert: stub cannot save without an implemented handler - self.assertRaises(IOError, lambda: im.save(dummy_filename)) + self.assertRaises(IOError, im.save, dummy_filename) self.assertRaises( IOError, - lambda: Hdf5StubImagePlugin._save(im, dummy_fp, dummy_filename)) + Hdf5StubImagePlugin._save, im, dummy_fp, dummy_filename) if __name__ == '__main__': diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index 913d71586..d8508e579 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -98,7 +98,7 @@ class TestFileIcns(PillowTestCase): def test_not_an_icns_file(self): with io.BytesIO(b'invalid\n') as fp: self.assertRaises(SyntaxError, - lambda: IcnsImagePlugin.IcnsFile(fp)) + IcnsImagePlugin.IcnsFile, fp) if __name__ == '__main__': diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 4eb9435bd..20f21db57 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -18,7 +18,7 @@ class TestFileIco(PillowTestCase): def test_invalid_file(self): with open("Tests/images/flower.jpg", "rb") as fp: self.assertRaises(SyntaxError, - lambda: IcoImagePlugin.IcoImageFile(fp)) + IcoImagePlugin.IcoImageFile, fp) def test_save_to_bytes(self): output = io.BytesIO() diff --git a/Tests/test_file_im.py b/Tests/test_file_im.py index 5347d1536..e42197034 100644 --- a/Tests/test_file_im.py +++ b/Tests/test_file_im.py @@ -54,13 +54,13 @@ class TestFileIm(PillowTestCase): def test_save_unsupported_mode(self): out = self.tempfile('temp.im') im = hopper("HSV") - self.assertRaises(ValueError, lambda: im.save(out)) + self.assertRaises(ValueError, im.save, out) def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: ImImagePlugin.ImImageFile(invalid_file)) + ImImagePlugin.ImImageFile, invalid_file) def test_number(self): self.assertEqual(1.2, ImImagePlugin.number("1.2")) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 7f701d0f6..8ec428d04 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -312,7 +312,7 @@ class TestFileJpeg(PillowTestCase): self.assertEqual(getsampling(im), (2, 2, 1, 1, 1, 1)) self.assertRaises( - TypeError, lambda: self.roundtrip(hopper(), subsampling="1:1:1")) + TypeError, self.roundtrip, hopper(), subsampling="1:1:1") def test_exif(self): im = Image.open("Tests/images/pil_sample_rgb.jpg") @@ -421,18 +421,18 @@ class TestFileJpeg(PillowTestCase): self._n_qtables_helper(4, "Tests/images/pil_sample_cmyk.jpg") # not a sequence - self.assertRaises(Exception, lambda: self.roundtrip(im, qtables='a')) + self.assertRaises(Exception, self.roundtrip, im, qtables='a') # sequence wrong length - self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[])) + self.assertRaises(Exception, self.roundtrip, im, qtables=[]) # sequence wrong length self.assertRaises(Exception, - lambda: self.roundtrip(im, qtables=[1, 2, 3, 4, 5])) + self.roundtrip, im, qtables=[1, 2, 3, 4, 5]) # qtable entry not a sequence - self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[1])) + self.assertRaises(Exception, self.roundtrip, im, qtables=[1]) # qtable entry has wrong number of items self.assertRaises(Exception, - lambda: self.roundtrip(im, qtables=[[1, 2, 3, 4]])) + self.roundtrip, im, qtables=[[1, 2, 3, 4]]) @unittest.skipUnless(djpeg_available(), "djpeg not available") def test_load_djpeg(self): @@ -477,7 +477,7 @@ class TestFileJpeg(PillowTestCase): # Act # Shouldn't raise error fn = "Tests/images/sugarshack_bad_mpo_header.jpg" - im = self.assert_warning(UserWarning, lambda: Image.open(fn)) + im = self.assert_warning(UserWarning, Image.open, fn) # Assert self.assertEqual(im.format, "JPEG") @@ -582,7 +582,7 @@ class TestFileCloseW32(PillowTestCase): im = Image.open(tmpfile) fp = im.fp self.assertFalse(fp.closed) - self.assertRaises(Exception, lambda: os.remove(tmpfile)) + self.assertRaises(Exception, os.remove, tmpfile) im.load() self.assertTrue(fp.closed) # this should not fail, as load should have closed the file. diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 8aeb7ecae..753b50598 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -44,8 +44,7 @@ class TestFileJpeg2k(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: - Jpeg2KImagePlugin.Jpeg2KImageFile(invalid_file)) + Jpeg2KImagePlugin.Jpeg2KImageFile, invalid_file) def test_bytesio(self): with open('Tests/images/test-card-lossless.jp2', 'rb') as f: diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index c4e4e7d0b..302600eb8 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -362,10 +362,9 @@ class TestFileLibTiff(LibTiffTestCase): im = hopper('RGB') out = self.tempfile('temp.tif') - self.assertRaises( - IOError, lambda: im.save(out, compression='tiff_ccitt')) - self.assertRaises(IOError, lambda: im.save(out, compression='group3')) - self.assertRaises(IOError, lambda: im.save(out, compression='group4')) + self.assertRaises(IOError, im.save, out, compression='tiff_ccitt') + self.assertRaises(IOError, im.save, out, compression='group3') + self.assertRaises(IOError, im.save, out, compression='group4') def test_fp_leak(self): im = Image.open("Tests/images/hopper_g4_500.tif") @@ -373,10 +372,10 @@ class TestFileLibTiff(LibTiffTestCase): os.fstat(fn) im.load() # this should close it. - self.assertRaises(OSError, lambda: os.fstat(fn)) + self.assertRaises(OSError, os.fstat, fn) im = None # this should force even more closed. - self.assertRaises(OSError, lambda: os.fstat(fn)) - self.assertRaises(OSError, lambda: os.close(fn)) + self.assertRaises(OSError, os.fstat, fn) + self.assertRaises(OSError, os.close, fn) def test_multipage(self): # issue #862 diff --git a/Tests/test_file_mcidas.py b/Tests/test_file_mcidas.py index 6785ac4d9..491d8ea03 100644 --- a/Tests/test_file_mcidas.py +++ b/Tests/test_file_mcidas.py @@ -9,8 +9,7 @@ class TestFileMcIdas(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: - McIdasImagePlugin.McIdasImageFile(invalid_file)) + McIdasImagePlugin.McIdasImageFile, invalid_file) def test_valid_file(self): # Arrange diff --git a/Tests/test_file_mic.py b/Tests/test_file_mic.py index 8724a2983..44be9a475 100644 --- a/Tests/test_file_mic.py +++ b/Tests/test_file_mic.py @@ -42,19 +42,19 @@ class TestFileMic(PillowTestCase): im.seek(0) self.assertEqual(im.tell(), 0) - self.assertRaises(EOFError, lambda: im.seek(99)) + self.assertRaises(EOFError, im.seek, 99) self.assertEqual(im.tell(), 0) def test_invalid_file(self): # Test an invalid OLE file invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: MicImagePlugin.MicImageFile(invalid_file)) + MicImagePlugin.MicImageFile, invalid_file) # Test a valid OLE file, but not a MIC file ole_file = "Tests/images/test-ole-file.doc" self.assertRaises(SyntaxError, - lambda: MicImagePlugin.MicImageFile(ole_file)) + MicImagePlugin.MicImageFile, ole_file) if __name__ == '__main__': diff --git a/Tests/test_file_msp.py b/Tests/test_file_msp.py index 9c58deeaf..4aac88092 100644 --- a/Tests/test_file_msp.py +++ b/Tests/test_file_msp.py @@ -26,7 +26,7 @@ class TestFileMsp(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: MspImagePlugin.MspImageFile(invalid_file)) + MspImagePlugin.MspImageFile, invalid_file) def test_bad_checksum(self): # Arrange @@ -35,7 +35,7 @@ class TestFileMsp(PillowTestCase): # Act / Assert self.assertRaises(SyntaxError, - lambda: MspImagePlugin.MspImageFile(bad_checksum)) + MspImagePlugin.MspImageFile, bad_checksum) def test_open_windows_v1(self): # Arrange @@ -77,7 +77,7 @@ class TestFileMsp(PillowTestCase): filename = self.tempfile("temp.msp") # Act/Assert - self.assertRaises(IOError, lambda: im.save(filename)) + self.assertRaises(IOError, im.save, filename) if __name__ == '__main__': diff --git a/Tests/test_file_palm.py b/Tests/test_file_palm.py index 9f88df373..b97a9b19e 100644 --- a/Tests/test_file_palm.py +++ b/Tests/test_file_palm.py @@ -51,7 +51,7 @@ class TestFilePalm(PillowTestCase): mode = "RGB" # Act / Assert - self.assertRaises(IOError, lambda: self.helper_save_as_palm(mode)) + self.assertRaises(IOError, self.helper_save_as_palm, mode) if __name__ == '__main__': diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index 9766318e6..415827e49 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -21,13 +21,14 @@ class TestFilePcx(PillowTestCase): # Test an unsupported mode f = self.tempfile("temp.pcx") - self.assertRaises(ValueError, lambda: hopper("RGBA").save(f)) + im = hopper("RGBA") + self.assertRaises(ValueError, im.save, f) def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: PcxImagePlugin.PcxImageFile(invalid_file)) + PcxImagePlugin.PcxImageFile, invalid_file) def test_odd(self): # see issue #523, odd sized images should have a stride that's even. diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index 2caa4cdab..5cb04595a 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -59,7 +59,7 @@ class TestFilePdf(PillowTestCase): im = hopper("LA") outfile = self.tempfile("temp_LA.pdf") - self.assertRaises(ValueError, lambda: im.save(outfile)) + self.assertRaises(ValueError, im.save, outfile) def test_save_all(self): # Single frame image diff --git a/Tests/test_file_pixar.py b/Tests/test_file_pixar.py index b6b3f934d..ae8c7d5f5 100644 --- a/Tests/test_file_pixar.py +++ b/Tests/test_file_pixar.py @@ -22,7 +22,7 @@ class TestFilePixar(PillowTestCase): self.assertRaises( SyntaxError, - lambda: PixarImagePlugin.PixarImageFile(invalid_file)) + PixarImagePlugin.PixarImageFile, invalid_file) if __name__ == '__main__': diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 53d67a7f6..e7b747622 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -87,14 +87,14 @@ class TestFilePng(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: PngImagePlugin.PngImageFile(invalid_file)) + PngImagePlugin.PngImageFile, invalid_file) def test_broken(self): # Check reading of totally broken files. In this case, the test # file was checked into Subversion as a text file. test_file = "Tests/images/broken.png" - self.assertRaises(IOError, lambda: Image.open(test_file)) + self.assertRaises(IOError, Image.open, test_file) def test_bad_text(self): # Make sure PIL can read malformed tEXt chunks (@PIL152) @@ -424,7 +424,7 @@ class TestFilePng(PillowTestCase): data = b'\x89' + fd.read() pngfile = BytesIO(data) - self.assertRaises(IOError, lambda: Image.open(pngfile)) + self.assertRaises(IOError, Image.open, pngfile) def test_trns_rgb(self): # Check writing and reading of tRNS chunks for RGB images. diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index fcb26258f..937a9dc32 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -39,7 +39,7 @@ class TestFilePpm(PillowTestCase): with open(path, 'w') as f: f.write('P6') - self.assertRaises(ValueError, lambda: Image.open(path)) + self.assertRaises(ValueError, Image.open, path) def test_neg_ppm(self): # Storage.c accepted negative values for xsize, ysize. the diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index e2a9419b8..99bd0ea80 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -21,7 +21,7 @@ class TestImagePsd(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: PsdImagePlugin.PsdImageFile(invalid_file)) + PsdImagePlugin.PsdImageFile, invalid_file) def test_n_frames(self): im = Image.open("Tests/images/hopper_merged.psd") @@ -66,7 +66,7 @@ class TestImagePsd(PillowTestCase): def test_seek_eoferror(self): im = Image.open(test_file) - self.assertRaises(EOFError, lambda: im.seek(-1)) + self.assertRaises(EOFError, im.seek, -1) def test_icc_profile(self): im = Image.open(test_file) diff --git a/Tests/test_file_sgi.py b/Tests/test_file_sgi.py index a535a1730..e78217f6f 100644 --- a/Tests/test_file_sgi.py +++ b/Tests/test_file_sgi.py @@ -42,8 +42,7 @@ class TestFileSgi(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(ValueError, - lambda: - SgiImagePlugin.SgiImageFile(invalid_file)) + SgiImagePlugin.SgiImageFile, invalid_file) def test_write(self): def roundtrip(img): @@ -62,14 +61,14 @@ class TestFileSgi(PillowTestCase): im = hopper('LA') out = self.tempfile('temp.sgi') - self.assertRaises(ValueError, lambda: im.save(out, format='sgi')) + self.assertRaises(ValueError, im.save, out, format='sgi') def test_incorrect_number_of_bands(self): im = hopper('YCbCr') im.mode = 'RGB' out = self.tempfile('temp.sgi') - self.assertRaises(ValueError, lambda: im.save(out, format='sgi')) + self.assertRaises(ValueError, im.save, out, format='sgi') if __name__ == '__main__': diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index 32f1f55e5..a2a27a3b3 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -84,12 +84,12 @@ class TestImageSpider(PillowTestCase): def test_invalid_file(self): invalid_file = "Tests/images/invalid.spider" - self.assertRaises(IOError, lambda: Image.open(invalid_file)) + self.assertRaises(IOError, Image.open, invalid_file) def test_nonstack_file(self): im = Image.open(TEST_FILE) - self.assertRaises(EOFError, lambda: im.seek(0)) + self.assertRaises(EOFError, im.seek, 0) def test_nonstack_dos(self): im = Image.open(TEST_FILE) diff --git a/Tests/test_file_sun.py b/Tests/test_file_sun.py index d5e4699fd..6bb6b98d4 100644 --- a/Tests/test_file_sun.py +++ b/Tests/test_file_sun.py @@ -24,7 +24,7 @@ class TestFileSun(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: SunImagePlugin.SunImageFile(invalid_file)) + SunImagePlugin.SunImageFile, invalid_file) def test_im1(self): im = Image.open('Tests/images/sunraster.im1') diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index fff1028a6..2a9132d48 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -144,11 +144,11 @@ class TestFileTiff(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: TiffImagePlugin.TiffImageFile(invalid_file)) + TiffImagePlugin.TiffImageFile, invalid_file) TiffImagePlugin.PREFIXES.append(b"\xff\xd8\xff\xe0") self.assertRaises(SyntaxError, - lambda: TiffImagePlugin.TiffImageFile(invalid_file)) + TiffImagePlugin.TiffImageFile, invalid_file) TiffImagePlugin.PREFIXES.pop() def test_bad_exif(self): @@ -167,7 +167,7 @@ class TestFileTiff(PillowTestCase): def test_save_unsupported_mode(self): im = hopper("HSV") outfile = self.tempfile("temp.tif") - self.assertRaises(IOError, lambda: im.save(outfile)) + self.assertRaises(IOError, im.save, outfile) def test_little_endian(self): im = Image.open('Tests/images/16bit.cropped.tif') @@ -344,7 +344,7 @@ class TestFileTiff(PillowTestCase): filename = "Tests/images/pil136.tiff" im = Image.open(filename) self.assertEqual(im.tell(), 0) - self.assertRaises(EOFError, lambda: im.seek(1)) + self.assertRaises(EOFError, im.seek, 1) def test__limit_rational_int(self): from PIL.TiffImagePlugin import _limit_rational @@ -519,7 +519,7 @@ class TestFileTiffW32(PillowTestCase): im = Image.open(tmpfile) fp = im.fp self.assertFalse(fp.closed) - self.assertRaises(Exception, lambda: os.remove(tmpfile)) + self.assertRaises(Exception, os.remove, tmpfile) im.load() self.assertTrue(fp.closed) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 844703370..9d11e32ef 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -161,7 +161,7 @@ class TestFileTiffMetadata(PillowTestCase): head = f.read(8) info = TiffImagePlugin.ImageFileDirectory(head) try: - self.assert_warning(UserWarning, lambda: info.load(f)) + self.assert_warning(UserWarning, info.load, f) except struct.error: self.fail("Should not be struct errors there.") diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index a15ca97df..6c16882fd 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -75,7 +75,8 @@ class TestFileWebp(PillowTestCase): def test_write_unsupported_mode(self): temp_file = self.tempfile("temp.webp") - self.assertRaises(IOError, lambda: hopper("L").save(temp_file)) + im = hopper("L") + self.assertRaises(IOError, im.save, temp_file) def test_WebPEncode_with_invalid_args(self): self.assertRaises(TypeError, _webp.WebPEncode) diff --git a/Tests/test_file_wmf.py b/Tests/test_file_wmf.py index 6a6ad8f23..1a15a514f 100644 --- a/Tests/test_file_wmf.py +++ b/Tests/test_file_wmf.py @@ -50,7 +50,7 @@ class TestFileWmf(PillowTestCase): for ext in [".wmf", ".emf"]: tmpfile = self.tempfile("temp"+ext) - self.assertRaises(IOError, lambda: im.save(tmpfile)) + self.assertRaises(IOError, im.save, tmpfile) if __name__ == '__main__': diff --git a/Tests/test_file_xpm.py b/Tests/test_file_xpm.py index d8cc4fa3a..4fa3f743f 100644 --- a/Tests/test_file_xpm.py +++ b/Tests/test_file_xpm.py @@ -21,7 +21,7 @@ class TestFileXpm(PillowTestCase): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, - lambda: XpmImagePlugin.XpmImageFile(invalid_file)) + XpmImagePlugin.XpmImageFile, invalid_file) def test_load_read(self): # Arrange diff --git a/Tests/test_file_xvthumb.py b/Tests/test_file_xvthumb.py index 5337a8390..d0256cabf 100644 --- a/Tests/test_file_xvthumb.py +++ b/Tests/test_file_xvthumb.py @@ -25,8 +25,7 @@ class TestFileXVThumb(PillowTestCase): # Act / Assert self.assertRaises(SyntaxError, - lambda: - XVThumbImagePlugin.XVThumbImageFile(bad_file)) + XVThumbImagePlugin.XVThumbImageFile, bad_file) def test_invalid_file(self): # Arrange @@ -34,8 +33,7 @@ class TestFileXVThumb(PillowTestCase): # Act / Assert self.assertRaises(SyntaxError, - lambda: - XVThumbImagePlugin.XVThumbImageFile(invalid_file)) + XVThumbImagePlugin.XVThumbImageFile, invalid_file) if __name__ == '__main__': diff --git a/Tests/test_font_bdf.py b/Tests/test_font_bdf.py index 20007215b..7c8fe579e 100644 --- a/Tests/test_font_bdf.py +++ b/Tests/test_font_bdf.py @@ -17,7 +17,7 @@ class TestFontBdf(PillowTestCase): def test_invalid_file(self): with open("Tests/images/flower.jpg", "rb") as fp: - self.assertRaises(SyntaxError, lambda: BdfFontFile.BdfFontFile(fp)) + self.assertRaises(SyntaxError, BdfFontFile.BdfFontFile, fp) if __name__ == '__main__': diff --git a/Tests/test_font_pcf.py b/Tests/test_font_pcf.py index 4c0333050..90e204303 100644 --- a/Tests/test_font_pcf.py +++ b/Tests/test_font_pcf.py @@ -32,7 +32,7 @@ class TestFontPcf(PillowTestCase): def test_invalid_file(self): with open("Tests/images/flower.jpg", "rb") as fp: - self.assertRaises(SyntaxError, lambda: PcfFontFile.PcfFontFile(fp)) + self.assertRaises(SyntaxError, PcfFontFile.PcfFontFile, fp) def xtest_draw(self): diff --git a/Tests/test_image.py b/Tests/test_image.py index 8f8fe70ca..7ef1fa1ad 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -47,10 +47,9 @@ class TestImage(PillowTestCase): self.assertEqual(im2.getcolors(), [(10000, 0)]) self.assertEqual(im3.getcolors(), [(10000, 0)]) - self.assertRaises(ValueError, lambda: Image.new("X", (100, 100))) - self.assertRaises(ValueError, lambda: Image.new("", (100, 100))) - # self.assertRaises( - # MemoryError, lambda: Image.new("L", (1000000, 1000000))) + self.assertRaises(ValueError, Image.new, "X", (100, 100)) + self.assertRaises(ValueError, Image.new, "", (100, 100)) + # self.assertRaises(MemoryError, Image.new, "L", (1000000, 1000000)) def test_width_height(self): im = Image.new("RGB", (1, 2)) @@ -68,10 +67,10 @@ class TestImage(PillowTestCase): else: import io im = io.BytesIO(b'') - self.assertRaises(IOError, lambda: Image.open(im)) + self.assertRaises(IOError, Image.open, im) def test_bad_mode(self): - self.assertRaises(ValueError, lambda: Image.open("filename", "bad mode")) + self.assertRaises(ValueError, Image.open, "filename", "bad mode") @unittest.skipIf(sys.version_info < (3, 4), "pathlib only available in Python 3.4 or later") @@ -112,7 +111,7 @@ class TestImage(PillowTestCase): def test_unknown_extension(self): im = hopper() temp_file = self.tempfile("temp.unknown") - self.assertRaises(ValueError, lambda: im.save(temp_file)) + self.assertRaises(ValueError, im.save, temp_file) def test_internals(self): @@ -278,17 +277,17 @@ class TestImage(PillowTestCase): # errors self.assertRaises(ValueError, - lambda: source.alpha_composite(over, "invalid source")) + source.alpha_composite, over, "invalid source") self.assertRaises(ValueError, - lambda: source.alpha_composite(over, (0, 0), "invalid destination")) + source.alpha_composite, over, (0, 0), "invalid destination") self.assertRaises(ValueError, - lambda: source.alpha_composite(over, (0))) + source.alpha_composite, over, (0)) self.assertRaises(ValueError, - lambda: source.alpha_composite(over, (0, 0), (0))) + source.alpha_composite, over, (0, 0), (0)) self.assertRaises(ValueError, - lambda: source.alpha_composite(over, (0, -1))) + source.alpha_composite, over, (0, -1)) self.assertRaises(ValueError, - lambda: source.alpha_composite(over, (0, 0), (0, -1))) + source.alpha_composite, over, (0, 0), (0, -1)) def test_registered_extensions_uninitialized(self): # Arrange @@ -344,7 +343,7 @@ class TestImage(PillowTestCase): # Act/Assert self.assertRaises( ValueError, - lambda: Image.effect_mandelbrot(size, extent, quality)) + Image.effect_mandelbrot, size, extent, quality) def test_effect_noise(self): # Arrange @@ -405,7 +404,7 @@ class TestImage(PillowTestCase): im = hopper() # Act / Assert - self.assertRaises(NotImplementedError, lambda: im.offset(None)) + self.assertRaises(NotImplementedError, im.offset, None) def test_fromstring(self): self.assertRaises(NotImplementedError, Image.fromstring) @@ -416,7 +415,7 @@ class TestImage(PillowTestCase): # Act / Assert self.assertRaises(ValueError, - lambda: Image.linear_gradient(wrong_mode)) + Image.linear_gradient, wrong_mode) return def test_linear_gradient(self): @@ -442,7 +441,7 @@ class TestImage(PillowTestCase): # Act / Assert self.assertRaises(ValueError, - lambda: Image.radial_gradient(wrong_mode)) + Image.radial_gradient, wrong_mode) return def test_radial_gradient(self): @@ -465,7 +464,7 @@ class TestImage(PillowTestCase): def test_remap_palette(self): # Test illegal image mode im = hopper() - self.assertRaises(ValueError, lambda: im.remap_palette(None)) + self.assertRaises(ValueError, im.remap_palette, None) class MockEncoder(object): @@ -491,10 +490,10 @@ class TestRegistry(PillowTestCase): self.assertEqual(enc.args, ('RGB', 'args', 'extra')) def test_encode_registry_fail(self): - self.assertRaises(IOError, lambda: Image._getencoder('RGB', - 'DoesNotExist', - ('args',), - extra=('extra',))) + self.assertRaises(IOError, Image._getencoder, 'RGB', + 'DoesNotExist', + ('args',), + extra=('extra',)) if __name__ == '__main__': unittest.main() diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index f56e2f4ed..4482a2d78 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -104,7 +104,7 @@ class TestImageConvert(PillowTestCase): p = self.assert_warning( UserWarning, - lambda: im.convert('P', palette=Image.ADAPTIVE)) + im.convert, 'P', palette=Image.ADAPTIVE) self.assertNotIn('transparency', p.info) p.save(f) @@ -124,7 +124,7 @@ class TestImageConvert(PillowTestCase): p = self.assert_warning( UserWarning, - lambda: im.convert('P', palette=Image.ADAPTIVE)) + im.convert, 'P', palette=Image.ADAPTIVE) self.assertNotIn('transparency', p.info) p.save(f) @@ -148,7 +148,7 @@ class TestImageConvert(PillowTestCase): # Act / Assert self.assertRaises(ValueError, - lambda: im.convert(mode='CMYK', matrix=matrix)) + im.convert, mode='CMYK', matrix=matrix) def test_matrix_wrong_mode(self): # Arrange @@ -161,7 +161,7 @@ class TestImageConvert(PillowTestCase): # Act / Assert self.assertRaises(ValueError, - lambda: im.convert(mode='L', matrix=matrix)) + im.convert, mode='L', matrix=matrix) def test_matrix_xyz(self): diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index c8a397e7e..99ec41cc1 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -34,7 +34,7 @@ class TestImageFilter(PillowTestCase): filter(ImageFilter.UnsharpMask) filter(ImageFilter.UnsharpMask(10)) - self.assertRaises(TypeError, lambda: filter("hello")) + self.assertRaises(TypeError, filter, "hello") def test_crash(self): @@ -83,7 +83,7 @@ class TestImageFilter(PillowTestCase): self.assertEqual(rankfilter("1"), (0, 4, 8)) self.assertEqual(rankfilter("L"), (0, 4, 8)) - self.assertRaises(ValueError, lambda: rankfilter("P")) + self.assertRaises(ValueError, rankfilter, "P") self.assertEqual(rankfilter("RGB"), ((0, 0, 0), (4, 0, 0), (8, 0, 0))) self.assertEqual(rankfilter("I"), (0, 4, 8)) self.assertEqual(rankfilter("F"), (0.0, 4.0, 8.0)) diff --git a/Tests/test_image_load.py b/Tests/test_image_load.py index c01558861..15a92e339 100644 --- a/Tests/test_image_load.py +++ b/Tests/test_image_load.py @@ -19,7 +19,7 @@ class TestImageLoad(PillowTestCase): im = Image.open("Tests/images/hopper.gif") im.close() self.assertRaises(ValueError, im.load) - self.assertRaises(ValueError, lambda: im.getpixel((0, 0))) + self.assertRaises(ValueError, im.getpixel, (0, 0)) def test_contextmanager(self): fn = None @@ -27,7 +27,7 @@ class TestImageLoad(PillowTestCase): fn = im.fp.fileno() os.fstat(fn) - self.assertRaises(OSError, lambda: os.fstat(fn)) + self.assertRaises(OSError, os.fstat, fn) if __name__ == '__main__': unittest.main() diff --git a/Tests/test_image_point.py b/Tests/test_image_point.py index a6d20daa4..977e98e83 100644 --- a/Tests/test_image_point.py +++ b/Tests/test_image_point.py @@ -6,17 +6,17 @@ class TestImagePoint(PillowTestCase): def test_sanity(self): im = hopper() - self.assertRaises(ValueError, lambda: im.point(list(range(256)))) + self.assertRaises(ValueError, im.point, list(range(256))) im.point(list(range(256))*3) im.point(lambda x: x) im = im.convert("I") - self.assertRaises(ValueError, lambda: im.point(list(range(256)))) + self.assertRaises(ValueError, im.point, list(range(256))) im.point(lambda x: x*1) im.point(lambda x: x+1) im.point(lambda x: x*1+1) - self.assertRaises(TypeError, lambda: im.point(lambda x: x-1)) - self.assertRaises(TypeError, lambda: im.point(lambda x: x/1)) + self.assertRaises(TypeError, im.point, lambda x: x-1) + self.assertRaises(TypeError, im.point, lambda x: x/1) def test_16bit_lut(self): """ Tests for 16 bit -> 8 bit lut for converting I->L images @@ -36,7 +36,8 @@ class TestImagePoint(PillowTestCase): self.assert_image_equal(out.convert('L'), im.point(int_lut, 'L')) def test_f_mode(self): - self.assertRaises(ValueError, lambda: hopper('F').point(None)) + im = hopper('F') + self.assertRaises(ValueError, im.point, None) if __name__ == '__main__': diff --git a/Tests/test_image_putpalette.py b/Tests/test_image_putpalette.py index 47bebea6f..e173f0000 100644 --- a/Tests/test_image_putpalette.py +++ b/Tests/test_image_putpalette.py @@ -13,14 +13,14 @@ class TestImagePutPalette(PillowTestCase): if p: return im.mode, p[:10] return im.mode - self.assertRaises(ValueError, lambda: palette("1")) + self.assertRaises(ValueError, palette, "1") self.assertEqual(palette("L"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) self.assertEqual(palette("P"), ("P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) - self.assertRaises(ValueError, lambda: palette("I")) - self.assertRaises(ValueError, lambda: palette("F")) - self.assertRaises(ValueError, lambda: palette("RGB")) - self.assertRaises(ValueError, lambda: palette("RGBA")) - self.assertRaises(ValueError, lambda: palette("YCbCr")) + self.assertRaises(ValueError, palette, "I") + self.assertRaises(ValueError, palette, "F") + self.assertRaises(ValueError, palette, "RGB") + self.assertRaises(ValueError, palette, "RGBA") + self.assertRaises(ValueError, palette, "YCbCr") def test_imagepalette(self): im = hopper("P") diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index 82c89584a..d9f52fb03 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -39,7 +39,7 @@ class TestImageQuantize(PillowTestCase): def test_rgba_quantize(self): image = hopper('RGBA') image.quantize() - self.assertRaises(Exception, lambda: image.quantize(method=0)) + self.assertRaises(Exception, image.quantize, method=0) def test_quantize(self): image = Image.open('Tests/images/caption_6_33_22.png').convert('RGB') diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index e08c8f502..8d14b9008 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -114,7 +114,7 @@ class TestImageResize(PillowTestCase): # Test unknown resampling filter im = hopper() - self.assertRaises(ValueError, lambda: im.resize((10, 10), "unknown")) + self.assertRaises(ValueError, im.resize, (10, 10), "unknown") if __name__ == '__main__': diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index da254eaf6..e03cfe24a 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -141,8 +141,8 @@ class TestImageTransform(PillowTestCase): self.test_mesh() def test_missing_method_data(self): - self.assertRaises(ValueError, lambda: - hopper().transform((100, 100), None)) + im = hopper() + self.assertRaises(ValueError, im.transform, (100, 100), None) class TestImageTransformAffine(PillowTestCase): diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index 105ca9f16..9e304ae01 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -148,22 +148,22 @@ class TestImageCms(PillowTestCase): psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") - self.assertRaises(ValueError, lambda: t.apply_in_place(hopper("RGBA"))) + self.assertRaises(ValueError, t.apply_in_place, hopper("RGBA")) # the procedural pyCMS API uses PyCMSError for all sorts of errors self.assertRaises( ImageCms.PyCMSError, - lambda: ImageCms.profileToProfile(hopper(), "foo", "bar")) + ImageCms.profileToProfile, hopper(), "foo", "bar") self.assertRaises( ImageCms.PyCMSError, - lambda: ImageCms.buildTransform("foo", "bar", "RGB", "RGB")) + ImageCms.buildTransform, "foo", "bar", "RGB", "RGB") self.assertRaises( ImageCms.PyCMSError, - lambda: ImageCms.getProfileName(None)) + ImageCms.getProfileName, None) self.skip_missing() self.assertRaises( ImageCms.PyCMSError, - lambda: ImageCms.isIntentSupported(SRGB, None, None)) + ImageCms.isIntentSupported, SRGB, None, None) def test_display_profile(self): # try fetching the profile for the current display device @@ -175,11 +175,11 @@ class TestImageCms(PillowTestCase): def test_unsupported_color_space(self): self.assertRaises(ImageCms.PyCMSError, - lambda: ImageCms.createProfile("unsupported")) + ImageCms.createProfile, "unsupported") def test_invalid_color_temperature(self): self.assertRaises(ImageCms.PyCMSError, - lambda: ImageCms.createProfile("LAB", "invalid")) + ImageCms.createProfile, "LAB", "invalid") def test_simple_lab(self): i = Image.new('RGB', (10, 10), (128, 128, 128)) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 9c989d2e5..d130a0cd2 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -56,8 +56,7 @@ class TestImageDraw(PillowTestCase): def test_mode_mismatch(self): im = hopper("RGB").copy() - self.assertRaises(ValueError, - lambda: ImageDraw.ImageDraw(im, mode="L")) + self.assertRaises(ValueError, ImageDraw.ImageDraw, im, mode="L") def helper_arc(self, bbox, start, end): # Arrange diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index d150d996d..6ef74989d 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -69,7 +69,7 @@ class TestImageFile(PillowTestCase): im1, im2 = roundtrip("JPEG") # lossy compression self.assert_image(im1, im2.mode, im2.size) - self.assertRaises(IOError, lambda: roundtrip("PDF")) + self.assertRaises(IOError, roundtrip, "PDF") def test_ico(self): with open('Tests/images/python.ico', 'rb') as f: @@ -93,7 +93,7 @@ class TestImageFile(PillowTestCase): self.assert_image_equal(im1, im2) def test_raise_ioerror(self): - self.assertRaises(IOError, lambda: ImageFile.raise_ioerror(1)) + self.assertRaises(IOError, ImageFile.raise_ioerror, 1) def test_raise_typeerror(self): with self.assertRaises(TypeError): @@ -182,7 +182,7 @@ class TestPyDecoder(PillowTestCase): self.assertEqual(d.state.xsize, xsize) self.assertEqual(d.state.ysize, ysize) - self.assertRaises(ValueError, lambda: d.set_as_raw(b'\x00')) + self.assertRaises(ValueError, d.set_as_raw, b'\x00') def test_extents_none(self): buf = BytesIO(b'\x00'*255) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 9f159e9e0..e0e40d4a2 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -106,7 +106,7 @@ class TestImageFont(PillowTestCase): # Usage note: making two fonts from the same buffer fails. # shared_bytes = self._font_as_bytes() # self._render(shared_bytes) - # self.assertRaises(Exception, lambda: _render(shared_bytes)) + # self.assertRaises(Exception, _render, shared_bytes) def test_font_with_open_file(self): with open(FONT_PATH, 'rb') as f: @@ -211,9 +211,9 @@ class TestImageFont(PillowTestCase): # Act/Assert self.assertRaises(AssertionError, - lambda: draw.multiline_text((0, 0), TEST_TEXT, - font=ttf, - align="unknown")) + draw.multiline_text, (0, 0), TEST_TEXT, + font=ttf, + align="unknown") def test_draw_align(self): im = Image.new('RGB', (300, 100), 'white') @@ -384,7 +384,7 @@ class TestImageFont(PillowTestCase): filename = "somefilenamethatdoesntexist.ttf" # Act/Assert - self.assertRaises(IOError, lambda: ImageFont.load_path(filename)) + self.assertRaises(IOError, ImageFont.load_path, filename) def test_default_font(self): # Arrange diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index 0ce8ac6da..b51b212e0 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -81,9 +81,9 @@ class MorphTests(PillowTestCase): 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)) + self.assertRaises(Exception, mop.apply, None) + self.assertRaises(Exception, mop.match, None) + self.assertRaises(Exception, mop.save_lut, None) # Test the named patterns def test_erosion8(self): @@ -214,9 +214,9 @@ class MorphTests(PillowTestCase): im = hopper('RGB') mop = ImageMorph.MorphOp(op_name="erosion8") - self.assertRaises(Exception, lambda: mop.apply(im)) - self.assertRaises(Exception, lambda: mop.match(im)) - self.assertRaises(Exception, lambda: mop.get_on_pixels(im)) + self.assertRaises(Exception, mop.apply, im) + self.assertRaises(Exception, mop.match, im) + self.assertRaises(Exception, mop.get_on_pixels, im) def test_add_patterns(self): # Arrange @@ -240,7 +240,7 @@ class MorphTests(PillowTestCase): def test_unknown_pattern(self): self.assertRaises( Exception, - lambda: ImageMorph.LutBuilder(op_name='unknown')) + ImageMorph.LutBuilder, op_name='unknown') def test_pattern_syntax_error(self): # Arrange @@ -249,9 +249,7 @@ class MorphTests(PillowTestCase): lb.add_patterns(new_patterns) # Act / Assert - self.assertRaises( - Exception, - lambda: lb.build_lut()) + self.assertRaises(Exception, lb.build_lut) def test_load_invalid_mrl(self): # Arrange @@ -259,7 +257,7 @@ class MorphTests(PillowTestCase): mop = ImageMorph.MorphOp() # Act / Assert - self.assertRaises(Exception, lambda: mop.load_lut(invalid_mrl)) + self.assertRaises(Exception, mop.load_lut, invalid_mrl) def test_roundtrip_mrl(self): # Arrange diff --git a/Tests/test_imageops_usm.py b/Tests/test_imageops_usm.py index 358beb6b4..eed8ff754 100644 --- a/Tests/test_imageops_usm.py +++ b/Tests/test_imageops_usm.py @@ -37,26 +37,26 @@ class TestImageOpsUsm(PillowTestCase): def test_usm_formats(self): usm = ImageOps.unsharp_mask - self.assertRaises(ValueError, lambda: usm(im.convert("1"))) + self.assertRaises(ValueError, usm, im.convert("1")) usm(im.convert("L")) - self.assertRaises(ValueError, lambda: usm(im.convert("I"))) - self.assertRaises(ValueError, lambda: usm(im.convert("F"))) + self.assertRaises(ValueError, usm, im.convert("I")) + self.assertRaises(ValueError, usm, im.convert("F")) usm(im.convert("RGB")) usm(im.convert("RGBA")) usm(im.convert("CMYK")) - self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr"))) + self.assertRaises(ValueError, usm, im.convert("YCbCr")) def test_blur_formats(self): blur = ImageOps.gaussian_blur - self.assertRaises(ValueError, lambda: blur(im.convert("1"))) + self.assertRaises(ValueError, blur, im.convert("1")) blur(im.convert("L")) - self.assertRaises(ValueError, lambda: blur(im.convert("I"))) - self.assertRaises(ValueError, lambda: blur(im.convert("F"))) + self.assertRaises(ValueError, blur, im.convert("I")) + self.assertRaises(ValueError, blur, im.convert("F")) blur(im.convert("RGB")) blur(im.convert("RGBA")) blur(im.convert("CMYK")) - self.assertRaises(ValueError, lambda: blur(im.convert("YCbCr"))) + self.assertRaises(ValueError, blur, im.convert("YCbCr")) def test_usm_accuracy(self): diff --git a/Tests/test_imagepalette.py b/Tests/test_imagepalette.py index 6afd09113..889f022ae 100644 --- a/Tests/test_imagepalette.py +++ b/Tests/test_imagepalette.py @@ -9,7 +9,7 @@ class TestImagePalette(PillowTestCase): ImagePalette.ImagePalette("RGB", list(range(256))*3) self.assertRaises(ValueError, - lambda: ImagePalette.ImagePalette("RGB", list(range(256))*2)) + ImagePalette.ImagePalette, "RGB", list(range(256))*2) def test_getcolor(self): @@ -20,10 +20,10 @@ class TestImagePalette(PillowTestCase): test_map[palette.getcolor((i, i, i))] = i self.assertEqual(len(test_map), 256) - self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3))) + self.assertRaises(ValueError, palette.getcolor, (1, 2, 3)) # Test unknown color specifier - self.assertRaises(ValueError, lambda: palette.getcolor("unknown")) + self.assertRaises(ValueError, palette.getcolor, "unknown") def test_file(self): @@ -65,9 +65,8 @@ class TestImagePalette(PillowTestCase): white = 255 # Act - self.assertRaises( - NotImplementedError, - lambda: ImagePalette.make_linear_lut(black, white)) + self.assertRaises(NotImplementedError, + ImagePalette.make_linear_lut, black, white) def test_make_gamma_lut(self): # Arrange @@ -92,9 +91,9 @@ class TestImagePalette(PillowTestCase): # Act / Assert self.assertRaises(ValueError, palette.tobytes) - self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3))) + self.assertRaises(ValueError, palette.getcolor, (1, 2, 3)) f = self.tempfile("temp.lut") - self.assertRaises(ValueError, lambda: palette.save(f)) + self.assertRaises(ValueError, palette.save, f) def test_getdata(self): # Arrange @@ -134,7 +133,7 @@ class TestImagePalette(PillowTestCase): def test_invalid_palette(self): self.assertRaises(IOError, - lambda: ImagePalette.load("Tests/images/hopper.jpg")) + ImagePalette.load, "Tests/images/hopper.jpg") if __name__ == '__main__': diff --git a/Tests/test_imagesequence.py b/Tests/test_imagesequence.py index e50b13924..2a4a358ba 100644 --- a/Tests/test_imagesequence.py +++ b/Tests/test_imagesequence.py @@ -22,7 +22,7 @@ class TestImageSequence(PillowTestCase): self.assertEqual(index, 1) - self.assertRaises(AttributeError, lambda: ImageSequence.Iterator(0)) + self.assertRaises(AttributeError, ImageSequence.Iterator, 0) def test_iterator(self): im = Image.open('Tests/images/multipage.tiff') @@ -30,7 +30,7 @@ class TestImageSequence(PillowTestCase): for index in range(0, im.n_frames): self.assertEqual(i[index], next(i)) self.assertRaises(IndexError, lambda: i[index+1]) - self.assertRaises(StopIteration, lambda: next(i)) + self.assertRaises(StopIteration, next, i) def _test_multipage_tiff(self): im = Image.open('Tests/images/multipage.tiff') diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index fe07473bc..da91e35c7 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -33,7 +33,7 @@ class TestImageShow(PillowTestCase): self.assertIsNone(viewer.get_format(None)) - self.assertRaises(NotImplementedError, lambda: viewer.get_command(None)) + self.assertRaises(NotImplementedError, viewer.get_command, None) if __name__ == '__main__': diff --git a/Tests/test_imagestat.py b/Tests/test_imagestat.py index 5e0ef06fe..77eb0aac1 100644 --- a/Tests/test_imagestat.py +++ b/Tests/test_imagestat.py @@ -26,7 +26,7 @@ class TestImageStat(PillowTestCase): self.assertRaises(AttributeError, lambda: st.spam) - self.assertRaises(TypeError, lambda: ImageStat.Stat(1)) + self.assertRaises(TypeError, ImageStat.Stat, 1) def test_hopper(self): diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 2b4e2528e..aefee2e08 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -29,8 +29,8 @@ class TestLibImage(PillowTestCase): im.im.setmode("RGB") self.assertEqual(im.im.getpixel((0, 0)), (1, 2, 3)) - self.assertRaises(ValueError, lambda: im.im.setmode("L")) - self.assertRaises(ValueError, lambda: im.im.setmode("RGBABCDE")) + self.assertRaises(ValueError, im.im.setmode, "L") + self.assertRaises(ValueError, im.im.setmode, "RGBABCDE") if __name__ == '__main__': diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 792ab603e..0529cce4d 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -60,7 +60,7 @@ class TestNumpy(PillowTestCase): # Check supported 1-bit integer formats self.assert_image(to_image(numpy.bool, 1, 1), '1', TEST_IMAGE_SIZE) self.assert_image(to_image(numpy.bool8, 1, 1), '1', TEST_IMAGE_SIZE) - + # Check supported 8-bit integer formats self.assert_image(to_image(numpy.uint8), "L", TEST_IMAGE_SIZE) self.assert_image(to_image(numpy.uint8, 3), "RGB", TEST_IMAGE_SIZE) @@ -86,12 +86,12 @@ class TestNumpy(PillowTestCase): self.assert_image(to_image(numpy.int32), "I", TEST_IMAGE_SIZE) # Check 64-bit integer formats - self.assertRaises(TypeError, lambda: to_image(numpy.uint64)) - self.assertRaises(TypeError, lambda: to_image(numpy.int64)) + self.assertRaises(TypeError, to_image, numpy.uint64) + self.assertRaises(TypeError, to_image, numpy.int64) # Check floating-point formats self.assert_image(to_image(numpy.float), "F", TEST_IMAGE_SIZE) - self.assertRaises(TypeError, lambda: to_image(numpy.float16)) + self.assertRaises(TypeError, to_image, numpy.float16) self.assert_image(to_image(numpy.float32), "F", TEST_IMAGE_SIZE) self.assert_image(to_image(numpy.float64), "F", TEST_IMAGE_SIZE) @@ -216,7 +216,7 @@ class TestNumpy(PillowTestCase): def test_bool(self): # https://github.com/python-pillow/Pillow/issues/2044 a = numpy.zeros((10,2), dtype=numpy.bool) - a[0][0] = True + a[0][0] = True im2 = Image.fromarray(a) self.assertEqual(im2.getdata()[0], 255) From 2ac18689d5d901ca57b011d60f0e76a48a6f12ae Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 31 Aug 2017 20:58:50 +1000 Subject: [PATCH 5/7] Removed unnecessary return statements --- Tests/test_image.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 7ef1fa1ad..6004132c4 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -416,7 +416,6 @@ class TestImage(PillowTestCase): # Act / Assert self.assertRaises(ValueError, Image.linear_gradient, wrong_mode) - return def test_linear_gradient(self): @@ -442,7 +441,6 @@ class TestImage(PillowTestCase): # Act / Assert self.assertRaises(ValueError, Image.radial_gradient, wrong_mode) - return def test_radial_gradient(self): From 5c2df30ac398bb68c876aadea167a9066fcc60b1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 31 Aug 2017 21:51:21 +1000 Subject: [PATCH 6/7] Removed ImageDraw.Draw del calls --- Tests/test_imagedraw.py | 23 +---------------------- Tests/test_imagefont.py | 10 ---------- 2 files changed, 1 insertion(+), 32 deletions(-) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index d130a0cd2..e4bcb9faa 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -51,7 +51,6 @@ class TestImageDraw(PillowTestCase): draw = ImageDraw.Draw(im) draw.line(((0, 0)), fill=(0, 0, 0)) - del draw def test_mode_mismatch(self): im = hopper("RGB").copy() @@ -65,7 +64,6 @@ class TestImageDraw(PillowTestCase): # Act draw.arc(bbox, start, end) - del draw # Assert self.assert_image_similar( @@ -88,7 +86,6 @@ class TestImageDraw(PillowTestCase): # Act draw.arc(BBOX1, start=start, end=end) - del draw # Assert self.assert_image_equal( @@ -104,7 +101,6 @@ class TestImageDraw(PillowTestCase): # Act draw.arc(BBOX1, start=start, end=end) - del draw # Assert self.assert_image_similar( @@ -118,7 +114,6 @@ class TestImageDraw(PillowTestCase): # Act draw.bitmap((10, 10), small) - del draw # Assert self.assert_image_equal( @@ -132,7 +127,6 @@ class TestImageDraw(PillowTestCase): # Act draw.chord(bbox, start, end, fill="red", outline="yellow") - del draw # Assert self.assert_image_similar(im, Image.open(expected), 1) @@ -155,7 +149,6 @@ class TestImageDraw(PillowTestCase): # Act draw.ellipse(bbox, fill="green", outline="blue") - del draw # Assert self.assert_image_similar(im, Image.open(expected), 1) @@ -175,7 +168,6 @@ class TestImageDraw(PillowTestCase): # Act draw.ellipse(((0, 0), (W-1, H)), fill="white") - del draw # Assert self.assert_image_similar( @@ -188,7 +180,6 @@ class TestImageDraw(PillowTestCase): # Act draw.line(points, fill="yellow", width=2) - del draw # Assert self.assert_image_equal( @@ -216,7 +207,6 @@ class TestImageDraw(PillowTestCase): s.line(x0, y0) draw.shape(s, fill=1) - del draw # Assert self.assert_image_equal( @@ -238,7 +228,6 @@ class TestImageDraw(PillowTestCase): s.line(x0, y0) draw.shape(s, outline="blue") - del draw # Assert self.assert_image_equal( @@ -251,7 +240,6 @@ class TestImageDraw(PillowTestCase): # Act draw.pieslice(bbox, start, end, fill="white", outline="blue") - del draw # Assert self.assert_image_similar( @@ -272,7 +260,6 @@ class TestImageDraw(PillowTestCase): # Act draw.point(points, fill="yellow") - del draw # Assert self.assert_image_equal( @@ -291,7 +278,6 @@ class TestImageDraw(PillowTestCase): # Act draw.polygon(points, fill="red", outline="blue") - del draw # Assert self.assert_image_equal( @@ -315,7 +301,6 @@ class TestImageDraw(PillowTestCase): # Act draw.polygon(KITE_POINTS, fill="blue", outline="yellow") - del draw # Assert self.assert_image_equal(im, Image.open(expected)) @@ -327,7 +312,6 @@ class TestImageDraw(PillowTestCase): # Act draw.rectangle(bbox, fill="black", outline="green") - del draw # Assert self.assert_image_equal( @@ -349,7 +333,6 @@ class TestImageDraw(PillowTestCase): # Act draw.rectangle(bbox, fill="orange") - del draw # Assert self.assert_image_similar(im, Image.open(expected), 1) @@ -376,7 +359,6 @@ class TestImageDraw(PillowTestCase): # 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") @@ -393,7 +375,6 @@ class TestImageDraw(PillowTestCase): ImageDraw.floodfill( im, centre_point, ImageColor.getrgb("red"), border=ImageColor.getrgb("black")) - del draw # Assert self.assert_image_equal( @@ -413,8 +394,7 @@ class TestImageDraw(PillowTestCase): ImageDraw.floodfill( im, centre_point, ImageColor.getrgb("red"), thresh=30) - del draw - + # Assert self.assert_image_equal( im, Image.open("Tests/images/imagedraw_floodfill2.png")) @@ -576,7 +556,6 @@ class TestImageDraw(PillowTestCase): # Act draw.line([(50, 50), (50, 50)], width=3) - del draw # Assert self.assert_image_similar(im, Image.open(expected), 1) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index e0e40d4a2..b09c64b2c 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -141,7 +141,6 @@ class TestImageFont(PillowTestCase): size = draw.textsize(txt, ttf) draw.text((10, 10), txt, font=ttf) draw.rectangle((10, 10, 10 + size[0], 10 + size[1])) - del draw target = 'Tests/images/rectangle_surrounding_text.png' target_img = Image.open(target) @@ -188,7 +187,6 @@ class TestImageFont(PillowTestCase): draw.text((0, 0), TEST_TEXT, fill=None, font=ttf, anchor=None, spacing=4, align="left") draw.text((0, 0), TEST_TEXT, None, ttf, None, 4, "left") - del draw # Test align center and right for align, ext in {"center": "_center", @@ -196,7 +194,6 @@ class TestImageFont(PillowTestCase): im = Image.new(mode='RGB', size=(300, 100)) draw = ImageDraw.Draw(im) draw.multiline_text((0, 0), TEST_TEXT, font=ttf, align=align) - del draw target = 'Tests/images/multiline_text'+ext+'.png' target_img = Image.open(target) @@ -221,7 +218,6 @@ class TestImageFont(PillowTestCase): ttf = self.get_font() line = "some text" draw.text((100, 40), line, (0, 0, 0), font=ttf, align='left') - del draw def test_multiline_size(self): ttf = self.get_font() @@ -236,7 +232,6 @@ class TestImageFont(PillowTestCase): # to multiline_textsize() draw.textsize(TEST_TEXT, font=ttf, spacing=4) draw.textsize(TEST_TEXT, ttf, 4) - del draw def test_multiline_width(self): ttf = self.get_font() @@ -246,7 +241,6 @@ class TestImageFont(PillowTestCase): self.assertEqual(draw.textsize("longest line", font=ttf)[0], draw.multiline_textsize("longest line\nline", font=ttf)[0]) - del draw def test_multiline_spacing(self): ttf = self.get_font() @@ -254,7 +248,6 @@ class TestImageFont(PillowTestCase): im = Image.new(mode='RGB', size=(300, 100)) draw = ImageDraw.Draw(im) draw.multiline_text((0, 0), TEST_TEXT, font=ttf, spacing=10) - del draw target = 'Tests/images/multiline_text_spacing.png' target_img = Image.open(target) @@ -279,7 +272,6 @@ class TestImageFont(PillowTestCase): # Rotated font draw.font = transposed_font box_size_b = draw.textsize(word) - del draw # Check (w,h) of box a is (h,w) of box b self.assertEqual(box_size_a[0], box_size_b[1]) @@ -302,7 +294,6 @@ class TestImageFont(PillowTestCase): # Rotated font draw.font = transposed_font box_size_b = draw.textsize(word) - del draw # Check boxes a and b are same size self.assertEqual(box_size_a, box_size_b) @@ -398,7 +389,6 @@ class TestImageFont(PillowTestCase): # Act default_font = ImageFont.load_default() draw.text((10, 10), txt, font=default_font) - del draw # Assert self.assert_image_equal(im, target_img) From 6602c7cec490563adb61a7f3fbc3ef245140bfc9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 1 Sep 2017 20:04:22 +1000 Subject: [PATCH 7/7] Removed debugging lines --- PIL/TgaImagePlugin.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/PIL/TgaImagePlugin.py b/PIL/TgaImagePlugin.py index 0bc52529a..75bef2a21 100644 --- a/PIL/TgaImagePlugin.py +++ b/PIL/TgaImagePlugin.py @@ -139,16 +139,13 @@ SAVE = { } -def _save(im, fp, filename, check=0): +def _save(im, fp, filename): try: rawmode, bits, colormaptype, imagetype = SAVE[im.mode] except KeyError: raise IOError("cannot write mode %s as TGA" % im.mode) - if check: - return check - if colormaptype: colormapfirst, colormaplength, colormapentry = 0, 256, 24 else: