You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
939 B

  1. # -*- coding: utf-8 -*-
  2. # © 2016 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import traceback
  5. from openerp.tools.safe_eval import test_expr, _SAFE_OPCODES, _BUILTINS
  6. from .data_error import DataError
  7. __all__ = ['mis_safe_eval']
  8. std_eval = eval
  9. def mis_safe_eval(expr, locals_dict):
  10. """ Evaluate an expression using safe_eval
  11. Returns the evaluated value or DataError.
  12. Raises NameError if the evaluation depends on a variable that is not
  13. present in local_dict.
  14. """
  15. try:
  16. c = test_expr(expr, _SAFE_OPCODES, mode='eval')
  17. globals_dict = {'__builtins__': _BUILTINS}
  18. val = std_eval(c, globals_dict, locals_dict) # noqa
  19. except NameError:
  20. raise
  21. except ZeroDivisionError:
  22. val = DataError('#DIV/0', traceback.format_exc())
  23. except:
  24. val = DataError('#ERR', traceback.format_exc())
  25. return val