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)

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."

Edited 6 Years Ago by snippsat: n/a

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.

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

^

Edited 6 Years Ago by G_S: n/a

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.

Edited 6 Years Ago by Gribouillis: n/a

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.

Edited 6 Years Ago by G_S: n/a

according to the documentation, it only raises py_compile.PyCompileError

so you probably need to except PyCompileError.

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.

This question has already been answered. Start a new discussion instead.