IronPython 2.0.1

I’m on vacation this week, but I wanted to quickly point out that we shipped IronPython v2.0.1 last Friday. This has been a performance focused release, as you can see via our 2.0 vs. 2.0.1 benchmarks. We have improved our PyStone performance by about 11.5% and our Richards performance by just over 4%. Thanks to Dino for the perf improvements and Dave for the great performance report.

Register now for Lang.NET Symposium 2009

We’re now accepting registrations and talk proposals for the 2009 Lang.NET Symposium, which will be held on the Microsoft campus April 14th-16th.

For those who aren’t familiar with this event, the Lang.NET Symposium is an opportunity for programming language designers and researchers from industry and academia to get together and basically geek out for two and a half days. And it’s not just Microsoft language wonks like Anders Hejlsberg or platform wonks like Don Box. For example, last year we had Dan Ingalls from Sun, Gilad Bracha from Cadence, John Rose from OpenJDK and Wez Furlong from the PHP Project among many others.

If you’re curious about the kind of talks we typically have at Lang.NET, the talks from 2006 and 2008 are available online.

I’ve attended both previous Lang.NET Symposiums and spoke at last year’s event, but this will be my first as a part of the team driving the event itself. We’ve expanded a little this year, though we’re still a small conference so the signal-to-noise ratio should still be very high. We barely fit in the Platform Lab last year, so we moved this year’s event to Microsoft Research’s sweet new building #99.

This year, we’re very excited to be joined at the hip with Chris Sells’ DSL Developers Conference. Those guys over in CSD are working on some cool language stuff of their own, so it just made sense to hold the two events in the same space, one right after the other. Note, registration for each event is separate. You’re welcome to attend both – we’d love it if you did – just make sure you register for both.

As in the past, there is no cost to attend Lang.NET. You get yourself here and find place to stay and we’ll take care of the content, meals and beers in the evening. Just make sure you let us know you’re coming and what, if anything, you’re interested in getting up and talking about by registering at the Lang.NET Symposium website.

IronPython and CodeDOM: Dynamically Compiling C# Files

As part of my series on using IronPython with WPF 1, I built an extension method in C# that does dynamic member resolution on WPF FrameworkElements. The upshot of this code is that I can write win1.listbox1 instead of win1.FindName('listbox1') when using WPF objects from Python or any DLR language. Convenient, right?

The problem with this approach is that the C# extension method gets compiled into an assembly that’s bound to a specific version of the DLR. I recently started experimenting with a more recent build of IronPython and I couldn’t load the extension method assembly due to a conflict between the different versions of Microsoft.Scripting.dll. Of course, I could have simply re-compiled the assembly against the new bits, but that would mean every time I moved to a new version of IronPython, I’d have to recompile. Worse, it would limit my ability to run multiple versions of IronPython on my machine at once. I currently have three – count ‘em, three – copies of IronPython installed: 2.0 RTM, nightly build version 46242, and an internal version without the mangled namespaces of our public CodePlex releases. Having to manage multiple copies of my extension assembly would get annoying very quickly.

Instead of adding a reference to the compiled assembly, what if I could add a reference to a C# file directly? Kinda like how adding references to Python files works, but for statically compiled C#. That would let me write code like the following, which falls back to adding a reference to the C# file directly if adding a reference to the compiled assembly fails.

try:
  clr.AddReference('Microsoft.Scripting.Extension.Wpf.dll')
except:
  import codedom
  codedom.add_reference_cs_file('FrameworkElementExtension.cs',
    ['System', 'WindowsBase', 'PresentationFramework',
     'PresentationCore', 'Microsoft.Scripting'])

Since this technique uses CodeDOM, I decided to encapsulate the code in a Python module named codedom, which is frankly pretty simple. As a shout-out to my pals on the VB team, I broke compiling out into it’s own separate function so I could easily support adding VB as well as C# files.

def compile(prov, file, references):
  cp = CompilerParameters()
  cp.GenerateInMemory = True
  for ref in references:
    a = Assembly.LoadWithPartialName(ref)
    cp.ReferencedAssemblies.Add(a.Location)
  cr = prov.CompileAssemblyFromFile(cp, file)
  if cr.Errors.Count > 0:
    raise Exception(cr.Errors)
  return cr.CompiledAssembly

def add_reference_cs_file(file, references):
  clr.AddReference(compile(CSharpCodeProvider(), file, references))

def add_reference_vb_file(file, references):
  clr.AddReference(compile(VBCodeProvider(), file, references))

The compile function uses a CodeDOM provider, which provides a convenient function to compile an assembly from a single file. The only tricky part was adding the references correctly. Of the five references in this example, the only one CodeDOM can locate automatically is System.dll. For the others, it appears that CodeDOM needs the full path to the assembly in question.

