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.

33 lines
1000 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, NameDataError
  7. __all__ = ['mis_safe_eval']
  8. def mis_safe_eval(expr, locals_dict):
  9. """ Evaluate an expression using safe_eval
  10. Returns the evaluated value or DataError.
  11. Raises NameError if the evaluation depends on a variable that is not
  12. present in local_dict.
  13. """
  14. try:
  15. c = test_expr(expr, _SAFE_OPCODES, mode='eval')
  16. globals_dict = {'__builtins__': _BUILTINS}
  17. val = eval(c, globals_dict, locals_dict) # pylint: disable=eval-used
  18. except NameError:
  19. val = NameDataError('#NAME', traceback.format_exc())
  20. except ZeroDivisionError:
  21. val = DataError('#DIV/0', traceback.format_exc())
  22. except:
  23. val = DataError('#ERR', traceback.format_exc())
  24. return val