Replace PillowTestCase.assert_warning with pytest.warns

This commit is contained in:
Hugo 2020-02-03 11:11:32 +02:00
parent 63881ab198
commit 38bf862185
24 changed files with 72 additions and 76 deletions

View File

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

View File

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

View File

@ -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"}
)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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