Skip to content

Commit

Permalink
Merge pull request #7721 from radarhere/imagemath_ops
Browse files Browse the repository at this point in the history
Changed ImageMath.ops to be static
  • Loading branch information
radarhere committed Jan 16, 2024
2 parents 1d7ff59 + e2aa0fd commit 6fd85cf
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,15 @@ def imagemath_convert(self: _Operand, mode: str) -> _Operand:
return _Operand(self.im.convert(mode))


ops = {}
for k, v in list(globals().items()):
if k[:10] == "imagemath_":
ops[k[10:]] = v
ops = {
"int": imagemath_int,
"float": imagemath_float,
"equal": imagemath_equal,
"notequal": imagemath_notequal,
"min": imagemath_min,
"max": imagemath_max,
"convert": imagemath_convert,
}


def eval(expression: str, _dict: dict[str, Any] = {}, **kw: Any) -> Any:
Expand All @@ -244,7 +249,7 @@ def eval(expression: str, _dict: dict[str, Any] = {}, **kw: Any) -> Any:
"""

# build execution namespace
args = ops.copy()
args: dict[str, Any] = ops.copy()
for k in list(_dict.keys()) + list(kw.keys()):
if "__" in k or hasattr(builtins, k):
msg = f"'{k}' not allowed"
Expand Down

0 comments on commit 6fd85cf

Please sign in to comment.