[New-bugs-announce] [issue5064] compiler.parse raises SyntaxErrors without line number information

Jean-Paul Calderone report at bugs.python.org
Mon Jan 26 04:25:44 CET 2009

New submission from Jean-Paul Calderone <exarkun at divmod.com>:

Sometimes a syntax error in source passed to `compiler.parse´ causes a
`SyntaxError´ with lots of nice information to be raised:

>>> from compiler import parse
>>> try:
...     parse("def f(")
... except SyntaxError, e:
...     pass
>>> e
SyntaxError('unexpected EOF while parsing', (None, 1, 6, 'def f('))

But for other syntax errors, only a string message is provided, no
location information:

>>> try:
...     parse("def f(x=10, y): pass")
... except SyntaxError, f:
...     pass
>>> f
SyntaxError('non-default argument follows default argument',)
>>> try:
...     parse("f(x=10, y)")
... except SyntaxError, g:
...     pass
>>> g
SyntaxError('non-keyword arg after keyword arg',)

On the other hand, the built in compiler produces exceptions which do
have this information in these cases.

The absence of the information makes the job of tools trying to operate
on Python source code more difficult (naively written, they'll simply
fail when they encounter one of these less informative exceptions).

components: Library (Lib)
messages: 80559
nosy: exarkun
severity: normal
status: open
title: compiler.parse raises SyntaxErrors without line number information
type: behavior

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list