[IronPython] Loading DLL from pyc.py into ScriptEngine

hellosticky hellosticky at gmail.com
Wed Aug 27 01:10:12 CEST 2008


Hi Curt, yes, i'm using vanilla beta 4. Are there places to grab a nightly drop or do I need to compile from source?
Thanks,


  _____  

From: Curt Hagenlocher [mailto:curt at hagenlocher.org] 
Sent: Tuesday, August 26, 2008 7:04 PM
To: hellosticky at gmail.com; Discussion of IronPython
Subject: Re: [IronPython] Loading DLL from pyc.py into ScriptEngine


Are you using Beta 4?  You probably need to grab a post-beta-4 nightly drop; we got rid of the IsComObject constructions that don't work in partial trust, but it happened right around the time Beta 4 was released and may not have made it in.


On Tue, Aug 26, 2008 at 3:54 PM, hellosticky <hellosticky at gmail.com> wrote:


Okay, I tried it but got a Security Exception in System.Scripting.Com.ComMetaObject.IsComObject. The permission requested was for UnmanagedCode. I'm running in partial trust so obviously I can't be giving the code UnmanagedCode privilege. Is there some flag I have to set that says I'm in partial trust?

FirstPermissionThatFailed:
{<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0 <http://2.0.0.0/> , Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Flags="UnmanagedCode"/>
}

Server stack trace:
  at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
  at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Object assemblyOrString, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
  at System.Security.CodeAccessSecurityEngine.CheckSetHelper(PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Object assemblyOrString, SecurityAction action, Boolean throwException)
  at System.Security.CodeAccessSecurityEngine.CheckSetHelper(CompressedStack cs, PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Assembly asm, SecurityAction action)
  at System.Scripting.Com.ComMetaObject.IsComObject(Object obj)
  at System.Scripting.Actions.MetaAction.Bind[T](Object[] args)
  at System.Scripting.Actions.CallSite`1.CreateNewRule(Rule`1 originalMonomorphicRule, Object[] args)
  at System.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
  at System.Scripting.Actions.UpdateDelegates.Update6[T,T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
  at IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level)
  at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, String fullName, Int32 level)
  at <module>$2##2(Closure , Scope , LanguageContext )
  at System.Scripting.ScriptCode.InvokeTarget(LambdaExpression code, Scope scope)
  at System.Scripting.Runtime.OptimizedScriptCode.InvokeTarget(LambdaExpression code, Scope scope)
  at System.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
  at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
  at DlrHostedProvider.OnRender()

Here is the code I used (approximately):

               StringBuilder sb = new StringBuilder(100);

           using (MemoryStream ms = new MemoryStream())
           {
               using (StringWriter writer = new StringWriter(sb))

               {
                   Assembly assembly = Assembly.Load(AssemblyName);
                   ScriptRuntime runtime = ScriptRuntime.Create();
                   runtime.LoadAssembly(assembly);

                   runtime.IO.SetOutput(ms, writer);
                   ScriptEngine engine = runtime.GetEngine(GetEngineName());
                   ScriptScope scope = engine.CreateScope();
                   string code = string.Format(@"import clr
clr.AddReference(""{0}"")
import {1}
",
    AssemblyName,
    AssemblyType.Replace(".py", "")
    );
                   ScriptSource source = engine.CreateScriptSourceFromString(
                       code,
                       SourceCodeKind.Statements
                   );
                   source.Execute(scope);
               }
           }
           content = sb.ToString();

Thanks,


> -----Original Message-----
> From: hellosticky [mailto:hellosticky at gmail.com]

> Sent: Tuesday, August 26, 2008 6:46 PM
> To: 'Srivatsn Narayanan'; 'Discussion of IronPython'
> Subject: RE: [IronPython] Loading DLL from pyc.py into ScriptEngine
>
> Thanks, I will try that. Can you explain again how just doing
> "import test" executes the test.py file that was compiled
> into the DLL? It's not very logical..
>
> Thanks,

