mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-07 22:04:46 +03:00
Only compute ops dict when needed
This commit is contained in:
parent
fd5df78669
commit
69ada9994a
|
@ -17,6 +17,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import builtins
|
import builtins
|
||||||
|
from functools import cache
|
||||||
from types import CodeType
|
from types import CodeType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
@ -224,10 +225,13 @@ def imagemath_convert(self: _Operand, mode: str) -> _Operand:
|
||||||
return _Operand(self.im.convert(mode))
|
return _Operand(self.im.convert(mode))
|
||||||
|
|
||||||
|
|
||||||
ops = {}
|
@cache
|
||||||
for k, v in list(globals().items()):
|
def _get_ops() -> dict[str, Any]:
|
||||||
|
ops = {}
|
||||||
|
for k, v in list(globals().items()):
|
||||||
if k[:10] == "imagemath_":
|
if k[:10] == "imagemath_":
|
||||||
ops[k[10:]] = v
|
ops[k[10:]] = v
|
||||||
|
return ops
|
||||||
|
|
||||||
|
|
||||||
def eval(expression: str, _dict: dict[str, Any] = {}, **kw: Any) -> Any:
|
def eval(expression: str, _dict: dict[str, Any] = {}, **kw: Any) -> Any:
|
||||||
|
@ -244,7 +248,7 @@ def eval(expression: str, _dict: dict[str, Any] = {}, **kw: Any) -> Any:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# build execution namespace
|
# build execution namespace
|
||||||
args = ops.copy()
|
args = _get_ops().copy()
|
||||||
for k in list(_dict.keys()) + list(kw.keys()):
|
for k in list(_dict.keys()) + list(kw.keys()):
|
||||||
if "__" in k or hasattr(builtins, k):
|
if "__" in k or hasattr(builtins, k):
|
||||||
msg = f"'{k}' not allowed"
|
msg = f"'{k}' not allowed"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user