Of course, hard-coding the assembly paths in my script would be too fragile, so instead I use partial names. I load each referenced assembly via Assembly.LoadWithPartialName then pass it’s Location to the CodeDOM provider via the CompilerParameters object. I realize that loading an assembly just to find its location it kind of overkill but a) I couldn’t find another mechanism to locate an assemblies location given only a partial name and b) I’m going to be loading the referenced assemblies when I load the generated assembly anyway, so I figured it loading them to find their location wasn’t a big deal. Note, that typically you’re used to passing a string to clr.AddReference, but it also can accept an assembly object directly.

Of course, this approach isn’t what you would call “fast”. Loading the pre-compiled assembly is much, much faster than compiling the C# file on the fly. But I figure slow code is better than code that doesn’t work at all. Besides, the way the code is written, I only take the extra compile hit if the pre-compiled assembly won’t load.

I stuck my codedom.py file up on my SkyDrive. Feel free to leverage as you need.


  1. I had to put that series on the back burner in part because the December update to Windows Live totally broke my WPF photo viewing app. I’ve got a new WPF app I’m working on, but I’m not quite ready to blog about it yet.

Thoughts from Inauguration Day

A few random thoughts on yesterday:

Though some in the progressive blogosphere disagree, I thought Obama’s inaugural speech was very good. His start, contrasting taking the oath of office “during rising tides of prosperity and the still waters of peace” (as now-former President Bush did 1) versus taking the oath as he does “amidst gathering clouds and raging storms” really set the tone for the whole speech. With Bush sitting right there – mere feet from the podium – Obama categorically rejected almost every policy of the Bush administration. When they cut to a shot of Bush in the middle, he didn’t seem to be enjoying Obama’s speech very much. In a way, I was reminded of Steven Colbert’s epic performance at the White House Correspondents Dinner a few years ago. Of course, the difference was that Colbert was speaking truth to power while Obama is speaking truth from power.

Speaking of Ex-President Bush (man, I love writing that) I can say a lot of negative things about him, but I will grudgingly commend him on handling the transition. However, it does feel like it’s grading him on a massive curve: “Sure he’s been an abysmal president, but at least he was able to peacefully hand over the reigns of power without fucking that up too.”

I felt sorry for Elizabeth Alexander, the poet who had to speak after President Obama’s inaugural speech. Everyone started heading for the exits after Obama was done. I hope those early exiters could hear Reverend Lowery’s benediction, it was awesome. I realize Reverend Warren was a controversial pick to deliver the invocation – certainly, I’m not a fan – but I respect Obama for being inclusive of all-viewpoints. And while having a quartet with the likes of Itzhak Perlman and Yo-Yo Ma was awesome, I especially liked John Williams’ inaugural piece “Air and Simple Gifts”. But wouldn’t the theme to Superman have been more appropriate? 😄

Finally, I skipped the rest of the pre and post inauguration analysis and commentary other than the Countdown podcast which I listen to on my way to work most days. Frankly, I didn’t need to be reminded of what an historic day it was. Res ipsa loquitur.


  1. The Onion was disturbingly prescient with their article eight years ago “Bush: ‘Our Long National Nightmare Of Peace And Prosperity Is Finally Over’”

Kid Programming with Kodu Coming to Xbox 360

As I’ve written before, I originally got the programming bug from a desire to build my own text adventure games. with significant influence from my dad. Now that I’m a father myself, I want my kids to have a similar opportunity, even if they never choose to go into the “family business”.

kodu_guy

Of course, the technology has moved on significantly since the days of “You are in a maze of twisty little passages, all alike”. At CES yesterday, Microsoft announced Kodu which I’ve written about before under it’s original name Boku. Kodu came out of Microsoft Research as a tool for teaching kids how to program. The programming language is very visual and iconic and you use the Xbox controller exclusively for all input. Here’s a screenshot:

To demo Kodu at CES, Robbie Bach brought a 12 year old girl named Sparrow up on stage to demo. I showed the video to my kids this morning and they went gaga for it. They’re a little young – Patrick turns 6 next month and Riley turns 4 later this year – but I think they’ll be able to get the hang of it (with a little help from dad). Below is the video of the CES demo, and there are more Kodu videos at On10 (Matthew MacLaurin on Kodu and Watch Kodu in Action).

Personally, I think this is brilliant. I have been eagerly waiting a change to play this with my kids for over a year, so I’m very excited that they’re bringing this to market. Seriously, Halo Wars just got bumped to the #2 slot on my “Most Anticipated Xbox Games of 2009” list.

I’m most interested in how these creations will be shared online. I couldn’t find any details, but Robbie specifically said “And on Xbox Live they can distribute and share those finished games with other people.” Will there be a charge? (“normal” Community Games cost between $2.50 and $10 a pop) How will parental controls affect shared Kodu games? I guess those details will come closer to release.