how to do *simple* syntax checking?

Andreas Jung ajung at sz-sb.de
Wed Dec 1 01:52:28 EST 1999


On Tue, Nov 30, 1999 at 11:50:53PM +0000, Preston Landers wrote:
>  Okay, I know there was an earlier thread regarding pre-distribution
> syntax checking (and the virtual impossibility of same).
> 
>  I understand Python is a dynamicly typed language, and I have no real
> need to be able to find previously undeclared names in code before I
> run it.
> 
>  However, it would be enormously helpful for me to be able to do
> *simple* checks on Python code at make/'make install' time.  Like
> catching whitespace/indentation problems, missing semicolons, and the
> like.
> 
>  I maintain a large set of Python programs, some of which are accessed
> through a CGI interface.  I develop some code, run make install,
> restart my web server (not always though) and then load up my URL.
> Only to find "500 Internal Server Error."
> 
>  Usually, if it's not a namespace issue, it's something silly like a
> missing semicolon.  But I have to track through the server logs to find
> out what went wrong.
> 
>  I thought that Python's compile() function would be what I want,
> because I thought that the SyntaxError exception was thrown when making
> bytecode.  But that's not the case, unless I was notably un-thorough in
> my investigation.
> 
>  Basically, what I want is to do something like this for every Python
> program in my makefile:
> 
> try:
>    simple_syntax_check("myfile.py")
> except:
>    # file is no good, abort make process
> 
> # ... continue with make
> 
>  Am I just dreaming? Is this unrealistic?  Or is this kind of thing
> fully availible and documented and I am just blind?
> 
> thanks,

The following code should do the job:

try:
    import myfile
except:
    print 'content-type: text/plain'
    print ''
    print 'my program sux'
    sys.exit(1)

myfile.main()


Cheers,
Andreas




More information about the Python-list mailing list