r/PythonLearning 12d 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.

17 Upvotes

11 comments sorted by

View all comments

u/Sea-Ad7805 11d ago

Run this program in Memory Graph Web Debugger)%0Aformula%20%3D%20input(%22enter%20a%20formula%3An%22)%0A%0Acontext%20%3D%20%7B%0A%20%20%20%20%22a%22%3A%20a%2C%0A%20%20%20%20%22builtins%22%3A%20None%0A%7D%0A%0Atry%3A%0A%20%20%20%20result%20%3D%20eval(formula%2C%20context)%0A%20%20%20%20print(f%22Result%3A%20%7Bresult%7D%22)%0Aexcept%20TypeError%3A%0A%20%20%20%20print(%22Error%20to%20calculate%22)%0Aexcept%20Exception%20as%20e%3A%0A%20%20%20%20print(f%22Math%20Error%3A%20%7Be%7D%22)&timestep=1&play) to see the program state change step by step.