>
> > -----Original Message-----
> > From: Srivatsn Narayanan [mailto:srivatsn at microsoft.com]
> > Sent: Tuesday, August 26, 2008 6:34 PM
> > To: Discussion of IronPython; hellosticky at gmail.com
> > Subject: RE: [IronPython] Loading DLL from pyc.py into ScriptEngine
> >
> > Also Assembly.Load is not sufficient to be able to import the
> > compiled code. A clr.AddReference needs to be done which
> > publishes the module. The easiest way to do that is include
> > it in the string as Michael pointed out.
> >

> > ScriptSource source =
> > engine.CreateScriptSourceFromString("import
> > clr;clr.AddReference(\"test.dll\");import test",
> > SourceCodeKind.Statements);
> >
> > You might need to add the current folder to the path as well
> > before the clr.AddReference.
> >
> > -----Original Message-----
> > From: users-bounces at lists.ironpython.com
> > [mailto:users-bounces at lists.ironpython.com] On Behalf Of
> Michael Foord
> > Sent: Tuesday, August 26, 2008 3:12 PM
> > To: hellosticky at gmail.com; Discussion of IronPython
> > Subject: Re: [IronPython] Loading DLL from pyc.py into ScriptEngine
> >
> > hellosticky wrote:
> > > I also tried:
> > >
> > >             Assembly assembly = Assembly.Load(AssemblyName);
> > >             ScriptRuntime runtime = ScriptRuntime.Create();
> > >             runtime.LoadAssembly(assembly);
> > >             ScriptScope scope = runtime.ExecuteFile("test.py");
> > >             object o = scope.Execute(null);
> > >
> > > But that threw a script not found exception saying it could
> > find the source file for test.py, so it would seem like this
> > wants to recompile it
> > >
> > >
> > >> -----Original Message-----
> > >> From: hellosticky [mailto:hellosticky at gmail.com]
> > >> Sent: Tuesday, August 26, 2008 5:52 PM
> > >> To: IronPython
> > >> Subject: Loading DLL from pyc.py into ScriptEngine
> > >>
> > >> Hi,
> > >>
> > >> I created an IronPython DLL with "ipy.exe pyc.py /out:test
> > >> /target:dll test.py" which created test.dll. Now, from C#,
> > >> I'd like to execute test.py from test.dll. I just opened up
> > >> dlr-spec-hosting and there's a CreateScriptSourceFromStream,
> > >> but I don't see that in 2.0 Beta 4 bits. Here is where I'm
> > >> stuck. How do I get that assembly into the engine and then
> > >> execute test.py?
> >
> > Well - the easiest way is to execute 'import test' after
> having added
> > the assembly. This should execute the code in order to create
> > a module.
> >
> > ScriptScope scope = engine.CreateScope();
> > ScriptSource source =
> > engine.CreateScriptSourceFromString("import test",
> > SourceCodeKind.Statements);
> > engine.Execute(scope);
> >
> > There is an API for setting a stream as stdout on an engine -
> > 'runtime.IO.SetOutput'.
> >
> > Michael
> >
> > >>  Also, I'd like to get the output into a
> > >> string (I guess I can just redirect Console.Out?)
> > >>
> > >> public string Execute(string assemblyName)
> > >> {
> > >>     string content = null;
> > >>
> > >>     Assembly assembly = Assembly.Load(assemblyName);
> > >>     ScriptRuntime runtime = ScriptRuntime.Create();
> > >>     ObjectOperations operations = runtime.CreateOperations();
> > >>     ScriptEngine engine = runtime.GetEngine("py");
> > >>     // magic..
> > >>
> > >>     return content;
> > >> }
> > >>
> > >> Thanks,
> > >>
> > >
> > > _______________________________________________
> > > Users mailing list
> > > Users at lists.ironpython.com
> > > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> > >
> >
> >
> > --
> > http://www.ironpythoninaction.com/
> > http://www.voidspace.org.uk/
> > http://www.trypython.org/
> > http://www.ironpython.info/
> > http://www.theotherdelia.co.uk/
> > http://www.resolverhacks.net/
> >
> > _______________________________________________
> > Users mailing list
> > Users at lists.ironpython.com
> > http://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



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080826/07527b3f/attachment.html>


More information about the Ironpython-users mailing list