test_utils.py 1015 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import pytest
  2. from joblib._utils import eval_expr
  3. @pytest.mark.parametrize(
  4. "expr",
  5. [
  6. "exec('import os')",
  7. "print(1)",
  8. "import os",
  9. "1+1; import os",
  10. "1^1",
  11. "' ' * 10**10",
  12. "9. ** 10000.",
  13. ],
  14. )
  15. def test_eval_expr_invalid(expr):
  16. with pytest.raises(ValueError, match="is not a valid or supported arithmetic"):
  17. eval_expr(expr)
  18. def test_eval_expr_too_long():
  19. expr = "1" + "+1" * 50
  20. with pytest.raises(ValueError, match="is too long"):
  21. eval_expr(expr)
  22. @pytest.mark.parametrize("expr", ["1e7", "10**7", "9**9**9"])
  23. def test_eval_expr_too_large_literal(expr):
  24. with pytest.raises(ValueError, match="Numeric literal .* is too large"):
  25. eval_expr(expr)
  26. @pytest.mark.parametrize(
  27. "expr, result",
  28. [
  29. ("2*6", 12),
  30. ("2**6", 64),
  31. ("1 + 2*3**(4) / (6 + -7)", -161.0),
  32. ("(20 // 3) % 5", 1),
  33. ],
  34. )
  35. def test_eval_expr_valid(expr, result):
  36. assert eval_expr(expr) == result