ls lib/python-in-c/

Building a Python in C

lib / Building a Python in C

We write minipy, an interpreter for a subset of Python, in C: from an empty file to a bytecode VM with reference counting and a cycle collector, each piece set beside the way CPython 3.15 does it.

This course is for readers who know Python well and can read C: pointers, structs, malloc. We build minipy, an interpreter for a subset of Python, one piece per lesson, and after each piece we read how CPython 3.15 does the same job, in its source at tag v3.15.2. The code for every lesson is in the minipy repository, tagged per lesson: C11 and a Makefile, tested with Apple clang on macOS.

minipy will run ints of any size, floats, strings, lists, tuples, dicts, functions with closures, classes with single inheritance, exceptions and generators. It will leave out imports of Python modules, async, f-strings and bytes, multiple inheritance, threads and a JIT. Where it differs from CPython on code they both accept, the lesson says so.

Reading source

Objects

Memory

Bytecode

The C boundary and speed