Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError inconsistency in Python?

Consider these two snippets:

try:
    a+a=a
except SyntaxError:
    print "first exception caught"

.

try:
    eval("a+a=a")
except SyntaxError:
    print "second exception caught"

In the second case the "second exception .." statement is printed (exception caught), while in the first one isn't.

Is first exception (lets call it "SyntaxError1") any different from second one ("SyntaxError2")?

Is there any way to catch SyntaxError1 (thus supressing compilation-time errors)? Wrapping large blocks of code in eval is unsatisfactory ;)

like image 516
gorsky Avatar asked Dec 06 '09 19:12

gorsky


People also ask

How do I fix invalid syntax in Python?

Defining and Calling Functions You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

What are syntax errors in Python?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.

What are the 3 errors in Python?

There are mainly three kinds of distinguishable errors in Python: syntax errors, exceptions and logical errors.

What is the syntax error?

In computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language. For compiled languages, syntax errors are detected at compile-time. A program will not compile until all syntax errors are corrected.

What are syntax errors in Python?

Syntax errors are the single most common error encountered in programming. Regardless of the language used, programming experience, or the amount of coffee consumed, all programmers have encountered syntax errors many times. Syntax problems manifest themselves in Python through the SyntaxError exception.

Why is my syntax invalid in Python?

Often, the cause of invalid syntax in Python code is a missed or mismatched closing parenthesis, bracket, or quote. These can be hard to spot in very long lines of nested parentheses or longer multi-line blocks. You can spot mismatched or missing quotes with the help of Python’s tracebacks: >>>.

What are SyntaxError and indentationerror in Python?

There are two other exceptions that you might see Python raise. These are equivalent to SyntaxError but have different names: These exceptions both inherit from the SyntaxError class, but they’re special cases where indentation is concerned. An IndentationError is raised when the indentation levels of your code don’t match up.

Why do I get SyntaxError when defining a function in Python?

You might run into invalid syntax in Python when you’re defining or calling functions. For example, you’ll see a SyntaxError if you use a semicolon instead of a colon at the end of a function definition: The traceback here is very helpful, with the caret pointing right to the problem character.


1 Answers

In the first case, the exception is raised by the compiler, which is running before the try/except structure even exists (since it's the compiler itself that will set it up right after parsing). In the second case, the compiler is running twice -- and the exception is getting raised when the compiler runs as part of eval, after the first run of the compiler has already set up the try/except.

So, to intercept syntax errors, one way or another, you have to arrange for the compiler to run twice -- eval is one way, explicit compile built-in function calls another, import is quite handy (after writing the code to another file), exec and execfile other possibilities yet. But however you do it, syntax errors can be caught only after the compiler has run one first time to set up the try/except blocks you need!

like image 122
Alex Martelli Avatar answered Oct 23 '22 08:10

Alex Martelli