Converted addCleanup

This commit is contained in:
Andrew Murray 2020-03-27 19:50:51 +11:00
parent 7b628a5ef6
commit 748739c992
5 changed files with 204 additions and 177 deletions

View File

@ -61,9 +61,8 @@ class TestDecompressionBomb(PillowTestCase):
class TestDecompressionCrop(PillowTestCase): class TestDecompressionCrop(PillowTestCase):
def setUp(self): def setUp(self):
self.src = hopper() width, height = 128, 128
self.addCleanup(self.src.close) Image.MAX_IMAGE_PIXELS = height * width * 4 - 1
Image.MAX_IMAGE_PIXELS = self.src.height * self.src.width * 4 - 1
def tearDown(self): def tearDown(self):
Image.MAX_IMAGE_PIXELS = ORIGINAL_LIMIT Image.MAX_IMAGE_PIXELS = ORIGINAL_LIMIT
@ -71,8 +70,9 @@ class TestDecompressionCrop(PillowTestCase):
def testEnlargeCrop(self): def testEnlargeCrop(self):
# Crops can extend the extents, therefore we should have the # Crops can extend the extents, therefore we should have the
# same decompression bomb warnings on them. # same decompression bomb warnings on them.
box = (0, 0, self.src.width * 2, self.src.height * 2) with hopper() as src:
pytest.warns(Image.DecompressionBombWarning, self.src.crop, box) box = (0, 0, src.width * 2, src.height * 2)
pytest.warns(Image.DecompressionBombWarning, src.crop, box)
def test_crop_decompression_checks(self): def test_crop_decompression_checks(self):

View File

@ -1,3 +1,4 @@
import os
import pytest import pytest
from PIL import FontFile, Image, ImageDraw, ImageFont, PcfFontFile from PIL import FontFile, Image, ImageDraw, ImageFont, PcfFontFile
@ -13,17 +14,23 @@ fontname = "Tests/fonts/10x20-ISO8859-1.pcf"
message = "hello, world" message = "hello, world"
@skip_unless_feature("zlib") pytestmark = skip_unless_feature("zlib")
class TestFontPcf(PillowTestCase):
def save_font(self): def save_font(request, tmp_path):
with open(fontname, "rb") as test_file: with open(fontname, "rb") as test_file:
font = PcfFontFile.PcfFontFile(test_file) font = PcfFontFile.PcfFontFile(test_file)
assert isinstance(font, FontFile.FontFile) assert isinstance(font, FontFile.FontFile)
# check the number of characters in the font # check the number of characters in the font
assert len([_f for _f in font.glyph if _f]) == 223 assert len([_f for _f in font.glyph if _f]) == 223
tempname = self.tempfile("temp.pil") tempname = str(tmp_path / "temp.pil")
self.addCleanup(self.delete_tempfile, tempname[:-4] + ".pbm")
def delete_tempfile():
try:
os.remove(tempname[:-4] + ".pbm")
except OSError:
pass # report?
request.addfinalizer(delete_tempfile)
font.save(tempname) font.save(tempname)
with Image.open(tempname.replace(".pil", ".pbm")) as loaded: with Image.open(tempname.replace(".pil", ".pbm")) as loaded:
@ -35,16 +42,16 @@ class TestFontPcf(PillowTestCase):
assert f_loaded.read() == f_target.read() assert f_loaded.read() == f_target.read()
return tempname return tempname
def test_sanity(self): def test_sanity(request, tmp_path):
self.save_font() save_font(request, tmp_path)
def test_invalid_file(self): def test_invalid_file():
with open("Tests/images/flower.jpg", "rb") as fp: with open("Tests/images/flower.jpg", "rb") as fp:
with pytest.raises(SyntaxError): with pytest.raises(SyntaxError):
PcfFontFile.PcfFontFile(fp) PcfFontFile.PcfFontFile(fp)
def test_draw(self): def test_draw(request, tmp_path):
tempname = self.save_font() tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname) font = ImageFont.load(tempname)
im = Image.new("L", (130, 30), "white") im = Image.new("L", (130, 30), "white")
draw = ImageDraw.Draw(im) draw = ImageDraw.Draw(im)
@ -52,8 +59,8 @@ class TestFontPcf(PillowTestCase):
with Image.open("Tests/images/test_draw_pbm_target.png") as target: with Image.open("Tests/images/test_draw_pbm_target.png") as target:
assert_image_similar(im, target, 0) assert_image_similar(im, target, 0)
def test_textsize(self): def test_textsize(request, tmp_path):
tempname = self.save_font() tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname) font = ImageFont.load(tempname)
for i in range(255): for i in range(255):
(dx, dy) = font.getsize(chr(i)) (dx, dy) = font.getsize(chr(i))
@ -63,8 +70,8 @@ class TestFontPcf(PillowTestCase):
msg = message[: l + 1] msg = message[: l + 1]
assert font.getsize(msg) == (len(msg) * 10, 20) assert font.getsize(msg) == (len(msg) * 10, 20)
def _test_high_characters(self, message): def _test_high_characters(request, tmp_path, message):
tempname = self.save_font() tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname) font = ImageFont.load(tempname)
im = Image.new("L", (750, 30), "white") im = Image.new("L", (750, 30), "white")
draw = ImageDraw.Draw(im) draw = ImageDraw.Draw(im)
@ -72,8 +79,8 @@ class TestFontPcf(PillowTestCase):
with Image.open("Tests/images/high_ascii_chars.png") as target: with Image.open("Tests/images/high_ascii_chars.png") as target:
assert_image_similar(im, target, 0) assert_image_similar(im, target, 0)
def test_high_characters(self): def test_high_characters(request, tmp_path):
message = "".join(chr(i + 1) for i in range(140, 232)) message = "".join(chr(i + 1) for i in range(140, 232))
self._test_high_characters(message) _test_high_characters(request, tmp_path, message)
# accept bytes instances. # accept bytes instances.
self._test_high_characters(message.encode("latin1")) _test_high_characters(request, tmp_path, message.encode("latin1"))

