import io import os import shutil import sys import tempfile import pytest import PIL from PIL import Image, ImageDraw, ImagePalette, ImageShow, UnidentifiedImageError from .helper import ( assert_image_equal, assert_image_equal_tofile, assert_image_similar_tofile, assert_not_all_same, hopper, is_win32, skip_unless_feature, ) class TestImage: def test_image_modes_success(self): for mode in [ "1", "P", "PA", "L", "LA", "La", "F", "I", "I;16", "I;16L", "I;16B", "I;16N", "RGB", "RGBX", "RGBA", "RGBa", "CMYK", "YCbCr", "LAB", "HSV", ]: Image.new(mode, (1, 1)) def test_image_modes_fail(self): for mode in [ "", "bad", "very very long", "BGR;15", "BGR;16", "BGR;24", "BGR;32", ]: with pytest.raises(ValueError) as e: Image.new(mode, (1, 1)) assert str(e.value) == "unrecognized image mode" def test_exception_inheritance(self): assert issubclass(UnidentifiedImageError, OSError) def test_sanity(self): im = Image.new("L", (100, 100)) assert repr(im)[:45] == "= 7 with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION < "9.9.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION <= "9.9.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION != "7.0.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION >= "7.0.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION > "7.0.0" @pytest.mark.parametrize( "path", [ "fli_overrun.bin", "sgi_overrun.bin", "sgi_overrun_expandrow.bin", "sgi_overrun_expandrow2.bin", "pcx_overrun.bin", "pcx_overrun2.bin", "ossfuzz-4836216264589312.pcx", "01r_00.pcx", ], ) def test_overrun(self, path): """For overrun completeness, test as: valgrind pytest -qq Tests/test_image.py::TestImage::test_overrun | grep decode.c """ with Image.open(os.path.join("Tests/images", path)) as im: try: im.load() assert False except OSError as e: buffer_overrun = str(e) == "buffer overrun when reading image file" truncated = "image file is truncated" in str(e) assert buffer_overrun or truncated def test_fli_overrun2(self): with Image.open("Tests/images/fli_overrun2.bin") as im: try: im.seek(1) assert False except OSError as e: assert str(e) == "buffer overrun when reading image file" def test_show_deprecation(self, monkeypatch): monkeypatch.setattr(Image, "_show", lambda *args, **kwargs: None) im = Image.new("RGB", (50, 50), "white") with pytest.warns(None) as raised: im.show() assert not raised with pytest.warns(DeprecationWarning): im.show(command="mock") class MockEncoder: pass def mock_encode(*args): encoder = MockEncoder() encoder.args = args return encoder class TestRegistry: def test_encode_registry(self): Image.register_encoder("MOCK", mock_encode) assert "MOCK" in Image.ENCODERS enc = Image._getencoder("RGB", "MOCK", ("args",), extra=("extra",)) assert isinstance(enc, MockEncoder) assert enc.args == ("RGB", "args", "extra") def test_encode_registry_fail(self): with pytest.raises(OSError): Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",))