Let’s build a Python!
Hi! 👋 Thanks for joining the workshop. Today we will vainly attempt to write an entire Python bytecode interpreter. We will surely fail! But along the way we’ll learn a couple of things about how Python is implemented.
This workshop is based on a class that we run at Bradfield, as part of our Languages and Compilers course, although I’ll be adapting it on the fly depending on the number and background of folk here. Wish me luck! 🤞
An asteroid has hit the earth! 🌏☄️ Oh no! All of the Python interpreters and .py files have been destroyed in the fire! 🔥 Somehow, the .pyc files remain unharmed. 🚒👨🚒 Your job is to write an interpreter to read and evaluate the surviving .pyc files, so that we can start rebuilding our tragically damaged civilization.
Download survivors.zip which has all of the .pyc files. Read the contents of a file, try to make sense of the bytecode instructions, and slowly build up your interpreter to support the new opcodes.
If you’re not sure where or how to start, interpreter.py has a skeleton, and solves the first problem.
The version of Python used is 2.7.10, which is the default Python on most OS X versions. It will help you a whole bunch to refer to a couple of key files in the CPython (reference) implementation of Python: opcode.h and ceval.c. You may even spend most of your time reading ceval.c! This is a kind of victory state, too.
Another useful resource is the
dis module, both for its functionality and its documentation.
Hopefully this exercise will raise a bunch of questions and start some interesting conversations! Please don’t hesitate to ask me about them. 🙂
Allison Kaptur has a couple of closely related resources: a book chapter in 500 Lines or Less called A Python Interpreter Written in Python and a talk from PyCon 2015 called Bytes in the Machine. These are both great places to start if you want to revise what we covered or revisit from a different angle.
An excellent more advanced resource is the videos from a short course by Philip Guo (pgbovine) on CPython internals. Watching and understanding these will make you a much more effective Python user.
Staying in touch
If you’d like to generally stay in touch, and receive updates from us on workshops, courses, computer science learning resources and the tech news that matters, we have a mailing list for that:
Finally, if you would like to dive deeper into computer science generally, but don’t know the best resources or overall plan, check out our microsite Teach Yourself Computer Science.