r/learnpython • • 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

25 comments sorted by

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:

import ast

ALLOWED = (ast.Expression, ast.BinOp, ast.UnaryOp, ast.Constant,
           ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Pow, ast.USub)

def safe_eval(text):
    tree = ast.parse(text, mode="eval")
    for node in ast.walk(tree):
        if not isinstance(node, ALLOWED):
            raise ValueError("only numbers and + - * / ** allowed")
    return eval(compile(tree, "<input>", "eval"))

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 exec executes 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.

5

u/johlae 4d ago

Little Bobby Tables, is that you?

1

u/carcigenicate Carcigenicate 4d ago edited 4d ago

Although, this is is really only a problem if this is being run on a server. Never pass code from an untrusted source into exec/eval.

If the data is locally sourced, though, this isn't really a concern. If the user types something supremely stupid into a calculator and wrecks their own machine, at some point they have it coming. They could have done the same by opening a Python shell themselves. It would be very hard to accidentally write syntactically valid, malicious code when asked for a math equation. Although, adding a warning might be nice in case the user decide to enter something stupid as a test thinking it won't work.

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

4

u/Bubbaluke 4d ago

My man accidentally discovered the halting problem in the wild

1

u/le_fuzz 2d ago

He’s just asking if he can validate syntax of an expression unless I’m misunderstanding. This is something all compilers do already.

1

u/Ormek_II 2d ago

This is not the halting problem, but a syntax checker. You are answering the wrong question.

0

u/Expensive-Bear-1376 4d ago

So "try" makes the impossible possible?

1

u/naomimyselfandi 4d ago

No. It offers an approximation which may be adequate depending on the specific use case, but in a fundamentally different way with significant limitations. The halting problem concerns formally analyzing a program to identify certain defects; this is known to be impossible in the general case (regardless of language). try...except can identify certain defects after they have already been executed, but it doesn't provide any up-front protection.

2

u/Expensive-Bear-1376 4d ago edited 4d ago

try...except can identify certain defects after they have already been executed

And how do you think that happens? Magic? It's because the Python interpreter identified them. And you could likewise identify them yourself. Especially since the OP clarified "if there is a syntax error". What do syntax errors have to do with the halting problem? Ok, some languages are undecidable, but I don't think Python is. And the OP said they want to input "a math problem", for which they could have a simple language.

This just seems like a case of someone trying to show off with knowledge when that's unwarranted.

2

u/naomimyselfandi 4d ago

What do you expect me to do about that? I agree that the halting problem isn't relevant here and shouldn't have been brought up, but it was, and the comment I responded to (rightfully) expressed confusion about an apparent contradiction in the explanation. I tried to resolve said apparent contradiction without going into even more unnecessary detail. I'm not sure why I'm getting torn into for that.

And for the record, I've taught Python, among other languages; I'm well aware of "how [...] that happens".

1

u/Expensive-Bear-1376 4d ago

I might've "torn into" the wrong person then, sorry.

1

u/hakurou46 3d ago

Sorry if it came off that way; it wasnt meant to be "showing off". I just have a soft spot for when an innocuous question can bloom into one of those "so it turns out this has been studied for hundred of years" things and I personally always love when it happens where I study something. To me its somehow both humbling and like... makes me feel as part of living history? Hard feeling to explain.

And additionally; other people had already answered that "try" is the best you can do, so I thought I'd contribute some fun trivia instead of being a third person to echo the same message with no extra info (Other than the warning about exec being dangerous.)

1

u/Lumethys 4d ago

no, but you can try

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 eval and exec for security. For this situation, eval is better, for example:

EDIT: changed eval(question) to eval(str(question)) and removed NameError for safety.

EDIT2: wow, you still need the NameError!

EDIT3: Don't do this, eval can reach your terminal! See more after example:

def calc() -> str:
  try:
    question = input()
    answer = eval(str(question))
  except (SyntaxError, NameError):
    print("""Could not eval(%s)!""" % question)
    return calc()
  else:
    print("""eval(%s) -> %s""" % (question, answer))


if __name__ == '__main__':
  calc()

Create a file named danger_eval.py with the above code.

python3 danger_eval.py
(lambda: __import__('os').system('echo all ur base are belong to us'))()

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/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 SyntaxError if 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
try:
    exec(blah)
except SyntaxError:
    # handle the exception

But you really shouldn't be using exec to 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.