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

Changed ImageMath.ops to be static #7721

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

radarhere
Copy link
Member

Resolves #7717

@hugovk
Copy link
Member

hugovk commented Jan 16, 2024

Looks good, thanks.

I don't expect we'll add new imagemath_* functions often, but if we do, is it worth adding a test to make sure we remember to add it to the dict?

@homm
Copy link
Member

homm commented Jan 16, 2024

If some function is not listed in the dict, it wouldn't work in eval, right? So any test will caught this

@hugovk
Copy link
Member

hugovk commented Jan 16, 2024

Ah, that's right, temporarily commenting one one:

diff --git a/src/PIL/ImageMath.py b/src/PIL/ImageMath.py
index a7652f237..47e552d3a 100644
--- a/src/PIL/ImageMath.py
+++ b/src/PIL/ImageMath.py
@@ -231,7 +231,7 @@ ops = {
     "notequal": imagemath_notequal,
     "min": imagemath_min,
     "max": imagemath_max,
-    "convert": imagemath_convert,
+    # "convert": imagemath_convert,
 }

Fails like this:

Tests/test_imagemath.py ........F..................                                                                              [100%]

=============================================================== FAILURES ===============================================================
_____________________________________________________________ test_convert _____________________________________________________________

    def test_convert():
>       assert pixel(ImageMath.eval("convert(A+B, 'L')", images)) == "L 3"

Tests/test_imagemath.py:84:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
src/PIL/ImageMath.py:276: in eval
    scan(compiled_code)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

code = <code object <module> at 0x107002f70, file "<string>", line 1>

    def scan(code: CodeType) -> None:
        for const in code.co_consts:
            if type(const) is type(compiled_code):
                scan(const)

        for name in code.co_names:
            if name not in args and name != "abs":
                msg = f"'{name}' not allowed"
>               raise ValueError(msg)
E               ValueError: 'convert' not allowed

src/PIL/ImageMath.py:274: ValueError
======================================================= short test summary info ========================================================
FAILED Tests/test_imagemath.py::test_convert - ValueError: 'convert' not allowed
===================================================== 1 failed, 26 passed in 1.80s =====================================================

@radarhere radarhere merged commit 6fd85cf into python-pillow:main Jan 16, 2024
56 checks passed
@radarhere radarhere deleted the imagemath_ops branch January 16, 2024 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image.open: dictionary changed size during iteration (race condition)
3 participants