Byte-run: a Python interpreter written in Python

Ned Batchelder ned at nedbatchelder.com
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 pearwood.info> wrote:
> >
> > http://aosabook.org/en/500L/a-python-interpreter-written-in-python.html
> 
> 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, coverage.py
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
did.

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:
coverage.py 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.

--Ned.



More information about the Python-list mailing list