Test that eval blocks certain syntax to emulate pd.eval() behavior.
()
| 119 | |
| 120 | |
| 121 | def test_eval_restricted_syntax() -> None: |
| 122 | """Test that eval blocks certain syntax to emulate pd.eval() behavior.""" |
| 123 | ds = Dataset({"a": ("x", [1, 2, 3])}) |
| 124 | |
| 125 | # Private attribute access is not allowed (consistent with pd.eval) |
| 126 | with pytest.raises(ValueError, match="Access to private attributes is not allowed"): |
| 127 | ds.eval("a.__class__") |
| 128 | |
| 129 | with pytest.raises(ValueError, match="Access to private attributes is not allowed"): |
| 130 | ds.eval("a._private") |
| 131 | |
| 132 | # Lambda expressions are not allowed (pd.eval: "Only named functions are supported") |
| 133 | with pytest.raises(ValueError, match="Lambda expressions are not allowed"): |
| 134 | ds.eval("(lambda x: x + 1)(a)") |
| 135 | |
| 136 | # These builtins are not in the namespace |
| 137 | with pytest.raises(NameError): |
| 138 | ds.eval("__import__('os')") |
| 139 | |
| 140 | with pytest.raises(NameError): |
| 141 | ds.eval("open('file.txt')") |
| 142 | |
| 143 | |
| 144 | def test_eval_unsupported_statements() -> None: |