[IronPython] How to run ScriptCode from ScriptCode.LoadFromAssembly?

Dino Viehland dinov at microsoft.com
Mon Sep 29 17:01:34 PDT 2008

My guess is you're hitting a bug I already have a fix for.  What's probably happening is we're burning the SymbolID into the compiled code and then when you run it we have the value inappropriately associated with the wrong string.  This should be fixed in the latest sources which were just pushed up today.

Also in RC1 ScriptRuntime.LoadAssembly will effectively do the load for you and then a simple "import mymodulename" should work although maybe you don't want to do compile & run the import.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Yongming Wang
Sent: Monday, September 29, 2008 4:33 PM
To: users at lists.ironpython.com
Subject: [IronPython] How to run ScriptCode from ScriptCode.LoadFromAssembly?

I want to save the ScriptCode to assembly, load and run it next time.
I tried 3 ways, but all failed.

1. Here is my code

        public void RunScript(string script)
                if (!System.IO.File.Exists(@"E:\Source
                    _source =
                    _binary = _source.Compile();
                    ScriptCode code =
                    List<ScriptCode> codes = new List<ScriptCode>();
                    ScriptCode[] codes =
                    if (codes != null && codes.Length >= 1)
                        OptimizedScriptCode optimizedCode =
                        Scope scope = optimizedCode.CreateScope();
                        this._mainForm.setScope(scope); //
SetObjectName "Button1" etc.
            catch (System.Exception ex)
As you can see, ScriptCode.SaveToAssembly was execute succesfully.
And ScriptCode.LoadFromAssembly successfully returned.
But when I got exception when I run it.
I got Microsoft.Scripting.Runtime.UnboundNameException
{"name 'Button1' is not defined"}
But I have set the object name to the scope. I don't know why.

2. If I use another ScriptScope _scope, e.g.
  OptimizedScriptCode optimizedCode = (OptimizedScriptCode)codes[0];
I got System.NullReferenceException because code is null in the
following function of OptimizedScriptCode.cs
        protected override object InvokeTarget(LambdaExpression code,
Scope scope) {
            if (scope == _optimizedScope) {
                return _optimizedTarget(scope, LanguageContext);

            // new scope, compile unoptimized code and use that.
            if (_unoptimizedTarget == null) {
                // TODO: fix generated DLR ASTs - languages should
remove their usage
                // of GlobalVariables and then this can go away.
                code = new GlobalLookupRewriter().RewriteLambda(code);

                _unoptimizedTarget =

            return _unoptimizedTarget(scope,

3. If I  modified the InvokeTarget function to
 if (scope == _optimizedScope || code == null)
I got System.InvalidCastException.
Cann't convert Microsoft.Scripting.Runtime.SymbolDictionary to
I don't know why this convertion wass needed.

Could anyone give a help?

Users mailing list
Users at lists.ironpython.com

More information about the Users mailing list