Instead of our planned presentation, we bring you rambling IronPython spelunking...

This entry was going to cover thinking about your .Net classes from the perspective of Python consumption... but actually it's not, we'll cover that another time.

What this post is really going to be about is how to spelunk a little into our managed classes via the IronPython interactive console - and to see how some common practices in .Net have interesting results in python.

This entry is going to be a little slow paced, I'm making the assumption you're a .Net developer, and haven't cut Python code before, if you know your way around Python probably best to pass this one over.

Also worth noting at this point that I'm really just a Python hacker, and certainly no guru...

So first off, we have a class with a bunch of overloads for various methods, here is the interface... I've left the code for the class itself out, but the class is called MyFileStore.

public interface IMyFileStore
Guid AddFile(string fileName);
Guid AddFile(string fileName, string mimeType);
Guid AddFile(byte[] contents, string mimeType);
Guid[] AddFiles(params string[] fileNames);
Guid AddFile(byte[] contents);
byte[] ReadFile(Guid id, out string mimeType);
void ReadFile(Guid id, string fileName);
void ExtractFiles(ref IList myList);

Pretty simple, now lets look at using this class with Iron python, I'll be running with the ipy (the interactive interpreter) so we can investigate our types as we go along (this is an executable which comes with the Iron Python binaries). The ipy console has a prompt consisting of three greater then signs ">>>" so you'll know what I'm typing into the console by looking for those.

First off, we need to load our type... this is pretty trivial...

>>> import clr


>>> from IronPythonLib import *

All done, lets make sure our class is in there, using the inbuilt function dir() - a list of built-in functions can be found here:

>>> dir()

['IMyFileStore', 'MyFileStore', '_', '__builtins__', '__doc__',
'__name__', 'clr', 'results', 'site', 'store', 'sys']

MyFileStore, sweet.

First off, lets create an instance of my type (not yours ;o)

>>> store = new MyFileStore()

Traceback (most recent call last):

SyntaxError: unexpected token MyFileStore (, line 1)


Arse, python doesn't have a new operator... forgot that, lets try it again.

>>> store = MyFileStore

Right, looks good, interpreter didn't return any errors... but wait, lets make sure we got what we want, which is an instance of MyFileStore....

>>> store


Woops, that's not what we wanted, we want an instance, not the type... lets try again:

>>> store = MyFileStore()

>>> store


That's better, lets find out what our store is capable off, using the dir command.

>>> dir(store)

['AddFile', 'AddFiles', 'Equals', 'ExtractFiles', 'Finalize',
'GetHashCode', 'GetType', 'MakeDynamicType', 'MemberwiseClone',
'ReadFile', 'Reduce', 'ReferenceEquals', 'ToString', '__class__',
'__doc__', '__init__', '__module__', '__new__','__reduce__',
'__reduce_ex__', '__repr__']


We see our AddFile, AddFiles,ReadFile & ExtractFiles methods - everything looks to be in order... but what are the parameters for those methods?

>>> dir(store.AddFile)

['Call', 'CallInstance', 'Equals', 'Finalize', 'GetHashCode',
'GetTargetType', '

GetType', 'Make', 'MakeDelegate', 'MakeDynamicType',
'MemberwiseClone', 'Overloads', 'PrependInstance', 'Reduce',
'ReferenceEquals', 'ToString', '__call__', '__class__',
'__doc__', '__eq__', '__getitem__', '__hash__', '__init__',
'__module__', '__name__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__self__', '__str__']


Hmm... what's the "Overloads" thing, lets find out...

>>> dir(store.AddFile.Overloads)

['Equals', 'Finalize', 'Function', 'GetHashCode', 'GetOverload',
'GetTargetFunction', 'GetType', 'MakeDynamicType',
'MemberwiseClone', 'Reduce', 'ReferenceEquals', 'Targets',
'ToString', '__class__', '__doc__', '__getitem__', '__init__',
'__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__str__']


That doesn't seem very useful, though what if we check it's string representation?

