Python on the Smartphone

Before you get all worked up thanks to the sensationalistic title, Jim Hugunin and Guido have nothing to do with this. After a conversation with Partha, I got all excited about the idea of porting my college project to the Smartphone with VS 2005’s new C++ support for devices.


For those among you who didn’t follow my previous blog avidly (and shame on you!), I hacked together a something-like-a-virtual-machine for my college finishing project. You can still see some blog posts I wrote back then (1, 2, 3) In the beginning, I envisioned it to be this be-all virtual machine which solved several issues relating to functional and dynamic languages on virtual machines and taking a stab at world peace in the process.

After a 3-week hack job, what I wound up with was something that could do a half-decent job of executing Python (since Python was the only language we wrote a compiler for – instead of the 3-4 languages we had planned originally). The interesting thing about this VM-rewrite was that Smoke (as the VM was called) allocated stack frames on the heap instead of using the C stack. While making our VM really slow when compared to the mainline virtual machines, it also gave us flexibility for doing nifty things like continuations and closures and having really deep recursive functions. For my project review, I showed off a recursive function calling itself several million times. When you have 2GB to play with, a lot of things become possible :-).

Enter Visual Studio for Devices

After joining Microsoft, I had pretty much forgotten about Smoke..until yesterday. After talking to Partha, I dusted off my old source code and started hacking away. I was prepared for the worst – after all, I was porting to a Magneto Smartphone, which is not only well…*not* a computer, but also runs on a totally different architecture (ARM).

To my pleasant surprise, I found that the code recompiled almost cleanly. I had to make very trivial fixes to the code. Fixes like

– GetProcAddress on WinCE has a Unicode version.
– On WinCE, there is no concept of a drive letter. All paths start with the root drive.

Within 10-15 minutes, I had the code compiling and running.

Taking it for a spin

I fired up a trusty editor and typed in the following code


def fact(i):           

            if i == 0:
                        return 1
                        return i * fact(i-1)
print fact(10)

We had written our Python compiler in Python itself. Unfortunately, the compiler needs the actual CPython runtime as we use modules that Smoke can’t handle yet. After compiling the Python code to a .smoke file, I copied the compiled bytecode file over to the device using ActiveSync ( <plug>using the new uber-cool feature of VS2005’s Device Emulator Manager which lets you hook up your emulator to ActiveSync and work with it like a normal device</plug>).

And voila! It ran perfectly at first try. Since the Smartphone doesn’t have a console, I had to be content with seeing output inside Visual Studio. But hey – I’m not complaining :-).



Comments (9)

  1. Sridhar Ratna says:

    For a long time i have been thinking to get a series 60 phone, but with decent gprs speed range. That day will hopefully come soon 🙂 My python on mobile hacking begins right there.

    Note: 6600 sucks for GPRS with the max speed of 40kbps.

  2. sriram says:

    Sridhar – I would be a lot happier if you buy a Windows Mobile phone 🙂

  3. >GetProcAddress on WinCE has a Unicode version

    does that mean WinCE lets you export DLL entry point names in languages other than english?

  4. sriram says:

    Kaushik – good point! I have to dig around here. But do keep in mind that WinCE shares the same PE file format as the desktop (or that’s what I think anyway).

    I never understood why the desktop didn’t let you export non-ANSI export names

  5. >I never understood why the desktop didn’t let you export non-ANSI export names

    Probably because UNICODE export / import sections would unnecessarily (because we all code in English) bloat the image on disk… but isn’t this is more important on WinCE!

  6. Nino.Mobile says:

    &amp;nbsp;Wooo Hooo!&amp;nbsp;&amp;nbsp; TMM 100.&amp;nbsp; Thought I&amp;rsquo;d never get here at the rate I post &amp;lt;g&amp;gt;;…

  7. sriram says:

    We all code in English? Assumptions like that in the past have caused us major harm 🙂

  8. sriram says:

    Kaushik – Got this from the horse’s mouth itself 🙂

    The Windows CE OS consumes the same PE file format as the desktop, which specifies ASCII names in export and import tables. Therefore, no, CE doesn’t support UNICODE exports. GetProcAddressW will accept UNICODE names, but will simply convert them to ASCII internally before performing a comparison on export table entry names.

  9. Sridhar Ratna says:

    would be nice to have email notification on replies ..