Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import annotations

import builtins
from functools import lru_cache
from types import CodeType
from typing import Any

Expand Down Expand Up @@ -224,10 +225,9 @@ 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
@lru_cache
def _get_ops() -> dict[str, Any]:
return {k[10:]: v for k, v in globals().items() if k[:10] == "imagemath_"}


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

# build execution namespace
args = ops.copy()
args = _get_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