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

Dino Viehland dinov at microsoft.com
Tue Sep 30 06:48:29 CEST 2008


Ok, it could be a problem on the compiling side instead then...  Does it work if you compile demo.dll w/ clr.CompileModules from Python instead and load w/ the code you have?  One significant difference here could be that your create/compile calls result in code that is being re-writen to be non-optimized code.  I think when we save it the code will be doing dictionary lookups instead of checking the array like it should.

BTW, I'd also advise against casting the code from LoadFromAssembly to OptimizedScriptCode - it could, and in fact most likely will, change.

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

Thanks,
But I tried the new version named IronPython-40877.zip, the problem
still exists. Is this version fixed?
And I tried to use ScriptRuntime.LoadAssembly
--------------------------------

_runtime.LoadAssembly(System.Reflection.Assembly.LoadFile(@"E:\Source
\IronPython_Main\Src\PyDemo\bin\Debug\demo.dll"));
                    //_engine.SetSearchPaths(new string[] { @"E:\Source
\IronPython_Main\Src\PyDemo\bin\Debug" });
                    _source =
_engine.CreateScriptSourceFromString("import demo",
Microsoft.Scripting.SourceCodeKind.Statements);
                    _source.Execute(_scope);
--------------------------------
I got an IronPython.Runtime.Exceptions.ImportException. {"No module
named demo"}
Is my code wrong?

Best Regards
Yongming

On Sep 30, 8:01 am, Dino Viehland <di... at microsoft.com> wrote:
> 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-boun... at lists.ironpython.com [mailto:users-boun... at lists.ironpython.com] On Behalf Of Yongming Wang
> Sent: Monday, September 29, 2008 4:33 PM
> To: us... 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)
>         {
>             try
>             {
>                 if (!System.IO.File.Exists(@"E:\Source
> \IronPython-2.0B5\Src\PyDemo\bin\Debug\demo.dll"))
>                 {
>                     _source =
> _engine.CreateScriptSourceFromString(script,
> Microsoft.Scripting.SourceCodeKind.Statements);
>                     _binary = _source.Compile();
>                     ScriptCode code =
> HostingHelpers.GetScriptCode(_binary);
>                     List<ScriptCode> codes = new List<ScriptCode>();
>                     codes.Add(code);
>                     ScriptCode.SaveToAssembly("demo.dll",
> codes.ToArray());
>                     _binary.Execute(_scope);
>                 }
>                 else
>                 {
>                     ScriptCode[] codes =
> ScriptCode.LoadFromAssembly(HostingHelpers.GetDomainManager(_runtime),
>                         System.Reflection.Assembly.LoadFile(@"E:\Source
> \IronPython-2.0B5\Src\PyDemo\bin\Debug\demo.dll"));
>                     if (codes != null && codes.Length >= 1)
>                     {
>                         OptimizedScriptCode optimizedCode =
> (OptimizedScriptCode)codes[0];
>                         Scope scope = optimizedCode.CreateScope();
>                         this._mainForm.setScope(scope); //
> SetObjectName "Button1" etc.
>                         optimizedCode.Run(scope);
>                     }
>                 }
>             }
>             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];
>   optimizedCode.Run(HostingHelpers.GetScope(_scope));
> --------------------------------------------------
> 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 =
> code.Compile<DlrMainCallTarget>(SourceUnit.EmitDebugSymbols);
>             }
>
>             return _unoptimizedTarget(scope,
> LanguageContext);
>         }
> -------------------------------------------------------
>
> 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
> Microsoft.Scripting.Runtime.GlobalsDictionary.
> I don't know why this convertion wass needed.
>
> Could anyone give a help?
> Thanks.
> Yongming
>
> _______________________________________________
> Users mailing list
> Us... at lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Us... at lists.ironpython.comhttp://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list