CPython is the reference implementation of the Python programming language, written in C. This document provides an architectural overview of CPython's core systems, covering Python 3.13, 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: Doc/whatsnew/3.14.rst48-71 Doc/whatsnew/3.15.rst48-100
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/whatsnew/3.14.rst63-142 Doc/whatsnew/3.15.rst59-100
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/whatsnew/3.14.rst108-112
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. This tier includes Adaptive Specialization, where instructions are "quickened" into specialized forms after observing specific types at runtime. In 3.14+, this includes a new tail-calling interpreter Doc/whatsnew/3.14.rst108 In 3.15, the official Windows 64-bit binaries use this tail-calling interpreter by default Doc/whatsnew/3.15.rst96-97
When a loop becomes "hot", the Tier 2 optimizer translates the bytecode into a trace of micro-operations (UOps). These UOps allow for more aggressive optimizations like guard elimination.
The JIT compiler (Python/jit.c) transforms Tier 2 UOp traces into native machine code. In Python 3.13, a basic JIT was added. In Python 3.14, binary releases for Windows and macOS officially support the experimental JIT Doc/whatsnew/3.14.rst138-139 Python 3.15 further significantly upgraded the JIT compiler Doc/whatsnew/3.15.rst95
Sources: InternalDocs/README.md40-44 Doc/whatsnew/3.14.rst108-139 Doc/whatsnew/3.15.rst95-97
All data in CPython is represented as objects. Every object has an identity, a type, and a value.
CPython pre-allocates certain frequently used objects as singletons to save memory and time.
_Py_small_ints_INIT Include/internal/pycore_runtime_init_generated.h15-227_Py_global_strings Include/internal/pycore_global_strings.h31-201_PyUnicode_InitStaticStrings Include/internal/pycore_unicodeobject_generated.h12-165CPython uses a reference-counting scheme with an optional generational garbage collector.
frozendict built-in type Doc/whatsnew/3.15.rst70-71 and a dedicated sentinel type Doc/whatsnew/3.15.rst72-73Sources: Doc/whatsnew/3.14.rst111 Doc/whatsnew/3.15.rst70-73 Include/internal/pycore_runtime_init_generated.h15-227 Include/internal/pycore_global_strings.h31-201
The CPython runtime manages the lifecycle of the interpreter and its threads.
Sources: Doc/whatsnew/3.14.rst68-135 Doc/whatsnew/3.15.rst91-93
compression.zstd module was introduced in 3.14 Doc/whatsnew/3.14.rst74-116Lib/pdb.py Lib/pdb.py1-116 and relies on bdb.py Lib/bdb.py1Tachyon high-frequency statistical sampling profiler Doc/whatsnew/3.15.rst74-77lazy keyword defers module loading until first use Doc/whatsnew/3.15.rst107-149Sources: Doc/whatsnew/3.14.rst74-168 Doc/whatsnew/3.15.rst74-149 Lib/pdb.py1-116
CPython uses an autoconf-based build system on POSIX and MSBuild on Windows.
Diagram: Build System Components and Configuration
The current development version is defined in configure.ac as 3.16 configure.ac13 The core interpreter on Windows is managed via pythoncore.vcxproj PCbuild/pythoncore.vcxproj1-72
Sources: configure.ac1-18 PCbuild/pythoncore.vcxproj1-72 Doc/using/configure.rst1-43
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.