Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed incorrect operator in ImageCms error #6370

Merged
merged 7 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 31 additions & 9 deletions Tests/test_imagecms.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,24 @@ def test_exceptions():
psRGB = ImageCms.createProfile("sRGB")
pLab = ImageCms.createProfile("LAB")
t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="mode mismatch"):
t.apply_in_place(hopper("RGBA"))

# the procedural pyCMS API uses PyCMSError for all sorts of errors
with hopper() as im:
with pytest.raises(ImageCms.PyCMSError):
with pytest.raises(ImageCms.PyCMSError, match="cannot open profile file"):
ImageCms.profileToProfile(im, "foo", "bar")
with pytest.raises(ImageCms.PyCMSError):

with pytest.raises(ImageCms.PyCMSError, match="cannot open profile file"):
ImageCms.buildTransform("foo", "bar", "RGB", "RGB")
with pytest.raises(ImageCms.PyCMSError):

with pytest.raises(ImageCms.PyCMSError, match="Invalid type for Profile"):
ImageCms.getProfileName(None)
skip_missing()
with pytest.raises(ImageCms.PyCMSError):

# Python <= 3.9: "an integer is required (got type NoneType)"
# Python > 3.9: "'NoneType' object cannot be interpreted as an integer"
with pytest.raises(ImageCms.PyCMSError, match="integer"):
ImageCms.isIntentSupported(SRGB, None, None)


Expand All @@ -201,15 +206,32 @@ def test_lab_color_profile():


def test_unsupported_color_space():
with pytest.raises(ImageCms.PyCMSError):
with pytest.raises(
ImageCms.PyCMSError,
match=re.escape(
"Color space not supported for on-the-fly profile creation (unsupported)"
),
):
ImageCms.createProfile("unsupported")


def test_invalid_color_temperature():
with pytest.raises(ImageCms.PyCMSError):
with pytest.raises(
ImageCms.PyCMSError,
match='Color temperature must be numeric, "invalid" not valid',
):
ImageCms.createProfile("LAB", "invalid")


@pytest.mark.parametrize("flag", ("my string", -1))
def test_invalid_flag(flag):
with hopper() as im:
with pytest.raises(
ImageCms.PyCMSError, match="flags must be an integer between 0 and "
):
ImageCms.profileToProfile(im, "foo", "bar", flags=flag)


def test_simple_lab():
i = Image.new("RGB", (10, 10), (128, 128, 128))

Expand Down Expand Up @@ -461,9 +483,9 @@ def test_profile_typesafety():
prepatch, these would segfault, postpatch they should emit a typeerror
"""

with pytest.raises(TypeError):
with pytest.raises(TypeError, match="Invalid type for Profile"):
ImageCms.ImageCmsProfile(0).tobytes()
with pytest.raises(TypeError):
with pytest.raises(TypeError, match="Invalid type for Profile"):
ImageCms.ImageCmsProfile(1).tobytes()


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def profileToProfile(
raise PyCMSError("renderingIntent must be an integer between 0 and 3")

if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
raise PyCMSError("flags must be an integer between 0 and %s" + _MAX_FLAG)
raise PyCMSError(f"flags must be an integer between 0 and {_MAX_FLAG}")

try:
if not isinstance(inputProfile, ImageCmsProfile):
Expand Down