Inside Python and PHP at OSCON 2012

Can I tell you guys something? Great. Thanks. You’re so swell.

My name is Tom and I’m addicted to compilers. This is the precautionary tale of of how I got hooked.

I started learning Python towards the end of my high school education, around the (CPython) 2.2/2.3 days. Back then, you couldn’t use try/except/finally in the same way that you would in a language like Java i.e.:

try {
  fudd.killTheWabbit(new SpearAndMagicHelmet());
}
catch (BugsBunnyException e) {
  // TODO log things
}
finally {
  return myLove;
}

The equivalent in Python looked something like this right up until version 2.4:

try:
  try:
    fudd.killTheWabbit(SpearAndMagicHelmet())
  except BugsBunnyException:
    # TODO log things
    pass
finally:
  return myLove

Yep, nested try statements. Yuck. I hated that extra level of indentation. It caused me profound sadness every time I had to deal with exceptions.

So I was fortunate to be watching the Python-Dev mailing list at  a time when discussion was starting around the possibility of implementing PEP 341 — “Unifying try/except and try/finally” — using the new Abstract Syntax Tree for CPython 2.5. Intrigued with compilers as I was/am & excited at the idea of fixing something that irked me, I had a quick read of Georg’s proposal & shot a quick email off to Python-Dev to see if they’d let me have a shot at implementing try/except/finally.

I was pretty green when it came to the Python code base and it took me a lot of time to figure out where I needed to change things, but the guys on Python-Dev (thanks Nick and Neal!) were really helpful and I eventually found my way to a working solution. Okay, so my initial patch also leaked memory like a sieve, but that in turn led to a better API for the arenas used to manage memory. So hey, even my bugs were helping to make Python better for everybody!

Why am I telling you all of this, dear reader? Well, on Thursday I’ll be presenting at OSCON about a way to add your own syntax to Python using an approach very similar to the one I used to implement the unified try/except/finally syntax. For me, it’s a walk down memory lane to glory days long past. For you, this is a perfect opportunity to get a feel for the code behind CPython’s bytecode compiler. It’s also a great opportunity for compiler newbies with an understanding of some basic theory who’re looking to see what things look like in a “real” bytecode compiler.

As a special, last-minute bonus I’ll also be giving an additional presentation as to how you can do the same thing in PHP. If you’re interested in seeing how similar concepts are implemented differently in two separate, real-world code bases, I’d suggest you might want to make it along to both presentations. There should only be a little bit of overlap in content — largely around terminology and some basic compiler theory.

If it seems like you could stand to hear more, come along on Thursday, July 19th at 1:40pm in E144 for Inside PHP, or at 5 pm in room D135 the same day for my second talk: Inside Python. Both sessions will be more of a quick & brutal dip-of-the-toe into each respective code base than a deep & thorough dive, though there will be live coding in both talks and thus ample opportunity to heckle me for my mistakes.

And hey, hopefully you’ll come away with a better understanding of PHP, CPython and compilers in general!

tom@newrelic.com'

View posts by .

Interested in writing for New Relic Blog? Send us a pitch!