Skip to content

Allowlist/blocklist for operator types as well #170

@danthedeckie

Description

@danthedeckie

as noted in:

>>> import simpleeval
>>> class Foo:
...     def __add__(self, other):
...         raise Exception('alas!')
...
>>> simpleeval.simple_eval('foo + 21', names={"foo": Foo()})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/daniel/src/simpleeval/simpleeval.py", line 768, in simple_eval
    return s.eval(expr)
           ^^^^^^^^^^^^
  File "/Users/daniel/src/simpleeval/simpleeval.py", line 433, in eval
    return self._eval(previously_parsed or self.parse(expr))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/daniel/src/simpleeval/simpleeval.py", line 445, in _eval
    return handler(node)
           ^^^^^^^^^^^^^
  File "/Users/daniel/src/simpleeval/simpleeval.py", line 448, in _eval_expr
    return self._eval(node.value)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/daniel/src/simpleeval/simpleeval.py", line 445, in _eval
    return handler(node)
           ^^^^^^^^^^^^^
  File "/Users/daniel/src/simpleeval/simpleeval.py", line 502, in _eval_binop
    return operator(self._eval(node.left), self._eval(node.right))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/daniel/src/simpleeval/simpleeval.py", line 272, in safe_add
    return a + b
           ~~^~~
  File "<stdin>", line 3, in __add__
Exception: alas!

so should we have a way to only run the various __add__ etc operators on classes that have been explicitly allowed to?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions