Philip Guo (Phil Guo, Philip J. Guo, Philip Jia Guo, pgbovine)

CPython internals: A ten-hour codewalk through the Python interpreter source code

Summary
Here are nine lectures walking through the internals of CPython, the canonical Python interpreter implemented in C. They were from a dynamic programming languages course that I taught in Fall 2014 at the University of Rochester. The format isn't ideal, but I haven't seen this level of detail about CPython presented online, so I wanted to share these videos.

Here are nine lectures I gave on CPython internals as part of my course on dynamic programming languages in Fall 2014. These videos are meant as a reference, not as a shining exemplar of pedagogy. If I had infinitely more time, I would edit these videos into 5- to 10-minute chunks and link them to the corresponding source code. More importantly, I would've gone back in time and planned my lectures to maximize “video chunkability” ... and used a real microphone ... and added in-situ quiz questions ... and made them more interactive. But in the spirit of CRAPL, I just recorded my lectures using Camtasia on an MS Surface Pro 3 tablet and put up the unedited raw videos.

If you want a polished introduction to the high-level ideas behind CPython, I highly recommend watching Allison Kaptur's PyCon 2015 talk on Byterun.

The purpose of these lectures is to give a technical overview of the core components of CPython. Although it does not strive to be comprehensive, once you understand these lectures, you should be able to understand the rest of the CPython code base.

If you want to follow along with the code walkthroughs, download and unzip the Python 2.7.8 source code. And to see the accompanying homework assignments, visit the course webpage.

Listen to a one-hour podcast describing this course:

Lecture 1 - Interpreter and source code overview

Lecture 2 - Opcodes and main interpreter loop

Note: There is a bug in this lecture regarding the compile() function, which I remedy at the beginning of Lecture 3.

This lecture references:

Lecture 3 - Frames, function calls, and scope

  • Include/code.h
  • Include/frameobject.h
  • Objects/frameobject.c
  • Python/ceval.c

Lecture 4 - PyObject: The core Python object

Lecture 5 - Example Python data types

  • Objects/abstract.c
  • Include/stringobject.h
  • Objects/stringobject.c

Lecture 6 - Code objects, function objects, and closures

  • Include/code.h
  • Include/funcobject.h
  • Objects/codeobject.c
  • Objects/funcobject.c

Lecture 7 - Iterators

  • Include/iterobject.h
  • Objects/iterobject.c
  • Objects/abstract.c
  • Python/ceval.c

Lecture 8 - User-defined classes and objects

  • Include/classobject.h
  • Objects/classobject.c
  • Objects/abstract.c
  • Python/ceval.c

Lecture 9 - Generators

  • Include/genobject.h
  • Objects/genobject.c
  • Python/ceval.c
Created: 2014-10-08
Last modified: 2015-05-31