[Python-Dev] Syntax errors on continuation lines

Roger Miller rogermiller at alum.mit.edu
Mon May 29 01:32:04 CEST 2006


I am a recent subscriber to this list.  A couple of months ago
I downloaded the Python source out of curiosity, then decided
to see if I could scratch a couple of small itches.

One of them was syntax errors reported on one line but actually
resulting from unbalanced brackets on the previous line. (The
folks on this list may not make such silly mistakes any more,
but several postings on comp.lang.python suggest that I am not
entirely alone in finding it an annoyance.)  I have modified
the parser and error reporting code to provide more explicit
information in such cases, and am now looking for some guidance
as to whether it is worth submitting a patch.

With my change a syntax error on a continuation line looks like
this:

      File "test.py", line 42 (statement started on line 41)
        y = 0
          ^
    SyntaxError: invalid syntax

The patch adds a new attribute to the SytaxError exception
object (the line number on which the statement started).  I
don't have the experience to judge whether this would be
considered a compatibility problem.  An alternative that does
not require modifying the exception would be to change the
message text without identifying the specific line number:

      File "test.py", line 42
        y = 0
          ^
    SyntaxError: invalid syntax in continuation of prior line

However I think the continuation indication properly belongs
with the location information rather than with the message.
(The loss of the starting line number itself is no big deal;
it will usually be one less than the error line number.)




More information about the Python-Dev mailing list