Byte-run: a Python interpreter written in Python

Ned Batchelder ned at
Fri Jan 13 14:57:32 EST 2017

On Friday, January 13, 2017 at 12:09:52 PM UTC-5, Ian wrote:
> On Fri, Jan 13, 2017 at 3:46 AM, Steve D'Aprano
> <steve+python at> wrote:
> >
> >
> Neat. But not really surprising IMO that it can fit into 500 lines,
> since it doesn't handle compiling Python into bytecode (which is the
> hard part) and doesn't include libraries. There doesn't seem to be
> much purpose to this other than it being a toy project written for a
> book.

I can tell you what its purpose was: it was to verify my understanding
of the execution semantics of Python bytecode.  At the time,
analyzed code for possible branches by reading the bytecode.  There are
some very tricky bytecodes, and I wasn't sure that I understood what they

I figured that if I could implement a Python VM, then it would prove that
I understood it, or would shine a light on my misconceptions.

I thought perhaps someone had done it already.  I found Paul Swartz's vm2
code, which was a very similar idea. I refactored it, polished it up, and
extended it, and the result was byterun.

It served its purpose: although it didn't execute everything properly,
there were some twisty bits made clearer by the exercise.  Post-Script: no longer uses bytecode analysis, it uses AST analysis, which
works much better.

Since we're talking about possible purposes: Paul Swartz's original goal
was to sandbox Python execution. I'm not sure that was a realistic goal
for code like this, but that was his goal.


More information about the Python-list mailing list