Truncating SyntaxError

SyntaxError is not appropriate for other parsing errors. One problem is that its representation for location of the error is different from json.JSONDecodeError, re.error, pickle.UnpicklingError, pyexpat.ParseError, etc (global offset vs the line-column pair, 0- vs 1- based indices, text vs bytes, single point vs span). Other problem is that the parsed source often has too long lines for non-Python sources. SyntaxError is also tightly associated with Python source errors, so some user code can be confused if other exceptions will became a subclass of SyntaxError.

I see two general solutions:

  • Introduce a special protocol (a set of attributes and methods) for parsing errors. SyntaxError, json.JSONDecodeError, etc (maybe even UnicodeError), should implement such protocol, and the traceback module should use that protocol instead of special casing SyntaxError.
  • This is a part of more ambitious plan for uniting notes and tracebacks (and the chain of handled exceptions) – instead of separate __notes__, __traceback__ and __context__, add notes, tracebacks, etc to a single linked list. Location information can be added as a kind of traceback node. For example, see new detailed exception notes for pickling or JSON serializing errors in the main branch. They could be structured nodes instead of plain string notes.
6 Likes