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

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

^

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

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.

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.

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.

Be a part of the DaniWeb community

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