Mono and IronPython

Diane Trout

Socal Piggies

2006 Nov 16

Why?

Once upon a time libraries could be written in different languages and combined together with a magical thing called a linker.

(well except that C/Fortran array thing, and C++'s name mangling...)

Then interpreted scripting languages came along...

Scripting

All of the various scripting languages can talk to C libraries, but they can't talk to each other.

As far as I can tell for one scripting language to use code written in another scripting language is something like XML-RPC, SOAP, or some other custom network layer.

Wish

I was hoping that there would be a common mutli-language bytecode interpreter that would let different scripting languages call libraries written in other languages.

For example it would be really useful to be able to use some CPAN in my python programs. (Not to mention making it easier for people to transition from one language to another.)

.NET

From http://en.wikipedia.org/wiki/.NET_Framework

JVM vs CLR

.NET always uses JIT, JVM is optional "The namespaces provided in the .Net Framework closely resemble the platform packages in Java EE API Specification both in style and invocation."

Comparison of C# and Java

http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java

(You can read that yourself)

The quick summary, is C# learned from the experience of "Java and Delphi". The wikipedia article suggested that C# is more feature-full than Java. But possibly at the expense of trying to be too trendy.

Processs

To me the big interesting difference is how Java and .NET development are handled.

Sun has a community process where people can submit proposals though Sun retains veto rights.

Microsoft handed the core portions of .NET to ECMA for standardization.

A standard made it easier for Mono.

Gettting Mono

(Or at least downloading mono)

The latest release builds are at

http://www.mono-project.com/Downloads

Unfortunately for me the OS X version doesn't come with GTK#. Also the assumption for Mono on OS X is that you'll use Gtk under X11.

Native GTK on OS X

http://developer.imendio.com/projects/gtk-macosx/

Has a useful little script to build all the dependencies for Gtk and then builds a native version for OS X

curl -O http://people.imendio.com/richard/gtk-osx/files/build-gtk.sh
# handles XML-Parser & pkg-config
./build-gtk bootstrap
./build-gtk build

For some reason they defaulted to installing everything to /opt/gtk

Mono SVN

It was a little bit annoying to find the page describing how to check mono components out

http://www.mono-project.com/AnonSVN

svn co svn://svn.myrealbox.com/source/trunk/$MODULE_NAME

Gtk#

The project page is at http://gtk-sharp.sourceforge.net/ but the source is at

svn co svn://svn.myrealbox.com/source/trunk/gtk-sharp

Gtk# replaced the traditional gnome ./autogen.sh with

./bootstrap-2.10 --prefix=<destdir>
make
make install

I tried my little Hello.cs program to see if Gtk# was installed--

It failed.

Global Assembly Cache

After reading about the Global Assembly Cache

Buried deep in /opt/gtk/lib/mono/gac/gtk-sharp/2.10.0.0__35e10195dab3c99f was a magic little file, gtk-sharp.dll.config

<configuration>
  <dllmap dll="libglib-2.0-0.dll" target="libglib-2.0.0.dylib"/>
  <dllmap dll="libgobject-2.0-0.dll" target="libgobject-2.0.0.dylib"/>
  <dllmap dll="libatk-1.0-0.dll" target="libatk-1.0.0.dylib"/>
  <dllmap dll="libgtk-win32-2.0-0.dll" target="libgtk-x11-2.0.0.dylib"/>
</configuration>

more docs on dllmap Interop with Native Libraries

libgtk-quartz

I changed the last line of gtk-sharp.dll.conf
  • from target="libgtk-x11-2.0.0.dylib"
  • to target="libgtk-quartz-2.0.0.dylib"
  • (and added a reference from libgtk-x11 to libgtk-quartz).

It died again, this time not finding the right library.

So why did the gtk on os x people pick /opt/gtk again?

export LD_LIBRARY_PATH=/opt/gtk/lib
Then it found the library, but after a brief moment crashed.

Getting Mono, the hard way

I grabbed one of the snapshots from http://mono.ximian.com/daily/ and tried building.

Oh, its self hosting, you need a C# compiler in order to compile the C# compiler.
There's a Makefile target get-monolite-latest
Except it uses wget, OS X only has curl.

Getting Monolite, the hard way

get-monolite-latest, by hand

cd ${monosrc}/mcs/class/lib
curl -O http://mono.ximian.com/daily/monolite-latest.tar.gz
tar xzvf monolite-latest.tar.gz
mv monolite-[0-9]* monolite
cd ${monosrc}/mono
make
What? The build hung?

Mono on Intel OS X

There's a change in the defines for the mach thread handling in the 10.4u sdk (XCode 2.4) needed for Intel 64 bit, which breaks mono.

Allan Hsu submitted a patch which fixed this

http://lists.ximian.com/pipermail/mono-devel-list/2006-October/021060.html
gasp, my hello.cs Gtk# program actually runs.

On To Iron Python

IronPython 1.0.1 is included in the mono distributions, but since I built from source...

http://www.codeplex.com/IronPython
http://fepy.sourceforge.net/

White IronPython

White Text

White IronPython

Two solutions

Backspace

Once I could see what I was doing I thought I might try to explore a bit.

Until I made a mistake 6 characters in and hit backspace--and got ^? instead of an erased character.

Backspace, Fixes

  1. hack IronPythonConsole/SuperConsole.cs to respond to DEL, and report to the mailing lists.
  2. flip the "Delete sends Backspace" option in terminal
  • worked some of the time.
  • snap and go play World of Warcraft for a couple weeks.
  1. discover that my .screenrc, which was carried over from my linux boxes was forcing backspace to be DEL.
-# On Debian GNU/Linux, `<--' (Backspace key) should send char `\177':
-bindkey -k kb stuff "\177"

It works!

IronPython, Snapshots

Since IronPython is tied to VisualStudio and uses some VisualStudio specific source code control system I eventually wanted more recent versions than 1.0.1

Each download link asks you to agree to the license, and provides a copy of the whole source tree.

Yep, no diffs.

GUIs

GUIs

Gtk# HelloConsole.cs

Gtk-Sharp Project Information Gtk-Sharp Tutorial

HelloConsole.cs:

using System;
using Gtk;

public class GtkHelloWorld {
  public static void Main() {
    Console.WriteLine("HelloWorld");
  }
}
// build with
// mcs -pkg:gtk-sharp-2.0 helloword.cs

Pkg-Config

Mono has a heavy reliance on pkg-config, a useful tool which appears to have popped out of gnome.

The first time I tried building that C# program it failed being unable to find gtk-sharp-2.0

Until I did:

export PKG_CONFIG_PATH=/opt/gtk/lib/pkgconfig

Gtk# HelloConsole.py

HelloConsole.py:

import sys
import System
import clr
clr.AddReference("gtk-sharp")
import Gtk

def main(argv):
  System.Console.WriteLine("HelloConsole")

if __name__ == "__main__":
  sys.exit(main(sys.argv[1:]))

Gtk# HelloGtk.cs

HelloGtk.cs:

Application.Init();
//Create the Window
Window myWin = new Window("Hello");
myWin.Resize(200,200);

//Create a label and put some text in it.
Label myLabel = new Label();
myLabel.Text = "Hello World!!!!";

//Add the label to the form
myWin.Add(myLabel);
myWin.ShowAll();
Application.Run();

Gtk# HelloGtk.py

HelloGtk.py:

Gtk.Application.Init()
# Create Window
win = Gtk.Window("hello")
win.Resize(200,200)

# Create Label
label = Gtk.Label("Hello!")

win.Add(label)
win.ShowAll()
Gtk.Application.Run()

Gtk# HelloEvent.py

HelloEvent.py:

def click(obj, args):
  print "Goodbye"

def quit(obj, args):
  Gtk.Application.Quit()

# and replacing the Label stuff
win.DeleteEvent += quit

# Create Button
button = Gtk.Button("Hello!")
button.Clicked += click

Silly Example

Performance

Grabbed some random benchmarks off of Computer Language Shootout

startup.py

partial-sums.py

cheap-concurrency.py

binary-trees.py

results.txt

More detail IronPython vs Python2.4

Performance

shootout/shootout.png
jython failed the cheap-concurrency test, and so I gave it a score of -10 seconds

Performance

shootout/PyVsIPy.png