0

So, we had instance in the past where code were broken in IOT devices because of syntax errors.

While there is exception handling in the code. I wanted to create a script to check and make sure that the codes compiles and run without syntax error, else the script replace the broken code by an earlier version.

I tried this

from delta_script import get_update
def test_function():
    try:
        get_update()
    except SyntaxError as syntaxError:
        replace_script("utility.py", syntaxError)
    except Exception as ignored:
        pass

However the problem it when it hit a SyntaxError, it just throw it on the screen and replace_script because the exception happens on delta_script.py from which get_update() was imported.

So what's the solution in this case?

I have also another function

def compile():
    try:
        for file in compile_list:
            py_compile.compile(file)
    except Exception as exception:
        script_heal(file, exception)

however in this one, it never report any exception, because I go and introduce syntaxError and the code still compile without reporting an error

Any one could help me figure out a better way to solve those two problems? thanks,

6
  • 1
    Why not fix the SyntaxError instead of catching it? Commented Mar 12, 2020 at 5:20
  • We always do, however if any unfortunate situation happens, we would like to be able to recover from it. Commented Mar 12, 2020 at 5:22
  • SyntaxError is a problem in code(except you run code string which is input by user). So usually it is not to be handled, it should be fixed. Commented Mar 12, 2020 at 5:22
  • You can catch runtime errors only. A syntax error is no runtime error. Commented Mar 12, 2020 at 5:22
  • To be fairly honnest, if your deployment process cannot catch a SyntaxError, you have more urgent problems than that error. A syntax error happens before run-time by definition. It's literally impossible to miss if you just do python your_script before deployment. Or do you import dynamicly generated scripts for some reason? Commented Mar 12, 2020 at 5:25

1 Answer 1

1

SyntaxErrors occur at compile time, not run time, so you generally can't catch them. There are exceptions, involving run time compilation using eval/exec, but in general, except SyntaxError: is nonsensical; something goes wrong compiling the code before it can run the code that sets up the try/except to catch the error.

The solution is to not write syntactically invalid code, or if you must write it (e.g. to allow newer Python syntax only when supported) to evaluate strings of said code dynamically with eval (often wrapping compile if you need something more complicated than a single expression) or exec.

Sign up to request clarification or add additional context in comments.

2 Comments

can you please give an example on how this can be done? the wrapping?
@DavidShapero: Examples exist already. Here's one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.