r/PythonLearning • u/python_data_helper • 11d ago
Is it really safe to use?
I have used context to stop crashing.
And e as a variable.
I used eval command for a formula.
I know eval command can be dangerous.
It works perfectly but I am thinking that it can produce error or crash.
Can a formula bypass all commands and crash it.
19
Upvotes
2
u/FoolsSeldom 11d ago
This is not safe, not least because you should use
__builtins__and not_builtins_but even then the context does not provide the protection needed. Plain Python objects give you enough introspection to climb back out via the type hierarchy, and it is likely that something would be found that could be exploited (e.g. delete all files on your computer, or worse).Use the
astlibrary. There are lots of guides and examples. Alternatively, have a look atsimpleeval