[IronPython] Syntax Checking

Leo Carbajal desleo at gmail.com
Mon Oct 6 06:57:02 CEST 2008


Thanks for the helpful info, as always. This latest release has made it
possible, at last, to run my code in a neat little restricted sandbox.

As part of my never ending quest to provide a "safe"(tm) scripting
environment for my app, I've finally boiled down the boilerplate of the
hosting model to feature the following function (not counting all the engine
setup). My question centers around the way I compile and "cache" the code.
Realistically, most of the scripts the app will be running will be edited a
handful of times as they're tested and then more or less left alone, one
issue though is that there could potentially be hundreds of them. Will that
cause any issues with memory or performance? Compiling the scripts rather
than intepreting them every time provides a tremendous (and obvious)
performance gain for all future execution of the scripts.

In short, my question is: am I doing something dumb here?

private static Dictionary<string, CompiledCode> codeCache = new
Dictionary<string, CompiledCode>();
        public static void RunPublicScript(FileInfo script, ScriptScope
scope)
        {
            try
            {
                if (script.LastWriteTime > script.LastAccessTime ||
!codeCache.ContainsKey(script.FullName))
                {
                    string sourceContent =
File.ReadAllText(script.FullName);
                    ScriptSource source =
publicEngine.CreateScriptSourceFromString(sourceContent,
SourceCodeKind.Statements);
                    codeCache[script.FullName] =
source.Compile(ErrorReporter);

                    if (ErrorReporter.Errors.Count > 0)
                    {
                        throw new
InvalidImplementationException(ErrorReporter.ListErrors());
                    }

                    codeCache[script.FullName].Execute(scope);
                    script.LastAccessTime =
script.LastWriteTime;
                    return;
                }

                CompiledCode chunk;
                if (codeCache.TryGetValue(script.FullName, out chunk))
                    chunk.Execute(scope);

            }
            catch (InvalidImplementationException synError)
            {
                throw synError;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

I may run the Execute() commands in a separate thread, later, to protect
against infinite loops - but I want to get more of the application up and
running before trying such worst-case scenarios.


On Thu, Oct 2, 2008 at 3:28 PM, Dino Viehland <dinov at microsoft.com> wrote:

>  In 2.0 you can call GetCodeProperties on a ScriptSource and it'll give
> you an indication of how the code is.  You can also call
> ScriptSource.Compile w/ an ErrorListener which can get more detailed
> information about the failures.
>
>
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Leo Carbajal
> *Sent:* Thursday, October 02, 2008 1:10 PM
> *To:* Users at lists.ironpython.com
> *Subject:* [IronPython] Syntax Checking
>
>
>
> Is there any way to check the syntax of a script without actually running
> the script? Akin to the way VStudio does the 'code smells' nowadays.
>
> Of course, by 'any way' I'm probably asking if there's an 'easy way', but
> I'll take the hard way, too, if it's something I can figure out and re-use.
>
> ---
> V
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081005/d98ec4c5/attachment.html>


More information about the Ironpython-users mailing list