Fix up pytest.warns lambda: uses

This commit is contained in:
Aarni Koskela 2023-02-23 15:30:38 +02:00
parent 5c8a9165ab
commit 43128ce716
16 changed files with 52 additions and 64 deletions

View File

@ -177,13 +177,14 @@ class TestEnvVars:
Image._apply_env_variables({"PILLOW_BLOCK_SIZE": "2m"}) Image._apply_env_variables({"PILLOW_BLOCK_SIZE": "2m"})
assert Image.core.get_block_size() == 2 * 1024 * 1024 assert Image.core.get_block_size() == 2 * 1024 * 1024
def test_warnings(self): @pytest.mark.parametrize(
pytest.warns( "vars",
UserWarning, Image._apply_env_variables, {"PILLOW_ALIGNMENT": "15"} [
) {"PILLOW_ALIGNMENT": "15"},
pytest.warns( {"PILLOW_BLOCK_SIZE": "1024"},
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCK_SIZE": "1024"} {"PILLOW_BLOCKS_MAX": "wat"},
) ],
pytest.warns( )
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCKS_MAX": "wat"} def test_warnings(self, vars):
) with pytest.warns(UserWarning):
Image._apply_env_variables(vars)

View File

@ -36,12 +36,10 @@ class TestDecompressionBomb:
Image.MAX_IMAGE_PIXELS = 128 * 128 - 1 Image.MAX_IMAGE_PIXELS = 128 * 128 - 1
assert Image.MAX_IMAGE_PIXELS == 128 * 128 - 1 assert Image.MAX_IMAGE_PIXELS == 128 * 128 - 1
def open(): with pytest.warns(Image.DecompressionBombWarning):
with Image.open(TEST_FILE): with Image.open(TEST_FILE):
pass pass
pytest.warns(Image.DecompressionBombWarning, open)
def test_exception(self): def test_exception(self):
# Set limit to trigger exception on the test file # Set limit to trigger exception on the test file
Image.MAX_IMAGE_PIXELS = 64 * 128 - 1 Image.MAX_IMAGE_PIXELS = 64 * 128 - 1
@ -87,7 +85,8 @@ class TestDecompressionCrop:
# same decompression bomb warnings on them. # same decompression bomb warnings on them.
with hopper() as src: with hopper() as src:
box = (0, 0, src.width * 2, src.height * 2) box = (0, 0, src.width * 2, src.height * 2)
pytest.warns(Image.DecompressionBombWarning, src.crop, box) with pytest.warns(Image.DecompressionBombWarning):
src.crop(box)
def test_crop_decompression_checks(self): def test_crop_decompression_checks(self):
im = Image.new("RGB", (100, 100)) im = Image.new("RGB", (100, 100))
@ -95,7 +94,8 @@ class TestDecompressionCrop:
for value in ((-9999, -9999, -9990, -9990), (-999, -999, -990, -990)): for value in ((-9999, -9999, -9990, -9990), (-999, -999, -990, -990)):
assert im.crop(value).size == (9, 9) assert im.crop(value).size == (9, 9)
pytest.warns(Image.DecompressionBombWarning, im.crop, (-160, -160, 99, 99)) with pytest.warns(Image.DecompressionBombWarning):
im.crop((-160, -160, 99, 99))
with pytest.raises(Image.DecompressionBombError): with pytest.raises(Image.DecompressionBombError):
im.crop((-99909, -99990, 99999, 99999)) im.crop((-99909, -99990, 99999, 99999))

View File

@ -263,12 +263,9 @@ def test_apng_chunk_errors():
with Image.open("Tests/images/apng/chunk_no_actl.png") as im: with Image.open("Tests/images/apng/chunk_no_actl.png") as im:
assert not im.is_animated assert not im.is_animated
def open(): with pytest.warns(UserWarning):
with Image.open("Tests/images/apng/chunk_multi_actl.png") as im: with Image.open("Tests/images/apng/chunk_multi_actl.png") as im:
im.load() im.load()
assert not im.is_animated
pytest.warns(UserWarning, open)
with Image.open("Tests/images/apng/chunk_actl_after_idat.png") as im: with Image.open("Tests/images/apng/chunk_actl_after_idat.png") as im:
assert not im.is_animated assert not im.is_animated
@ -287,21 +284,17 @@ def test_apng_chunk_errors():
def test_apng_syntax_errors(): def test_apng_syntax_errors():
def open_frames_zero(): with pytest.warns(UserWarning):
with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im: with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im:
assert not im.is_animated assert not im.is_animated
with pytest.raises(OSError): with pytest.raises(OSError):
im.load() im.load()
pytest.warns(UserWarning, open_frames_zero) with pytest.warns(UserWarning):
def open_frames_zero_default():
with Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") as im: with Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") as im:
assert not im.is_animated assert not im.is_animated
im.load() im.load()
pytest.warns(UserWarning, open_frames_zero_default)
# we can handle this case gracefully # we can handle this case gracefully
exception = None exception = None
with Image.open("Tests/images/apng/syntax_num_frames_low.png") as im: with Image.open("Tests/images/apng/syntax_num_frames_low.png") as im:
@ -316,13 +309,11 @@ def test_apng_syntax_errors():
im.seek(im.n_frames - 1) im.seek(im.n_frames - 1)
im.load() im.load()
def open(): with pytest.warns(UserWarning):
with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im: with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im:
assert not im.is_animated assert not im.is_animated
im.load() im.load()
pytest.warns(UserWarning, open)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"test_file", "test_file",

View File

@ -24,11 +24,10 @@ def test_sanity():
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(): def test_closed_file():

View File

@ -32,11 +32,10 @@ def test_sanity():
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
im = Image.open(static_test_file) im = Image.open(static_test_file)
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(): def test_closed_file():

View File

