[IronPython] IP 2.0 Hosting

Dino Viehland dinov at exchange.microsoft.com
Mon Dec 17 17:48:17 CET 2007


Yes, the document is not yet up to date - we're currently working on implementing it so the next release will probably have some of it, and subsequence releases will have the whole thing.

For giving objects to C# code you need to derive from the interface (and implement the methods).  If you fail to implement any methods we'll throw at runtime when that method is called.

For exposing classes/namespaces/etc...  I think we may have a deficiency here even in the new model.  While we do have a global namespace per ScriptRuntime I don't believe we have a way to say "populate this with these types/namespaces".  I'll bring this up next time we're going over the spec internally.

Here's some simple code you can give a shot to get started.  I don't have anything comprehensive, this is just what I pulled from EngineTest.cs which if you get the IronPython source code is in IronPythonTest.  I imagine whatever Toni can post will be more useful :).

using IronPython.Runtime.Hosting;
using Microsoft.Scripting;
...

PythonEngine pe = PythonEngine.CurrentEngine;

pe.Execute(script); # where script is a string w/ some code in it.  This runs in the DefaultModule which is:

IScriptModule default_module = ScriptDomainManager.CurrentManager.Host.DefaultModule;

You can also do:

pe.Execute("x = 1", anonymousModule);

You can create new modules via:

ScriptModule publishedModule = pe.CreateModule("published_context_test", ModuleOptions.PublishModule);



-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Toni Alatalo
Sent: Monday, December 17, 2007 1:04 AM
To: users at lists.ironpython.com
Subject: Re: [IronPython] IP 2.0 Hosting

On Monday 17 December 2007 03:52:03 Slide wrote:
> I am at a huge loss trying to figure out hosting IP 2.0 in my C#
> application to provide scripting capabilities to my app. Does ANYONE
> have an example of this? I tried grabbing the document listed in a
> post a little while ago, but the class names in the document don't
> match anything I have in the package I downloaded. I'm looking for the

I have done it for two systems now.

Apparently that document is not about the implementation in the 2.0 alphas,
but I've guessed that of a plan for the next step.

The embeddings I made were actually done before that document was sent, and I
just mostly did trial-and-error based on the method names and signatures etc.
Don't have the code here now, but unless someone sends something soon can put
something together later today (in 6 hours or so) .. it's simple, nothing
special there.

> ability to have a class in Python inherit (implement) an interface
> defined in C# and then create an instance of that class for use from
> the C#.

Python uses 'duck typing', so to implement an interface you just implement it,
i.e. define the class so that it has the methods that the interface requires.
I actually don't know if that is the case when giving objects to c#-written
code that expects something that implements a certain interface, but based on
how I've used delegates my guess is that no extra syntax is needed. For
example if there is a delegate type MyDelegate(int i); defined in c#, a
normal Python function that takes 1 argument is accepted
(ironpython/dlr/something seems to introspect the definition somehow at
runtime and raises an error if the signature does not match).

I have not yet found a way to expose selected c#-written classes (or
interfaces) to the embedded Python context - have tried many things with the
context and module etc. creation methods that are there in 2.0 alphas with no
luck.

What works, and what I've resorted to currently, is just adding references to
the dlls also on the Python side, and importing the modules/classes in
Python. Typechecking of objects created from those assemblies seems to work
back on the c#-written side as long as the path from where you loaded the dll
is exactly the same.

With 1.1 it is possible to use the LoadAssembly command to make already loaded
assemblies from c# to show in the Python side. I am guessing that the 2.x
series will have something similar using the runtime objects or what was it
that is mentioned in that document, but there was no detailed info of that in
the doc yet and I have not found any implementation of it either (i have not
checked out stuff from codeplex repo though, just used alpha releases).

> slide

~Toni
_______________________________________________
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