Embedding IronPython

I am trying my luck with embedded IronPython 2 using the DLR so I thought I would share some of the steps to make that happen.

Prereqs:

  • Microsoft.Scripting.dll
  • IronPython.dll
  • IronPython.Modules.dll

These all come from the IronPython2 beta 1 release--and yes you need all of them. Failing to add IronPython.Modules.dll as a reference results in strange errors when trying to get the scripting engine which can be confusing.

The interface to IronPython and presumably other DLR languages is through the Microsoft.Scripting.Hosting.ScriptEngine class. There seems to be about a billion ways to get the engine here is what worked for me:

ScriptEngine engine; engine = PythonEngine.CurrentEngine;

This gets/creates the ScriptEngine for IronPython. Next we create a Microsoft.Scripting.Hosting.ScriptScope which allows for variables to be bound on the embedder's end before the script is run. It also can be inspected after the script is run.

ScriptScope scope = engine.CreateScope();

Now all that is left is to load the script woooo. Doing this creates a Microsoft.Scripting.Hosting.ScriptSource object which can have fun things done to it.

ScriptSource source = engine.CreateScriptSourceFromFile("myScript.py"); source.Execute(scope);

This loads a script and then executes it using the scope that was created. The contents of the scope can be examined afterwards like I said which can be useful if you want to create factories that return an extended C#/CLR class that can then be used like normal.

TacticsGame game = scope.GetVariable("game");

This gets the global "game" form the script. In order for this to work right you need to add the assembly references to your script. This can be done in two ways. First you can do it the normal way from inside the script:

import clr clr.AddReference("MyAssembly.dll")

This works but its kind of annoying if MyAssembly.dll is actually the same assembly that the script is being hosted in or some other assembly that is known. Another way is to call engine.Runtime.LoadAssembly and feed it assemblies it should know about. See System.Reflection.Assembly for some ways of getting these assemblies.

Hopefully this works a bit better than boo did!

Embedding example
An informative but trying on the eyes tutorial.
IronPython Website
In all of its official and incompletely documented glory.

XNA ContentManager.RootDirectory Considered Annoying as Hell ( XCCAH)

Beware of older XNA code using code created by the new project templates! The newer code sets the root directory of Content to "Content/" which breaks all my old code and was very confusing. Which leaves me wondering if they are going to include that as the default why not change it in the class! Blargg. Now any "game libraries" which have content that gets loaded have to worry about what the RootDirectory setting is at unless they use their own ContentManager.

ContentManager.RootDirectory Property <-- boo

Time to put patches down.

When you are coding what we might call language X and you ever think "yay it actually lets me do this non-stupid thing" such as map, filter or anything really a sane person might need when constructing software then you know you have a problem.

I am looking at you perl 5.

*glare*

Ruby,anything from ml,python,scheme,lisp... I can't count the number of better languages suited for most jobs perl gets used for still. I normally not a language zealot but jebus! If I see another perl hash with elements whose values are "1" because perl has not set data type I am going to vomit.

Perl is not regression in programming languages as much as it is a scripting language that is on life support. Its an old man clinging to life after a long and fulfilling life. Its time to pull the plug and let gramps die in peace.

OK I lied it probably was but lets just let it die in dignity. Perl was one of my first scripting languages so well I hate it slightly less than I should but still...

You know its bad when you would rather be writing java than perl. Actually its not too bad with anonymous classes. Why the hell they don't add anonymous functions I don't know. I guess they don't want to ruin the master piece that is java.

mmm rant tastic. I need to record my insane ramblings so that whatever future society goes through my blog (cause you know they will) gets the record straight. I am one of those scary computer scientist Larry Wall warns you about in programming perl--and I hate sunshine puppies and probably a lot other things. Especially kittens. I also eat metal scraps and speak in binary. In fact I often eat kittens for desert while speaking in binary.

Wendorb

Mmmm cafe!