Checking Python Syntax without Executing
From IronPython Cookbook
(Redirected from Checking Python Syntax with Executing)
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
Here, ErrorListener is some class that derives from Microsoft.Scripting.Hosting.ErrorListener and stores the errors.
Back to Contents.

