Hi,

I'm trying to make a frontend for the compile module in python.

Everything works just right, except when the file has mistakes in the code that lead to a syntax error.

It does raise the invalid syntax error in the terminal, but instead of executing the block inside the except statement, it goes on to the else block. look:

try:
        file = t1.get()
        py_compile.compile(file)
    except IOError:
        tkinter.messagebox.showerror("Error", "File not found")
    except UnicodeDecodeError:
        tkinter.messagebox.showerror("Error", "This is not python code")
    except SyntaxError:
        tkinter.messagebox.showerror("Error", "There is a syntax error in the code")
    else:
        time.sleep(2)
        tkinter.messagebox.showinfo("¡Successfully compiled!", "The file \n(%s)\n was compiled successfully" %file)

Recommended Answers

Here is attached my test. I did not find any problem.

Jump to Post

All 8 Replies

Re: How do I catch a syntax error with except? 80 80

You dont need a new except for every excption you whant to catch.
Could look something like this.

try:   
    file = t1.get()
    py_compile.compile(file)
except (IOError,UnicodeDecodeError,SyntaxError):
    #Give message
else:
    print "That went well!"
finally:
    print "Cleaning up."
Re: How do I catch a syntax error with except? 80 80

What was the error that you saw in the terminal? If an error other than IOError, UnicodeDecodeError or SyntaxError occurs (if that is possible?) then I think (haven't tested it) control would go to your else block which reports success.

Re: How do I catch a syntax error with except? 80 80

Yes I know that, d5e5, but the error is clearly a SyntaxError (I think) because I made a test file with a broken print statement: print x (this is python 3.1).

the file:

x = "hello world"
y = "hello"
print x

The error in the terminal is this:

File "/home/mat/bin/frontends/bad.py", line 3
    print x

File "/home/an/bin/PyCompiler/malo.py", line 3      
    print x
          ^
SyntaxError: invalid syntax

^

Re: How do I catch a syntax error with except? 80 80

Here is attached my test. I did not find any problem.

Re: How do I catch a syntax error with except? 80 80

Hi,

I'm trying to make a frontend for the compile module in python.

Everything works just right, except when the file has mistakes in the code that lead to a syntax error.

It does raise the invalid syntax error in the terminal, but instead of executing the block inside the except statement, it goes on to the else block. look:

try:
        file = t1.get()
        py_compile.compile(file)
    except IOError:
        tkinter.messagebox.showerror("Error", "File not found")
    except UnicodeDecodeError:
        tkinter.messagebox.showerror("Error", "This is not python code")
    except SyntaxError:
        tkinter.messagebox.showerror("Error", "There is a syntax error in the code")
    else:
        time.sleep(2)
        tkinter.messagebox.showinfo("¡Successfully compiled!", "The file \n(%s)\n was compiled successfully" %file)

According to the py_compile's module doc, it seems that you should call py_compile.compile(file, doraise=True) and it should raise a py_compile.PyCompileError and not a SyntaxError.

Re: How do I catch a syntax error with except? 80 80

What snippsat said is good,but what if I have several messages? One for each particular exception?

Gribouillis, thanks a lot man, you're really helpful. This works wonders.

Re: How do I catch a syntax error with except? 80 80

according to the documentation, it only raises py_compile.PyCompileError

so you probably need to except PyCompileError.

Re: How do I catch a syntax error with except? 80 80

Yes, it's working perfectly now. The trick was to change the type of error raised by the compiler function. It seems that SyntaxError's are not caught by except, at least not in this context.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.