View File

@ -1,21 +1,26 @@
import pytest
from PIL import Image, ImageQt from PIL import Image, ImageQt
from .helper import PillowTestCase, assert_image_equal, hopper from .helper import PillowTestCase, assert_image_equal, hopper
from .test_imageqt import PillowQtTestCase from .test_imageqt import skip_if_qt_is_not_installed
class TestFromQImage(PillowQtTestCase, PillowTestCase): pytestmark = skip_if_qt_is_not_installed()
def setUp(self):
super().setUp() @pytest.fixture
self.files_to_test = [ def test_images():
ims = [
hopper(), hopper(),
Image.open("Tests/images/transparent.png"), Image.open("Tests/images/transparent.png"),
Image.open("Tests/images/7x13.png"), Image.open("Tests/images/7x13.png"),
] ]
for im in self.files_to_test: try:
self.addCleanup(im.close) yield ims
finally:
for im in ims.values():
im.close()
def roundtrip(self, expected): def roundtrip(expected):
# PIL -> Qt # PIL -> Qt
intermediate = expected.toqimage() intermediate = expected.toqimage()
# Qt -> PIL # Qt -> PIL
@ -26,22 +31,22 @@ class TestFromQImage(PillowQtTestCase, PillowTestCase):
else: else:
assert_image_equal(result, expected.convert("RGB")) assert_image_equal(result, expected.convert("RGB"))
def test_sanity_1(self): def test_sanity_1(test_images):
for im in self.files_to_test: for im in test_images:
self.roundtrip(im.convert("1")) roundtrip(im.convert("1"))
def test_sanity_rgb(self): def test_sanity_rgb(test_images):
for im in self.files_to_test: for im in test_images:
self.roundtrip(im.convert("RGB")) roundtrip(im.convert("RGB"))
def test_sanity_rgba(self): def test_sanity_rgba(test_images):
for im in self.files_to_test: for im in test_images:
self.roundtrip(im.convert("RGBA")) roundtrip(im.convert("RGBA"))
def test_sanity_l(self): def test_sanity_l(test_images):
for im in self.files_to_test: for im in test_images:
self.roundtrip(im.convert("L")) roundtrip(im.convert("L"))
def test_sanity_p(self): def test_sanity_p(test_images):
for im in self.files_to_test: for im in test_images:
self.roundtrip(im.convert("P")) roundtrip(im.convert("P"))

View File

