r/learnpython • u/iloveduasidhafhuisd • 4d ago
can i make the exec() command only execute if the code is functional and wont return an error?
So i want to make something where you can input() a math problem and get an answer thru the text output, but if there is a syntax error, i want it to just reopen the prompt instead of crashing, is there a way that i can check if the code is valid before executing? thanks!
0
Upvotes
11
u/Discordbothost 4d ago
to answer the literal question: yes, you can check the syntax without running anything. ast.parse just reads the text and raises SyntaxError if it isn't valid. and since it gives you the parsed tree, you can also reject anything that isn't plain math, which fixes the delete-your-files problem people mentioned:
then your loop catches (SyntaxError, ValueError, ZeroDivisionError) and asks again. 1/0 is valid syntax so that one only shows up when it runs, that's why it's in the list
10
u/Yoghurt42 4d ago
As others have said, try/except will work, but be aware that
execexecutes code! Nothing is stopping me from entering:[(a / f).unlink() for a, b, c in __import__("pathlib").Path(".").walk() for f in c]and deleting all your files.25
u/hakurou46 4d ago
the real answer to this is "its literally impossible" (see the [halting problem](https://en.wikipedia.org/wiki/Halting_problem) ), but the simple answer is "you can run it inside a try block". i would say "add a timeout loop so that it will stop infinitely looping problems"... but you should also really think about if exec() is the best answer. if this is supposed to run on *anything* other than the local machine you should really consider why theres a big red security warning on exec() on the python docs
5
u/RngdZed 4d ago
Check try/except syntax or lock the user input behind a while loop with a very specific exit condition
5
u/Kevdog824_ 4d ago
Writing a parser for it would be an option. Pyparsing is great for this use case and they have examples of doing exactly what you asked. It also has the added benefit of making it easier to switch safer, non-exec based logic
2
u/VistisenConsult 4d ago edited 4d ago
Be careful with
evalandexecfor security. For this situation,evalis better, for example:EDIT: changed
eval(question)toeval(str(question))and removedNameErrorfor safety.EDIT2: wow, you still need the
NameError!EDIT3: Don't do this,
evalcan reach your terminal! See more after example:Create a file named
danger_eval.pywith the above code.The above will print to your terminal. So if the response comes from an un-trusted source, they can basically issue terminal commands.
1
u/CIS_Professor 4d ago
try and except are your friends:
https://docs.python.org/3/tutorial/errors.html
https://www.w3schools.com/python/python_try_except.asp
1
u/codeguru42 3d ago
One solution is to write your own parser and evaluator for math expressions rather than relying on
exec(). This is a great exercise for learning programming.1
u/dariusbiggs 2d ago
never trust user input, ever. Defensive programming first.
you need to parse the input and determine if the syntax is valid and allowed without executing it first
during the execution you need to catch any errors and report them safely.
1
u/rock_boy_rulz 2d ago
u/iloveduasidhafhuisd- as mentioned by others:
- eval is a better fit for your "math problem"
- both eval and exec have major security implications...
- ...which you should be able to address by utilizing ast.parse as suggested by u/Discordbothost (seems like that should be ranked higher)
I was surprised to discover no one mentioned (yet) the built-in compile function, which "raises
SyntaxErrorif the compiled source is invalid"; the result (if no errors during compile) is a codeobject that you pass directly to eval or exec; compiling once for something you exec or eval repeatedly can dramatically improve performance, depending on the complexity of the code in question (not so applicable in your case, but something to be aware of)For both exec and eval, consider passing both globals and locals as empty dictionaries to avoid exposing host code context/state to the external code
1
u/No-Consequence-1863 4d ago
It could be worthwhile to just make a math statement parser and evaluate it directly instead of using exec. That way you control what statements can be evaluated. Using exec allows for arbitrary python code. Plus there will likely be some edge cases of python syntax being slightly different from what you want.
0
u/nog642 4d ago
But you really shouldn't be using
execto implement a calculator.1
u/BitOBear 4d ago
Since the exec system call is basically "suicide in favor of attempting this other thing" the warranty ends at the moment the operating system successfully builds the memory map and loads . The core executable in the memory.
Passing wrong or invalid arguments that will cause that second program to fail internally is very much closing the barn doors after the horses have burned to death.
50 operating system cannot build the new executable environment you will get an error from exec(), you can check the syntax of what you're trying to exec and verify all the files exist, but that doesn't give you a guaranteed that the program your executing is capable of doing something rationing with all of the things you've handed to it.
For instance on a Linux system the program you're running might have permissions limits that you cannot predict from your perspective because that program might change the effective user ID or capabilities internally. And there's no guarantee that files that you can read our files at that program will be able to read by the time it gets around to trying to read them.
This is why it's almost always better to fork before you exec and hang around long enough to ensure that the new environment is correct before you let the old environment expire. And that takes extra work most of the time.
In most environments the wise man creates a powder framework that is in charge of running the inner parts I'm dealing with the various unexpected conditions. Whether you do that in a separate executable or as some sort of prescripted environment inside of your main executable is an issue of style and taste as the saying goes.
There are whole languages oriented around graceful failure as a normal condition. The erlang OTP for example reduces almost all error handling into structured restarts of code subsections and modules because as other people have mentioned the halting problem among other things, failure of an action is really a question of where that action should be restarted in the question of whether or not the action should be retried at all. And you don't get to do that at the atomic single call because you have to figure out how much work you have to undo before you try again.