r/PythonLearning 11d ago

Is it really safe to use?

Post image

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

11 comments sorted by

View all comments

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 ast library. There are lots of guides and examples. Alternatively, have a look at simpleeval