Beyond Literals: What `ast.literal_eval` Won't Parse

`ast.literal_eval` explicitly rejects any input that contains non-literal Python expressions, function calls, imports, or attribute access, which would be executable by `eval()`. For instance, expressions like "1 + 1", function calls like "__import__('os').system('rm -rf /')", or `dict()` will raise a `ValueError` or `SyntaxError` because they go beyond simple data structures.

Sources

Open the full topic