Is it possible to seperate Python's Syntax checker from the Interpreter?

Fredrik Lundh fredrik at pythonware.com
Tue Dec 17 12:15:46 EST 2002


Jason Keith Ergle wrote:

> I am developing a simple Python IDE for one of our apps.  I need
> to allow the users a simple way of checking the correctness of the
> syntax of their Python code.  I wanted to use the Python interpreter,
> but I cannot allow the user to execute their code at this point.  Is
> their a way I can force the interpreter to just do its syntax check,
> and then exit without attempting to execute the code?  Any help
> would be appreciated, thanks.

compile is your friend:

    try:
        code = compile(text, "<string>", "exec")
    except (SyntaxError, ValueError, OverflowError):
        print "oops!"

more info here:

    http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-10

the "code" and "codeop" modules might also be useful:

    http://www.python.org/doc/current/lib/module-code.html
    http://www.python.org/doc/current/lib/module-codeop.html

</F>





More information about the Python-list mailing list