@ -32,11 +32,10 @@ def test_sanity():
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
im = Image.open(TEST_GIF) im = Image.open(TEST_GIF)
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(): def test_closed_file():
@ -1087,7 +1086,8 @@ def test_rgb_transparency(tmp_path):
im = Image.new("RGB", (1, 1)) im = Image.new("RGB", (1, 1))
im.info["transparency"] = b"" im.info["transparency"] = b""
ims = [Image.new("RGB", (1, 1))] ims = [Image.new("RGB", (1, 1))]
pytest.warns(UserWarning, im.save, out, save_all=True, append_images=ims) with pytest.warns(UserWarning):
im.save(out, save_all=True, append_images=ims)
with Image.open(out) as reloaded: with Image.open(out) as reloaded:
assert "transparency" not in reloaded.info assert "transparency" not in reloaded.info

View File

@ -212,12 +212,10 @@ def test_save_append_images(tmp_path):
def test_unexpected_size(): def test_unexpected_size():
# This image has been manually hexedited to state that it is 16x32 # This image has been manually hexedited to state that it is 16x32
# while the image within is still 16x16 # while the image within is still 16x16
def open(): with pytest.warns(UserWarning):
with Image.open("Tests/images/hopper_unexpected.ico") as im: with Image.open("Tests/images/hopper_unexpected.ico") as im:
assert im.size == (16, 16) assert im.size == (16, 16)
pytest.warns(UserWarning, open)
def test_draw_reloaded(tmp_path): def test_draw_reloaded(tmp_path):
with Image.open(TEST_ICO_FILE) as im: with Image.open(TEST_ICO_FILE) as im:

View File

@ -28,11 +28,10 @@ def test_name_limit(tmp_path):
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
im = Image.open(TEST_IM) im = Image.open(TEST_IM)
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(): def test_closed_file():

View File

@ -38,11 +38,10 @@ def test_sanity(test_file):
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
im = Image.open(test_files[0]) im = Image.open(test_files[0])
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(): def test_closed_file():

View File

@ -23,11 +23,10 @@ def test_sanity():
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
im = Image.open(test_file) im = Image.open(test_file)
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(): def test_closed_file():

View File

@ -21,11 +21,10 @@ def test_sanity():
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(): def test_closed_file():

View File

@ -29,11 +29,9 @@ def test_sanity(codec, test_path, format):
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(): def test_unclosed_file():
def open(): with pytest.warns(ResourceWarning):
TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
pytest.warns(ResourceWarning, open)
def test_close(): def test_close():
with warnings.catch_warnings(): with warnings.catch_warnings():

View File

@ -163,7 +163,9 @@ def test_save_id_section(tmp_path):
# Save with custom id section greater than 255 characters # Save with custom id section greater than 255 characters
id_section = b"Test content" * 25 id_section = b"Test content" * 25
pytest.warns(UserWarning, lambda: im.save(out, id_section=id_section)) with pytest.warns(UserWarning):
im.save(out, id_section=id_section)
with Image.open(out) as test_im: with Image.open(out) as test_im:
assert test_im.info["id_section"] == id_section[:255] assert test_im.info["id_section"] == id_section[:255]

View File

@ -57,11 +57,10 @@ class TestFileTiff:
@pytest.mark.skipif(is_pypy(), reason="Requires CPython") @pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file(self): def test_unclosed_file(self):
def open(): with pytest.warns(ResourceWarning):
im = Image.open("Tests/images/multipage.tiff") im = Image.open("Tests/images/multipage.tiff")
im.load() im.load()
del im
pytest.warns(ResourceWarning, open)
def test_closed_file(self): def test_closed_file(self):
with warnings.catch_warnings(): with warnings.catch_warnings():
@ -231,7 +230,8 @@ class TestFileTiff:
def test_bad_exif(self): def test_bad_exif(self):
with Image.open("Tests/images/hopper_bad_exif.jpg") as i: with Image.open("Tests/images/hopper_bad_exif.jpg") as i:
# Should not raise struct.error. # Should not raise struct.error.
pytest.warns(UserWarning, i._getexif) with pytest.warns(UserWarning):
i._getexif()
def test_save_rgba(self, tmp_path): def test_save_rgba(self, tmp_path):
im = hopper("RGBA") im = hopper("RGBA")

View File

@ -252,7 +252,8 @@ def test_empty_metadata():
head = f.read(8) head = f.read(8)
info = TiffImagePlugin.ImageFileDirectory(head) info = TiffImagePlugin.ImageFileDirectory(head)
# Should not raise struct.error. # Should not raise struct.error.
pytest.warns(UserWarning, info.load, f) with pytest.warns(UserWarning):
info.load(f)
def test_iccprofile(tmp_path): def test_iccprofile(tmp_path):
@ -422,7 +423,8 @@ def test_too_many_entries():
ifd.tagtype[277] = TiffTags.SHORT ifd.tagtype[277] = TiffTags.SHORT
# Should not raise ValueError. # Should not raise ValueError.
pytest.warns(UserWarning, lambda: ifd[277]) with pytest.warns(UserWarning):
_ = ifd[277]
def test_tag_group_data(): def test_tag_group_data():

View File

@ -29,7 +29,9 @@ class TestUnsupportedWebp:
WebPImagePlugin.SUPPORTED = False WebPImagePlugin.SUPPORTED = False
file_path = "Tests/images/hopper.webp" file_path = "Tests/images/hopper.webp"
pytest.warns(UserWarning, lambda: pytest.raises(OSError, Image.open, file_path)) with pytest.warns(UserWarning):
with pytest.raises(OSError):
Image.open(file_path)
if HAVE_WEBP: if HAVE_WEBP:
WebPImagePlugin.SUPPORTED = True WebPImagePlugin.SUPPORTED = True