>>> store.AddFile.Overloads

at 0x0000000000000036 [{'Guid AddFile(self, str fileName)':
, 'Guid AddFile(self, str
fileName, str mimeType)': ,
'Guid AddFile(self, Array[Byte] contents, str mimeType)':
, 'Guid AddFile(self,
Array[Byte] contents)':


Look at that, there are our 4 overloads - but it's a bit jumbled, what am I looking at it....

Well at first glance it looks a bit like a python dictionary... notice the colon separating the signature for the method and ... what if we try to grab one for use later, maybe for some functional programming, why don't we get the one which takes the fileName and mimeType parameters eh?

>>> myFunc =

Traceback (most recent call last):

File , line 0, in ##100

TypeError: __getitem__() takes exactly 1 argument (1 given)


Bugger, that didn't work... still, not a bad guess - one parameter eh? Lets have another stab, maybe it just wants us to group the types together somehow?

>>> myFunc =

>>> myFunc



Looks better, of course if we wanted to we could use our function right now... let's give it a whirl:

>>> myFunc("d:testinput.3gp",



Looks to have worked, the method has successfully returned us a Guid identifying the new file... shame we didn't save it into a variable for future reference, cest la vie.

Luckily I can just copy and paste it - lets see if I can get the file back using this overload...

byte[] ReadFile(Guid id, out string mimeType);

Simple as.. lets try...

>>> id =

Traceback (most recent call last):

File , line 0, in ##116

NameError: name 'Guid' not defined


Woops, we need to import the System namespace...

>>> import System

Now lets try again...

>>> id =

Traceback (most recent call last):

File , line 0, in ##116

NameError: name 'Guid' not defined


Huh... oh wait, let's check our local symbol table:

>>> dir()

['MyFileStore', 'System', '_', '__builtins__', '__doc__',
'__name__', 'clr', 'myArray', 'myFunc', 'results', 'site',
'store', 'sys']

System eh? Lets dig into it...

>>> dir(System)

['AccessViolationException', 'Action', 'ActivationContext',
'Activator', 'AppDom

ain', 'AppDomainInitializer', 'AppDomainManager',

nOptions', 'AppDomainSetup', 'AppDomainUnloadedException',
'ApplicationException... and all the rest.


Well, what can we do about that... we could import them all using "from System import *" - but we only need Guid, so why don't we just grab that eh?

>>> from System import Guid

>>> dir()

['Guid', 'MyFileStore', 'System', '_', '__builtins__', '__doc__',
'__name__', 'clr', 'myArray', 'myFunc', 'results', 'site',
'store', 'sys']


Sweet, now lets try it again... finally!

>>> id =

>>> mimeType = ""

>>> contents = store.ReadFile(id, mimeType)

Traceback (most recent call last):

File , line 0, in ##136

File , line 0, in ReadFile##66

File D:devProjectsIronPythonDemoIronPythonLibMyClass.cs,
line 70, in Read


File mscorlib, line unknown, in WriteAllBytes

File mscorlib, line unknown, in .ctor

File mscorlib, line unknown, in Init

ValueError: Empty path name is not legal.


Oh no! what went wrong??... what's this about an empty path name, I was expecting to get some bytes back.... Hmmm, don't panic, lets just do some digging...

>>> store.ReadFile.Overloads

at 0x0000000000000039 [{'(Array[Byte], str) ReadFile(self, Guid
id)': , 'ReadFile(self, Guid
id, str fileName)':


Well there's the problem, I've called the wrong version, but... that's odd, there's a ReadFile there I haven't defined in my class... and one that's missing... hmmm, time to file a bug report.

Or not, this is just where the Python and .Net world don't see eye to eye - Python doesn't know about out parameters as such, so it simulates the effect by altering the method's signature - lets have a go at playing by IronPython's rules:

>>> contentsAndMimeType =

>>> len(contentsAndMimeType)


>>> type(contentsAndMimeType[0])

>>> type(contentsAndMimeType[1])

>>> contentsAndMimeType[1]



So our "out string mimeType" parameter was returned by the method instead, as the second item in an array... hmm... interesting.

Well if python does this to out parameters, what does it do to ref parameters - lets try the extract files method to see what happens... this method is implemented to create a new list if we don't supply a valid instance, but lets pass in a valid one first - I wont be caught out this time, so I'll import the types I need first (List):

>>> from System.Collections.Generic import

>>> bytes = List()

Traceback (most recent call last):

SyntaxError: unexpected token ] (, line 1)


Oh dear, looks like Python doesn't use the syntax... lets start digging again...

>>> dir(List)

['Add', 'AddRange', 'AsReadOnly', 'BinarySearch', 'Capacity',
'Clear', 'Contains

', 'ConvertAll', 'CopyTo', 'Count', 'Enumerator', 'Equals',
'Exists', 'Finalize'

, 'Find', 'FindAll', 'FindIndex', 'FindLast', 'FindLastIndex',
'ForEach', 'GetEn

umerator', 'GetHashCode', 'GetRange', 'GetType', 'IndexOf',
'Insert', 'InsertRange', 'LastIndexOf', 'MakeDynamicType',
'MemberwiseClone', 'Reduce', 'ReferenceEquals', 'Remove',
'RemoveAll', 'RemoveAt', 'RemoveRange', 'Reverse', 'Sort',
'ToArray', 'ToString', 'TrimExcess', 'TrueForAll', '__class__',
'__doc__', '__getitem__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setitem__']

>>> List


Hmm... nothing obvious there, but we could hazard a guess...

>>> List[str]


Hmmm... so lets give our List of byte array another go...

>>> bytes = List[byte[]]()

Traceback (most recent call last):

SyntaxError: unexpected token ] (, line 1)

Hmm... it doesn't like byte, lets have a look...

>>> byte

Traceback (most recent call last):

File , line 0, in ##56

NameError: name 'byte' not defined

>>> Byte


Ahhh... I forgot that "byte" (lowercase) isn't actually what the Clr calls it... hmm... we could try it again, but it wont work - simply because Byte[] doesn't make sense (if we're using square brackets to index into a generic type... ) so having another dig in the IronPython docs we see that we need to index to the type "Array" with the type of our array, given this knowledge we can get what we want...

>>> bytes = List[Array[Byte]]()

>>> bytes


Sweet, now to call that ExtractFiles method...

>>> store.ExtractFiles(bytes)

>>> bytes

>>> len(bytes)


Well it returned a list... but it did accept our reference parameter, and it has updated the collection we passed to it... but what if we had passed it a null instead?

>>> bytes = None

>>> store.ExtractFiles(bytes)

>>> len(bytes)

Traceback (most recent call last):

File , line 0, in ##83

File , line 0, in Length##75

TypeError: len() of unsized object of type <>


Hmmm... it returned a list, but it didn't set the value in our variable "bytes" - this isn't really ref like behavior... yet, if you think about it we can get almost the same thing by doing this:

>>> bytes = store.ExtractFiles(bytes)

>>> len(bytes)



Right, our journey has almost come to an end... looking at the interface we have a method declared like so:

Guid[] AddFiles(params string[] fileNames);

params is handy in .Net code - anything to save tedious typing of array declarations, lets see if we can do the same thing in python?

>>> store.AddFiles("d:testinput1.3gp",

[d3e8f099-3005-461c-a63a-fdc69b2091ee]>, <>
object at 0x0000000000000032


Sweet, that's a pleasant surprise after that whole out and ref debacle, I was beginning to loosing hope!

But wait, why not revive our faith in Python a little more by looking at a few tricks...

>>> paramsDict = { "fileName" :
"d:testinput.3gp", "mimeType" : "video/3gpp"}

>>> store.AddFile(**paramsDict)



What did we just do? Well... given a dictionary, where the keys are the parameter names, we used them as the parameters for one of our .Net methods using the double asterisk syntax - think of what you'd have to do to code this in .Net... reflection city

It doesn't just have to be dictionaries... we can use arrays too...

>>> paramsArray = [ "d:testinput.3gp",
"video/3gpp" ]

>>> store.AddFile(*paramsArray)



Neat, of course, being a dynamic language and all, the selection of the appropriate overload is based on the types in the array or dictionary... it would be a nightmare to do both of these in .Net with reflection yourself.

Faith still not restored eh, what about creating a python class that wraps an existing (possibly sealed) .Net class, that forwards calls on unless we want to override the behavior...

>>> class MyFileStoreWrapper:

... def __init__(self, realStore):

... self.realStore = realStore

... def ExtractFiles(self, myList):

... raise Exception, "this method isn't allowed"

... def __getattr__(self, name):

... if name == "ExtractFiles": return ExtractFiles

... return eval("self.realStore."+name)


>>> store = MyFileStoreWrapper(MyFileStore())

>>> store.AddFile("d:testinput.3gp")



>>> store.ExtractFiles(None)

Traceback (most recent call last):

File , line 0, in ##52

File , line 0, in ExtractFiles

Exception: this method isn't allowed


ExtractFiles can't be invoked on our wrapper, however our wrapper automagically responds for other methods like AddFile - sadly I've used an eval (which, rhymes with Evil - coincidence?) here because MyFileStore doesn't expose a __getattr__ method (because it's a .Net classes, not a native Python one).

The nice thing now, is that given a native python class, we can do some things we aren't allowed to do to the underlying .Net class - like adding new methods at run time.

>>> def ExtractMoreFiles(self, myList):

... raise Exception, 'not implemented yet'


>>> MyFileStoreWrapper.ExtractMoreFiles =

>>> store.ExtractMoreFiles(None)

Traceback (most recent call last):

File , line 0, in ##60

File , line 0, in ExtractMoreFiles

Exception: not implemented yet


I think it's pretty cool, and though python's meta-programming model isn't quite up to ruby standards, it's still pretty easy to dig down and create some pleasantly surprising results :) and if you have your thinking caps on you can probably see how this stuff would really help to bring a DSL to life for your applications special needs.

And that's where I'm going to conclude this rambling post... ;o) Hopefully it'll spark some thoughts about what's possible with IronPython and allow you avoid some simple mistakes when using .Net classes in iron python.

