top band

Saturday 1:40 p.m.–2:25 p.m.

Bytes in the Machine: Inside the CPython interpreter

Allison Kaptur

Audience level:
Python Internals


Have you ever wondered how the CPython interpreter works? Do you know where to find a 1,500 line switch statement in CPython? I'll talk about the structure of the interpreter that we all use every day by explaining how Ned Batchelder and I chased down a mysterious bug in Byterun, a Python interpreter written in Python. We'll also see visualizations of the VM as it executes your code.


### Introduction / A mysterious bug An outline of the Python interpreter in Python that Ned Batchelder and I were writing, the reason we set out to write it, and a mysterious bug we encountered ### Into the machine #### Introduction to bytecode - The definition of bytecode as an internal representation of Python code to the interpreter - What it means to talk about "compiling" Python code when Python is an "interpreted" language - Using `dis` to understand bytecode #### The VM is a stack machine - Discussing the virtual machine as a stack machine - Why bytecodes like `BINARY_MOD` don't have arguments - What "dynamic" means #### Executing Bytecode - The main loop of the CPython interpreter is a 1,500 line switch statement! - Visualizations of the stack as code executes ### Resolving the bug At this point I'll reveal the misunderstanding we had when first writing the interpreter ### Conclusion I'll close by restating what we learned and drawing analogies to other systems, if time allows.
bottom band background