CPython is the reference implementation of the Python programming language, written in C. This document provides an architectural overview of CPython's core systems for Python 3.14 (released October 2025) and 3.15 (in development).
Major architectural components:
PyObject-based type hierarchy with PyTypeObject metaclass, reference counting, and cyclic garbage collection._PyRuntimeState → PyInterpreterState → PyThreadState), Global Interpreter Lock (GIL) implementation, and multi-interpreter support.Navigation to detailed subsystems:
Sources: Include/patchlevel.h23-30 InternalDocs/README.md1-66 Doc/reference/datamodel.rst1-120
Diagram: CPython Architecture - Data Flow and Key Code Symbols
This diagram maps the flow of Python code through CPython's major subsystems, annotated with actual function names, file paths, and data structures from the codebase.
Sources: InternalDocs/README.md17-66 Doc/reference/lexical_analysis.rst10-23 Doc/reference/datamodel.rst9-65 Doc/library/dis.rst17-43
CPython's bytecode system is defined in a single source of truth: Python/bytecodes.c. This file uses a DSL to define instructions, which are then processed by Python-based generators to produce the C code used by the interpreter and optimizer.
Diagram: Bytecode DSL to Generated Code Pipeline
Instruction Definition Macros:
| Macro | Purpose |
|---|---|
inst(name, stack_effect) | Defines a Tier 1 bytecode instruction. |
op(name, stack_effect) | Defines a Micro-operation (UOp) for the Tier 2 optimizer. |
macro(name) | Combines multiple micro-ops into a single Tier 1 instruction. |
family(name, ...) | Defines a specialization family for adaptive bytecode. |
Sources: InternalDocs/README.md31-40 Doc/library/dis.rst38-44 Python/bltinmodule.c7-8
CPython implements a multi-tier execution strategy to balance fast startup with high-performance steady-state execution.
The core evaluation loop is _PyEval_EvalFrameDefault in Python/ceval.c. It processes standard bytecode instructions. This tier includes Adaptive Specialization, where instructions like LOAD_ATTR are "quickened" into specialized forms (e.g., LOAD_ATTR_INSTANCE_VALUE) after observing specific types at runtime.
When a loop becomes "hot" (monitored by JUMP_BACKWARD counters), the Tier 2 optimizer (Python/optimizer.c) translates the bytecode into a trace of micro-operations (UOps). These UOps are simpler than bytecode and allow for more aggressive optimizations like guard elimination and constant propagation.
If enabled, the JIT compiler (Python/jit.c) transforms Tier 2 UOp traces into native machine code using a "copy-and-patch" technique. This avoids the overhead of the interpreter dispatch loop for hot code paths.
Sources: InternalDocs/README.md40-44 Doc/library/dis.rst33-53 Python/bltinmodule.c102-106
All data in CPython is represented as objects. Every object has an identity, a type, and a value.
id().len()); retrieved via type().list) or immutable (value is fixed, like int or str).The interpreter includes several fundamental built-in types:
int, float, complex.list, tuple, range, str, bytes.dict.set, frozenset.None, True, False, NotImplemented, Ellipsis (...).Sources: Doc/reference/datamodel.rst9-55 Doc/library/stdtypes.rst10-26 Doc/library/functions.rst7-41 Doc/reference/expressions.rst79-87
The CPython runtime manages the lifecycle of the interpreter and its threads.
Sources: Doc/glossary.rst145-154 Doc/reference/datamodel.rst57-74 Python/bltinmodule.c204-207 InternalDocs/README.md46-52
CPython provides a rich set of built-in functions and a comprehensive standard library.
Functions like abs(), len(), print(), and __import__() are always available and implemented in C for performance. For example, __build_class__ handles the logic for creating new class objects at runtime.
The library spans from core system interfaces to high-level data formats:
os, io, pathlib.threading, asyncio, multiprocessing.json, pickle, csv.unittest, dis, pdb.Sources: Doc/library/functions.rst10-41 Python/bltinmodule.c101-135 Doc/library/stdtypes.rst1-17 Doc/glossary.rst28-39
Refresh this wiki
This wiki was recently refreshed. Please wait 7 days to refresh again.