diff --git a/Tests/helper.py b/Tests/helper.py index 769a34377..e4b1d917c 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -178,31 +178,6 @@ class PillowTestCase(unittest.TestCase): except OSError: pass # report? - def assert_warning(self, warn_class, func, *args, **kwargs): - import warnings - - with warnings.catch_warnings(record=True) as w: - # Cause all warnings to always be triggered. - warnings.simplefilter("always") - - # Hopefully trigger a warning. - result = func(*args, **kwargs) - - # Verify some things. - if warn_class is None: - self.assertEqual( - len(w), 0, "Expected no warnings, got %s" % [v.category for v in w] - ) - else: - self.assertGreaterEqual(len(w), 1) - found = False - for v in w: - if issubclass(v.category, warn_class): - found = True - break - self.assertTrue(found) - return result - def tempfile(self, template): assert template[:5] in ("temp.", "temp_") fd, path = tempfile.mkstemp(template[4:], template[:4]) diff --git a/Tests/test_bmp_reference.py b/Tests/test_bmp_reference.py index c493c7852..c4fe25910 100644 --- a/Tests/test_bmp_reference.py +++ b/Tests/test_bmp_reference.py @@ -1,5 +1,6 @@ import os +import pytest from PIL import Image from .helper import PillowTestCase, assert_image_similar @@ -28,7 +29,7 @@ class TestBmpReference(PillowTestCase): pass # Assert that there is no unclosed file warning - self.assert_warning(None, open, f) + pytest.warns(None, open, f) def test_questionable(self): """ These shouldn't crash/dos, but it's not well defined that these diff --git a/Tests/test_core_resources.py b/Tests/test_core_resources.py index 9dd8bd065..22563b92a 100644 --- a/Tests/test_core_resources.py +++ b/Tests/test_core_resources.py @@ -1,6 +1,7 @@ import sys import unittest +import pytest from PIL import Image from .helper import PillowTestCase, is_pypy @@ -169,12 +170,12 @@ class TestEnvVars(PillowTestCase): self.assertEqual(Image.core.get_block_size(), 2 * 1024 * 1024) def test_warnings(self): - self.assert_warning( + pytest.warns( UserWarning, Image._apply_env_variables, {"PILLOW_ALIGNMENT": "15"} ) - self.assert_warning( + pytest.warns( UserWarning, Image._apply_env_variables, {"PILLOW_BLOCK_SIZE": "1024"} ) - self.assert_warning( + pytest.warns( UserWarning, Image._apply_env_variables, {"PILLOW_BLOCKS_MAX": "wat"} ) diff --git a/Tests/test_decompression_bomb.py b/Tests/test_decompression_bomb.py index 5d0d37099..e55cf23dc 100644 --- a/Tests/test_decompression_bomb.py +++ b/Tests/test_decompression_bomb.py @@ -1,3 +1,4 @@ +import pytest from PIL import Image from .helper import PillowTestCase, hopper @@ -38,7 +39,7 @@ class TestDecompressionBomb(PillowTestCase): with Image.open(TEST_FILE): pass - self.assert_warning(Image.DecompressionBombWarning, open) + pytest.warns(Image.DecompressionBombWarning, open) def test_exception(self): # Set limit to trigger exception on the test file @@ -71,7 +72,7 @@ class TestDecompressionCrop(PillowTestCase): # Crops can extend the extents, therefore we should have the # same decompression bomb warnings on them. box = (0, 0, self.src.width * 2, self.src.height * 2) - self.assert_warning(Image.DecompressionBombWarning, self.src.crop, box) + pytest.warns(Image.DecompressionBombWarning, self.src.crop, box) def test_crop_decompression_checks(self): @@ -87,7 +88,7 @@ class TestDecompressionCrop(PillowTestCase): self.assertEqual(im.crop(value).size, (9, 9)) for value in warning_values: - self.assert_warning(Image.DecompressionBombWarning, im.crop, value) + pytest.warns(Image.DecompressionBombWarning, im.crop, value) for value in error_values: with self.assertRaises(Image.DecompressionBombError): diff --git a/Tests/test_file_dcx.py b/Tests/test_file_dcx.py index 23bab5356..36f46299c 100644 --- a/Tests/test_file_dcx.py +++ b/Tests/test_file_dcx.py @@ -1,5 +1,6 @@ import unittest +import pytest from PIL import DcxImagePlugin, Image from .helper import PillowTestCase, assert_image_equal, hopper, is_pypy @@ -27,7 +28,7 @@ class TestFileDcx(PillowTestCase): im = Image.open(TEST_FILE) im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -35,14 +36,14 @@ class TestFileDcx(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open(TEST_FILE) as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_invalid_file(self): with open("Tests/images/flower.jpg", "rb") as fp: diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index d6779a457..45c0fd1a1 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -1,5 +1,6 @@ import unittest +import pytest from PIL import FliImagePlugin, Image from .helper import PillowTestCase, assert_image_equal, is_pypy @@ -34,7 +35,7 @@ class TestFileFli(PillowTestCase): im = Image.open(static_test_file) im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -42,14 +43,14 @@ class TestFileFli(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open(static_test_file) as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_tell(self): # Arrange diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 0709a66a6..d9226cdba 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1,6 +1,7 @@ import unittest from io import BytesIO +import pytest from PIL import GifImagePlugin, Image, ImageDraw, ImagePalette from .helper import ( @@ -47,7 +48,7 @@ class TestFileGif(PillowTestCase): im = Image.open(TEST_GIF) im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -55,14 +56,14 @@ class TestFileGif(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open(TEST_GIF) as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" @@ -684,7 +685,7 @@ class TestFileGif(PillowTestCase): # Single frame im = Image.new("RGB", (1, 1)) im.info["transparency"] = (255, 0, 0) - self.assert_warning(UserWarning, im.save, out) + pytest.warns(UserWarning, im.save, out) with Image.open(out) as reloaded: self.assertNotIn("transparency", reloaded.info) @@ -693,7 +694,7 @@ class TestFileGif(PillowTestCase): im = Image.new("RGB", (1, 1)) im.info["transparency"] = b"" ims = [Image.new("RGB", (1, 1))] - self.assert_warning(UserWarning, im.save, out, save_all=True, append_images=ims) + pytest.warns(UserWarning, im.save, out, save_all=True, append_images=ims) with Image.open(out) as reloaded: self.assertNotIn("transparency", reloaded.info) diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index 5a1d958fc..5693a19fd 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -2,6 +2,7 @@ import io import sys import unittest +import pytest from PIL import IcnsImagePlugin, Image from .helper import PillowTestCase, assert_image_equal, assert_image_similar @@ -19,7 +20,7 @@ class TestFileIcns(PillowTestCase): with Image.open(TEST_FILE) as im: # Assert that there is no unclosed file warning - self.assert_warning(None, im.load) + pytest.warns(None, im.load) self.assertEqual(im.mode, "RGBA") self.assertEqual(im.size, (1024, 1024)) diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 9e0a78256..47fae7ed9 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -1,5 +1,6 @@ import io +import pytest from PIL import IcoImagePlugin, Image, ImageDraw from .helper import PillowTestCase, assert_image_equal, hopper @@ -87,7 +88,7 @@ class TestFileIco(PillowTestCase): with Image.open("Tests/images/hopper_unexpected.ico") as im: self.assertEqual(im.size, (16, 16)) - self.assert_warning(UserWarning, open) + pytest.warns(UserWarning, open) def test_draw_reloaded(self): with Image.open(TEST_ICO_FILE) as im: diff --git a/Tests/test_file_im.py b/Tests/test_file_im.py index 66f4e7be1..ec046020e 100644 --- a/Tests/test_file_im.py +++ b/Tests/test_file_im.py @@ -1,5 +1,6 @@ import unittest +import pytest from PIL import Image, ImImagePlugin from .helper import PillowTestCase, assert_image_equal, hopper, is_pypy @@ -22,7 +23,7 @@ class TestFileIm(PillowTestCase): im = Image.open(TEST_IM) im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -30,14 +31,14 @@ class TestFileIm(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open(TEST_IM) as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_tell(self): # Arrange diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 92ffa586f..eb623b408 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -1,6 +1,7 @@ import os from io import BytesIO +import pytest from PIL import Image, ImageFile, JpegImagePlugin from .helper import ( @@ -531,7 +532,7 @@ class TestFileJpeg(PillowTestCase): # Act # Shouldn't raise error fn = "Tests/images/sugarshack_bad_mpo_header.jpg" - with self.assert_warning(UserWarning, Image.open, fn) as im: + with pytest.warns(UserWarning, Image.open, fn) as im: # Assert self.assertEqual(im.format, "JPEG") diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 12405294e..5ed1bb75e 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -1,6 +1,7 @@ import unittest from io import BytesIO +import pytest from PIL import Image from .helper import PillowTestCase, assert_image_similar, is_pypy @@ -38,7 +39,7 @@ class TestFileMpo(PillowTestCase): im = Image.open(test_files[0]) im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -46,14 +47,14 @@ class TestFileMpo(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open(test_files[0]) as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_app(self): for test_file in test_files: diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index cf878f33b..a3d3c0897 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -338,7 +338,7 @@ class TestFilePng(PillowTestCase): with Image.open(TEST_PNG_FILE) as im: # Assert that there is no unclosed file warning - self.assert_warning(None, im.verify) + pytest.warns(None, im.verify) with Image.open(TEST_PNG_FILE) as im: im.load() diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index a5eb9aa61..e77964d32 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -1,5 +1,6 @@ import unittest +import pytest from PIL import Image, PsdImagePlugin from .helper import PillowTestCase, assert_image_similar, hopper, is_pypy @@ -24,7 +25,7 @@ class TestImagePsd(PillowTestCase): im = Image.open(test_file) im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -32,14 +33,14 @@ class TestImagePsd(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open(test_file) as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index c2ced2a9f..243b5e466 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -2,6 +2,7 @@ import tempfile import unittest from io import BytesIO +import pytest from PIL import Image, ImageSequence, SpiderImagePlugin from .helper import PillowTestCase, assert_image_equal, hopper, is_pypy @@ -23,7 +24,7 @@ class TestImageSpider(PillowTestCase): im = Image.open(TEST_FILE) im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -31,14 +32,14 @@ class TestImageSpider(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open(TEST_FILE) as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_save(self): # Arrange diff --git a/Tests/test_file_tar.py b/Tests/test_file_tar.py index a77e4e84c..240f7acba 100644 --- a/Tests/test_file_tar.py +++ b/Tests/test_file_tar.py @@ -1,5 +1,6 @@ import unittest +import pytest from PIL import Image, TarIO from .helper import PillowTestCase, is_pypy @@ -33,18 +34,18 @@ class TestFileTar(PillowTestCase): def open(): TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_close(self): def open(): tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") tar.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_contextmanager(self): def open(): with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"): pass - self.assert_warning(None, open) + pytest.warns(None, open) diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index 8a446028d..61d8e7efe 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -2,6 +2,7 @@ import os from glob import glob from itertools import product +import pytest from PIL import Image from .helper import PillowTestCase, assert_image_equal, hopper @@ -136,7 +137,7 @@ class TestFileTga(PillowTestCase): # Save with custom id section greater than 255 characters id_section = b"Test content" * 25 - self.assert_warning(UserWarning, lambda: im.save(out, id_section=id_section)) + pytest.warns(UserWarning, lambda: im.save(out, id_section=id_section)) with Image.open(out) as test_im: self.assertEqual(test_im.info["id_section"], id_section[:255]) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index d415f29ee..8140fa3fe 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -60,7 +60,7 @@ class TestFileTiff(PillowTestCase): im = Image.open("Tests/images/multipage.tiff") im.load() - self.assert_warning(ResourceWarning, open) + pytest.warns(ResourceWarning, open) def test_closed_file(self): def open(): @@ -68,14 +68,14 @@ class TestFileTiff(PillowTestCase): im.load() im.close() - self.assert_warning(None, open) + pytest.warns(None, open) def test_context_manager(self): def open(): with Image.open("Tests/images/multipage.tiff") as im: im.load() - self.assert_warning(None, open) + pytest.warns(None, open) def test_mac_tiff(self): # Read RGBa images from macOS [@PIL136] @@ -185,7 +185,7 @@ class TestFileTiff(PillowTestCase): def test_bad_exif(self): with Image.open("Tests/images/hopper_bad_exif.jpg") as i: # Should not raise struct.error. - self.assert_warning(UserWarning, i._getexif) + pytest.warns(UserWarning, i._getexif) def test_save_rgba(self): im = hopper("RGBA") diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index a46b09aaf..e46e94668 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -1,6 +1,7 @@ import io import struct +import pytest from PIL import Image, TiffImagePlugin, TiffTags from PIL.TiffImagePlugin import IFDRational @@ -174,7 +175,7 @@ class TestFileTiffMetadata(PillowTestCase): head = f.read(8) info = TiffImagePlugin.ImageFileDirectory(head) # Should not raise struct.error. - self.assert_warning(UserWarning, info.load, f) + pytest.warns(UserWarning, info.load, f) def test_iccprofile(self): # https://github.com/python-pillow/Pillow/issues/1462 @@ -333,4 +334,4 @@ class TestFileTiffMetadata(PillowTestCase): ifd.tagtype[277] = TiffTags.SHORT # Should not raise ValueError. - self.assert_warning(UserWarning, lambda: ifd[277]) + pytest.warns(UserWarning, lambda: ifd[277]) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 16ed14ce1..81742c659 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -1,5 +1,6 @@ import unittest +import pytest from PIL import Image, WebPImagePlugin from .helper import ( @@ -23,7 +24,7 @@ class TestUnsupportedWebp(PillowTestCase): WebPImagePlugin.SUPPORTED = False file_path = "Tests/images/hopper.webp" - self.assert_warning( + pytest.warns( UserWarning, lambda: self.assertRaises(IOError, Image.open, file_path) ) @@ -146,7 +147,7 @@ class TestFileWebp(PillowTestCase): file_path = "Tests/images/hopper.webp" with Image.open(file_path) as image: temp_file = self.tempfile("temp.webp") - self.assert_warning(None, image.save, temp_file) + pytest.warns(None, image.save, temp_file) def test_file_pointer_could_be_reused(self): file_path = "Tests/images/hopper.webp" diff --git a/Tests/test_image.py b/Tests/test_image.py index 2cddd604f..334e330f4 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -4,6 +4,7 @@ import shutil import tempfile import unittest +import pytest from PIL import Image, UnidentifiedImageError from .helper import ( @@ -576,7 +577,7 @@ class TestImage(PillowTestCase): # Act/Assert with Image.open(test_file) as im: - self.assert_warning(None, im.save, temp_file) + pytest.warns(None, im.save, temp_file) def test_load_on_nonexclusive_multiframe(self): with open("Tests/images/frozenpond.mpo", "rb") as fp: diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 53f872d2f..b7355a3ad 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -1,3 +1,4 @@ +import pytest from PIL import Image from .helper import ( @@ -122,7 +123,7 @@ class TestImageConvert(PillowTestCase): self.assertIn("transparency", im_p.info) im_p.save(f) - im_p = self.assert_warning(UserWarning, im.convert, "P", palette=Image.ADAPTIVE) + im_p = pytest.warns(UserWarning, im.convert, "P", palette=Image.ADAPTIVE) self.assertNotIn("transparency", im_p.info) im_p.save(f) @@ -144,7 +145,7 @@ class TestImageConvert(PillowTestCase): self.assertNotIn("transparency", im_rgba.info) im_rgba.save(f) - im_p = self.assert_warning(UserWarning, im.convert, "P", palette=Image.ADAPTIVE) + im_p = pytest.warns(UserWarning, im.convert, "P", palette=Image.ADAPTIVE) self.assertNotIn("transparency", im_p.info) im_p.save(f) diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index 07aa26692..ba704f4c8 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -2,6 +2,7 @@ import datetime import os from io import BytesIO +import pytest from PIL import Image, ImageMode from .helper import ( @@ -447,7 +448,7 @@ class TestImageCms(PillowTestCase): p = o.profile def helper_deprecated(attr, expected): - result = self.assert_warning(DeprecationWarning, getattr, p, attr) + result = pytest.warns(DeprecationWarning, getattr, p, attr) self.assertEqual(result, expected) # p.color_space diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 19ed76591..45fabee78 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -1,5 +1,6 @@ import unittest +import pytest from PIL import Image from .helper import PillowTestCase, assert_deep_equal, assert_image, hopper @@ -220,4 +221,4 @@ class TestNumpy(PillowTestCase): with Image.open(test_file) as im: # Act/Assert - self.assert_warning(None, lambda: array(im)) + pytest.warns(None, lambda: array(im))