Checking Python Syntax without Executing

From IronPython Cookbook

(Difference between revisions)
(New page: 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. <pre> engine = IronP...)
Line 10: Line 10:
</pre>
</pre>
 +
 +
Here, ErrorListener is some class that derives from '''Microsoft.Scripting.Hosting.ErrorListener'' and stores the errors.
Back to [[Contents]].
Back to [[Contents]].

Revision as of 13:49, 11 June 2009

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.

TOOLBOX
LANGUAGES