Compile without execute for syntax check?

Alex alex at somewhere.round.here
Wed Mar 8 00:11:04 CET 2000


Hi, Bernhard.

I have a little program that I get emacs to run each time before I save
a python file, or execute a region of one:

#!/usr/bin/python

import sys, os

filename = sys.argv[1]

if not os.path.exists(filename):
    raise IOError, 'File not found.'

file = open(filename).read()
try:
    compile(file, 'test', 'exec')
except:
    exception_instance = sys.exc_info()[1]
    print '%i %i' % (exception_instance.lineno,
                     exception_instance.offset)
else:
    print 'No error'

If it finds an error, it prints out where in the file it is, so that
emacs can warp the cursor there.

Alex.



More information about the Python-list mailing list