@ -4,68 +4,82 @@ from PIL import Image, ImageFilter
from .helper import PillowTestCase from .helper import PillowTestCase
class TestImageOpsUsm(PillowTestCase): @pytest.fixture
def setUp(self): def test_images():
super().setUp() ims = {
self.im = Image.open("Tests/images/hopper.ppm") "im": Image.open("Tests/images/hopper.ppm"),
self.addCleanup(self.im.close) "snakes": Image.open("Tests/images/color_snakes.png"),
self.snakes = Image.open("Tests/images/color_snakes.png") }
self.addCleanup(self.snakes.close) try:
yield ims
finally:
for im in ims.values():
im.close()
def test_filter_api(self):
def test_filter_api(test_images):
im = test_images["im"]
test_filter = ImageFilter.GaussianBlur(2.0) test_filter = ImageFilter.GaussianBlur(2.0)
i = self.im.filter(test_filter) i = im.filter(test_filter)
assert i.mode == "RGB" assert i.mode == "RGB"
assert i.size == (128, 128) assert i.size == (128, 128)
test_filter = ImageFilter.UnsharpMask(2.0, 125, 8) test_filter = ImageFilter.UnsharpMask(2.0, 125, 8)
i = self.im.filter(test_filter) i = im.filter(test_filter)
assert i.mode == "RGB" assert i.mode == "RGB"
assert i.size == (128, 128) assert i.size == (128, 128)
def test_usm_formats(self):
def test_usm_formats(test_images):
im = test_images["im"]
usm = ImageFilter.UnsharpMask usm = ImageFilter.UnsharpMask
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("1").filter(usm) im.convert("1").filter(usm)
self.im.convert("L").filter(usm) im.convert("L").filter(usm)
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("I").filter(usm) im.convert("I").filter(usm)
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("F").filter(usm) im.convert("F").filter(usm)
self.im.convert("RGB").filter(usm) im.convert("RGB").filter(usm)
self.im.convert("RGBA").filter(usm) im.convert("RGBA").filter(usm)
self.im.convert("CMYK").filter(usm) im.convert("CMYK").filter(usm)
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("YCbCr").filter(usm) im.convert("YCbCr").filter(usm)
def test_blur_formats(self):
def test_blur_formats(test_images):
im = test_images["im"]
blur = ImageFilter.GaussianBlur blur = ImageFilter.GaussianBlur
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("1").filter(blur) im.convert("1").filter(blur)
blur(self.im.convert("L")) blur(im.convert("L"))
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("I").filter(blur) im.convert("I").filter(blur)
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("F").filter(blur) im.convert("F").filter(blur)
self.im.convert("RGB").filter(blur) im.convert("RGB").filter(blur)
self.im.convert("RGBA").filter(blur) im.convert("RGBA").filter(blur)
self.im.convert("CMYK").filter(blur) im.convert("CMYK").filter(blur)
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.im.convert("YCbCr").filter(blur) im.convert("YCbCr").filter(blur)
def test_usm_accuracy(self):
src = self.snakes.convert("RGB") def test_usm_accuracy(test_images):
snakes = test_images["snakes"]
src = snakes.convert("RGB")
i = src.filter(ImageFilter.UnsharpMask(5, 1024, 0)) i = src.filter(ImageFilter.UnsharpMask(5, 1024, 0))
# Image should not be changed because it have only 0 and 255 levels. # Image should not be changed because it have only 0 and 255 levels.
assert i.tobytes() == src.tobytes() assert i.tobytes() == src.tobytes()
def test_blur_accuracy(self):
i = self.snakes.filter(ImageFilter.GaussianBlur(0.4)) def test_blur_accuracy(test_images):
snakes = test_images["snakes"]
i = snakes.filter(ImageFilter.GaussianBlur(0.4))
# These pixels surrounded with pixels with 255 intensity. # These pixels surrounded with pixels with 255 intensity.
# They must be very close to 255. # They must be very close to 255.
for x, y, c in [ for x, y, c in [

View File

@ -1,3 +1,4 @@
import pytest
from PIL import ImageQt from PIL import ImageQt
from .helper import PillowTestCase, hopper from .helper import PillowTestCase, hopper
@ -5,14 +6,14 @@ from .helper import PillowTestCase, hopper
if ImageQt.qt_is_installed: if ImageQt.qt_is_installed:
from PIL.ImageQt import qRgba from PIL.ImageQt import qRgba
def skip_if_qt_is_not_installed(_): def skip_if_qt_is_not_installed():
pass pass
else: else:
def skip_if_qt_is_not_installed(test_case): def skip_if_qt_is_not_installed():
test_case.skipTest("Qt bindings are not installed") return pytest.mark.skip(reason="Qt bindings are not installed")
class PillowQtTestCase: class PillowQtTestCase: