Skip to content

Commit

Permalink
Allow ops
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 29, 2023
1 parent 45c726f commit 0ca3c33
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Tests/test_imagemath.py
Expand Up @@ -64,6 +64,11 @@ def test_prevent_exec(expression):
ImageMath.eval(expression)


def test_prevent_double_underscores():
with pytest.raises(ValueError):
ImageMath.eval("1", {"__": None})


def test_logical():
assert pixel(ImageMath.eval("not A", images)) == 0
assert pixel(ImageMath.eval("A and B", images)) == "L 2"
Expand Down
9 changes: 5 additions & 4 deletions src/PIL/ImageMath.py
Expand Up @@ -234,13 +234,14 @@ def eval(expression, _dict={}, **kw):

# build execution namespace
args = ops.copy()
args.update(_dict)
args.update(kw)
for k, v in args.items():
if '__' in k or hasattr(__builtins__, k):
for k in list(_dict.keys()) + list(kw.keys()):
if "__" in k or hasattr(__builtins__, k):
msg = f"'{k}' not allowed"
raise ValueError(msg)

args.update(_dict)
args.update(kw)
for k, v in args.items():
if hasattr(v, "im"):
args[k] = _Operand(v)

Expand Down

0 comments on commit 0ca3c33

Please sign in to comment.