Read More

Ignore this entry - just trying to fix the code-formatting problems

public void ExtendingClass()
PythonEngine engine = new PythonEngine();

engine.ExecuteToConsole("from IronPythonLib.Tests import *");

engine.Execute(@"class MyTransformer(IStringTransformer):
def Transform(self, input):
return input + "" is now transformed""");

IStringTransformer transformer = engine.EvaluateAs("MyTransformer()");
Assert.AreEqual("input is now transformed", transformer.Transform("input"));


edit: seems better now, swapped to a simpler layout though I doub't it will work well for people with lower resolutions... most people will (hopefully) read this via RSS at any rate.
Read More

The final solution (for generic IronPython delegates)...

Back once more...

I decided to spend ten minutes implementing a version of the last post which didn't require us to generate a function definition for a python method with a generic number of parameters... it's actually not too hard.

First off, the key to solving this problem is to generate a Type for our custom delegate - for performance reasons you would want to cache the results, but we won't do that here... Also I think it would be more apropriate for the first 0->9 parameters that we just return a generic Func delegate (generic Func & Proc delegates are going to be part of .Net 3.0, as mentioned here - At the moment I use something similar from the RhinoCommons library, have a look here)

So, first off we build a helper method for generating our delegate - just heavy use of emit here, based off an example on MSDN:

public Type CreateCustomDelegate(Type returnType, params Type[] parameterTypes)
AssemblyName assembly = new AssemblyName();
assembly.Name = "CreateCustomDelegateAssembly";

AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(assembly, AssemblyBuilderAccess.RunAndSave);

ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("TempModule");

TypeBuilder typeBuilder =

TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Class |

TypeAttributes.AnsiClass | TypeAttributes.AutoClass, typeof (MulticastDelegate));

ConstructorBuilder constructorBuilder =
MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
CallingConventions.Standard, new Type[] {typeof (object), typeof (int)});
constructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

MethodBuilder methodBuilder =

MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot |

MethodAttributes.Virtual, returnType, parameterTypes);
methodBuilder.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.Runtime);

return typeBuilder.CreateType();

Basically it goes through the motions of building a delegate type, derived from MulticastDelegate with our selected output type and parameter types...

Now to put that type to use:

public void WithCustomDelegate()
Dictionary context = new Dictionary();
context.Add("user", "alex");
context.Add("age", 26);

Proc testCore = delegate
List parameters = new List(context.Keys);
List values = new List(context.Values);

PythonEngine engine = new PythonEngine();

Type[] parameterTypes = new Type[context.Count];
for (int i = 0; i < parametertypes.length;="" i++)="" parametertypes[i]="typeof">

Type delegateType = CreateCustomDelegate(typeof (object), parameterTypes);

Type pythonEngine = typeof (PythonEngine);
MethodInfo genericMethod =
pythonEngine.GetMethod("CreateMethod", new Type[] {typeof (string), typeof (IList)});
MethodInfo method = genericMethod.MakeGenericMethod(delegateType);

object result =

new object[] {"return user + ' is ' + str(age) + ' years old'", parameters});

Delegate myDelegate = (Delegate) result;
Assert.AreEqual("alex is 26 years old", myDelegate.DynamicInvoke(values.ToArray()));

// try it with our two keys in the dictionary


// try it with another key in the dictionary

context.Add("monkey_colour", "brown");

Our test creates our custom delegate type for the number of parameters we have, and then we use that when invoking the
PythonEngine.CreateMethodTDelegate>(string statements, IListstring> parameters) method.

Here we have to do a little reflection, simply because we cant directly use a local variable of type "Type" as a generic type
parameter, so we grab the generic method, set the parameter and then invoke it.

However in the end, as you can see we get a real Delegate, which we can use DynamicInvoke upon.

This is actually pretty handy, and it's certainly a lot more script-friendly, compared to the alternative of forcing users to
look up values from context by index ie.

    context['user'] + ' is ' + str(context['age']) +
' years old'


    user + ' is ' + str(age) + ' years old'

I know what I prefer ;o)
Read More

IronPython is your friend - Part 2 - More IronPython and delegates….

Part  2 - More IronPython and delegates...

So we have briefly covererd strongly typed delegates and event handlers... But they make the assumption that we know what our arguments are at compile time, what if we dont?

Say for instance you're expression engine has a bag of contextual values being passed around in a dictionary, like this:

Dictionary context = new Dictionary();
context.Add("user", "alex");
context.Add("age", 26);

What if we want to use python to evaluate expressions against that context?... Say something like writing out "my name is alex and my age is 25" - the expression in python is easy enough, we can go:

my name is' + name + ? and my age is ? + str(age)

But how do we marry all these together... lets start exploring... ever noticed that delegates have a DynamicInvoke method with a signature like this?

object DynamicInvoke(params object[] parameters)

Perhaps we can try and use this to our advantage... lets see shall we?

First Attempt

[ExpectedException(typeof (ArgumentException), "T must be a concrete delegate, not MulticastDelegate or Delegate")]
public void LooseDelegateAndDynamicInvoke()
PythonEngine engine = new PythonEngine();

List parameters = new List(new string[] {"name", "age"});

Delegate func =
engine.CreateMethod("return ?my name is' + name + ? and my age is ? + str(age)", parameters);
string result = (string) func.DynamicInvoke("alex", 26);

Assert.AreEqual("my name is alex and my age is 25", result);

Sadly this doesn't work so well, seems the PythonEngine is looking for a concrete delegate... we could try giving it void delegate, but it does type checking on the number of parameters and their type, so we're a little stuck.

At this point I think the best answer is to actually build some code which generates the apropriate delegate type, based on the supplied dictionary, and then passing that onto the python engine, but there is another way... it's a little less elegant, but it's at least amusing:

Round 2

First of let's build a little helper method...

private static string GenerateFunction(string functionName, string[] parameters, string statements)
StringBuilder builder = new StringBuilder();

builder.AppendFormat("def {0}(", functionName);

for (int i = 0; i < parameters.length;="">
if (i > 0) builder.AppendFormat(", ");

builder.AppendFormat("):rn ");

builder.AppendFormat(statements.Replace("rn", "n").Replace("r", "n").Replace("n", "rn "));

return builder.ToString();

You can no doubt see where this is going - so given a statement like this:

GenerateFunction("func", new string[] { "user", "age" }, "return user + ' is ' + str(age) + ' years old'")

We'd end up with a string like this:

    def func(user, age):

        return user + ' is ' +
str(age) + ' years old'

And, moving on from there we build a test...
public void GeneratingPythonFunction()
Dictionary context = new Dictionary();
context.Add("user", "alex");
context.Add("age", 26);

List parameters = new List(context.Keys);
List values = new List(context.Values);

PythonEngine engine = new PythonEngine();

GenerateFunction("func", parameters.ToArray(), "return user + ' is ' + str(age) + ' years old'"));

PythonFunction func1 = engine.EvaluateAs("func");

GenerateFunction("func", parameters.ToArray(),
"return user + ' is ' + str(age+1) + ' years old next year'"));

PythonFunction func2 = engine.EvaluateAs("func");

object result1 = func1.Call(values.ToArray());
Assert.AreEqual("alex is 26 years old", result1);

object result2 = func2.Call(values.ToArray());
Assert.AreEqual("alex is 27 years old next year", result2);

Are there problems with this?... well there's a few, unsurprisingly!

  • PythonFunction's are not delegates.
  • String manipulation is a bit clunky
  • Because these are named functions, you probably want to lock against some object when generating the function, to avoid cross-threading issues...

Back to delegates

Aside from the problems it works alright, but what we really want is a delegate... those PythonFunctions don't give you the flexibility to substitute IronRuby in the future now do they?

So first off, lets declare a delegate suitable for our purposes.

[ThereBeDragons("Only use as a last resort")]
public delegate object UntypedDelegate(params object[] parameters);

Aint she a beauty ;o) - full credit for the ThereBeDragons attribute goes to Ayende, though it's probably not really warranted in this situation - now, let's rework the last test to use a delegate instead, a simple anonymous delegate will do the dirty work:

public void UntypedDelegateForPythonFunction()
Dictionary context = new Dictionary();
context.Add("user", "alex");
context.Add("age", 26);

List parameters = new List(context.Keys);
List values = new List(context.Values);

PythonEngine engine = new PythonEngine();

engine.Execute(GenerateFunction("func",parameters.ToArray(),"return user + ' is ' + str(age) + ' years old'"));

PythonFunction func1 = engine.EvaluateAs("func");

UntypedDelegate func1Delegate = delegate(object[] param)
return func1.Call(param);

object result1 = func1Delegate(values.ToArray());
Assert.AreEqual("alex is 26 years old", result1);

It's certainly better then passing around PythonFunction instances - though you need to be a little careful... there's a little quirk here, if we were to use:

object result1 = func1Delegate.DynamicInvoke(values.ToArray());

It's going to fail because the wrong number of arguments were supplied (it expects only 1, an array), so our delegate doesn't really behave like it has multiple parameters.. so to dynamically invoke this delegate we'd need to take special care, promoting the arguments into a second array like so:

object result1 = func1Delegate.DynamicInvoke(new object[] { values.ToArray()});

Next Time

When I get bored I'll write a version which doesn't require the clunky generator and post it up...

Next time I'll talk about writing classes which are python-friendly, riveting stuff eh? As you were.
Read More