Checking Python Syntax without Executing

From IronPython Cookbook

Revision as of 13:49, 11 June 2009 by MichaelFoord (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

A useful snippet of code (actually shown from IronPython - but very easy to translate into C#) for checking the validity of Python code without actually executing it.

engine = IronPython.Hosting.Python.CreateEngine()
source = engine.CreateScriptSourceFromString(text, SourceCodeKind.File)
errors = ErrorListener()
command = source.Compile(errors)
if command is None:
    # compilation failed

Back to Contents.