On Thu, Jan 15, 2004 at 11:17:21PM -0600, Jason E. Sibre wrote:
> If I have a syntax error in a PTL file, I get an [almost] useless exception.
> For example, I modified a good PTL file (pages.ptl) by removing a quote (")
> from line 38, and fired up python and did:
>
> import quixote as q
> q.enable_ptl()
> import pages
>
> ... and I get the following traceback:
> Traceback (most recent call last):
> File "/usr/local/lib/python2.3/site-packages/quixote/ptl_compile.py", line
> 229, in parse
> raise SyntaxError(str(value), (filename, lineno, offset, text))
> File "/home/jsibre/programs/python/mp3/mp3player/web/pages.ptl", line 4
> import time
> ^
> SyntaxError: invalid syntax (pages.ptl, line 4)
>
> Line 4? (It's the first non-comment line in the file.)
I can't reproduce this. However, I believe you've run into a real
problem. The history of this part of Quixote is pretty long. The
short story is that the 'parser' module in Python <= 2.2 can raise
SyntaxError or ParserError. ParserError is basically useless since
it does not include a good description of the error and is missing
the lineno.
In Python >= 2.3, the 'parser' module does not raise ParserError
when it means SyntaxError (I'm not 100% sure about this but it seems
to be true). I don't know why things are going wrong with 2.3, but
here's my proposed fix:
if sys.hexversion >= 0x20300b1:
def parse(buf, filename=''):
buf = translate_tokens(buf, filename)
return TemplateTransformer().parsesuite(buf)
else:
# The parser module in Python <= 2.2 can raise ParserError. Since
# the ParserError exception is basically useless, we use compile()
# to generate a better exception.
def parse(buf, filename=''):
buf = translate_tokens(buf, filename)
try:
return TemplateTransformer().parsesuite(buf)
except (parser.ParserError, SyntaxError):
import __builtin__
try:
__builtin__.compile(buf, filename, 'exec')
except SyntaxError, exc:
# Another hack to fix the filename attribute (otherwise
# it is often "", even though we specify it).
raise SyntaxError(str(exc), (filename, exc.lineno, exc.offset,
exc.text))
Everyting in the 'else' clause is for backwards compatibility only. It
can eventually go away.
Neil