CPython is the reference implementation of the Python programming language, written in C. This document provides a high-level architectural overview of CPython (including milestones from 3.13 through 3.15), outlining its main subsystems and their interrelations.
The structure of the CPython source code and major components follows conventions described in InternalDocs/structure.md which organizes code by feature and component responsibilities.
This page serves as a parent overview linking to specialized child pages for deeper detail on distinct topics such as compilation, execution, runtime infrastructure, and the standard library.
PyObject with PyTypeObject metaclasses, implementing reference counting and cycle-detecting garbage collection._PyRuntimeState, PyInterpreterState, and PyThreadState manage interpreter and thread lifecycles, the Global Interpreter Lock (GIL), and subinterpreter support.Sources: Doc/whatsnew/3.14.rst48-71 Doc/whatsnew/3.15.rst48-100 InternalDocs/structure.md
Diagram: CPython Architecture - Data Flow and Key Code Entities
This diagram illustrates the flow of Python source code through parsing, compilation, interpretation, optimization, and execution, highlighting core code entities, files, and functions.
Sources: InternalDocs/README.md17-65 Doc/whatsnew/3.14.rst63-142 Doc/whatsnew/3.15.rst59-100
CPython defines its bytecode instructions in a single authoritative source written in C with a declarative DSL:
Python/bytecodes.c defines instructions and micro-ops with macros like inst(), op(), macro(), and family().Tools/cases_generator/ process this file to generate tier-specific C code.Python/generated_cases.c.h, executor_cases.c.h, optimizer_cases.c.h, and opcode metadata headers.Python/ceval.c and Python/optimizer.c.| Macro | Purpose |
|---|---|
inst(name, stack_effect) | Defines a Tier 1 bytecode instruction visible to the main interpreter. |
op(name, stack_effect) | Defines a micro-operation (UOp) used internally by the Tier 2 trace optimizer. |
macro(name) | Defines composite instructions that expand into multiple micro-ops. |
family(name, ...) | Groups instructions for specialization (adaptive quickening). |
Sources: InternalDocs/README.md31-40 Doc/whatsnew/3.14.rst108-112
CPython executes bytecode using a three-tier execution system optimized for fast startup, quick steady-state execution, and high-performance native code:
_PyEval_EvalFrameDefault (Python/ceval.c)._PyExecutorObject) that implement the trace logic.Python/jit.c) compiles optimized UOp traces into native machine code using a stencil mechanism.Sources: InternalDocs/README.md40-44 Doc/whatsnew/3.14.rst108-139 Doc/whatsnew/3.15.rst95-97
CPython models all data as objects. Each object has a unique identity, immutable type, and mutable or immutable value.
PyObject: Base structure for all objects, with reference count and type pointer.PyTypeObject: Defines type metadata and behavior for objects.Py_INCREF, Py_DECREF) manage object lifetimes._Py_small_ints_INIT) for performance optimization._Py_global_strings and initialized on startup via _PyUnicode_InitStaticStrings.frozendict and a sentinel type to improve type clarity.Sources: Include/internal/pycore_runtime_init_generated.h15-227 Include/internal/pycore_global_strings.h31-201 Include/internal/pycore_unicodeobject_generated.h12-165 Doc/whatsnew/3.14.rst111 Doc/whatsnew/3.15.rst70-73
CPython runtime state is divided into multiple hierarchical structures:
_PyRuntimeState: Represents global interpreter runtime state for the whole process.PyInterpreterState: Maintains state for an individual interpreter instance including modules, builtins, GIL state, and garbage collection.PyThreadState: Holds state for each thread including frame stack, current interpreter, and recursion counters.Sources: Doc/whatsnew/3.14.rst68-135 Doc/whatsnew/3.15.rst91-93
The CPython standard library provides modules spanning IO, concurrency, data serialization, networking, and more.
compression.zstd module) introduced in 3.14.lazy keyword and runtime flags to improve startup performance.pdb) implemented within the standard library; 3.14 added safe remote debugging interfaces.Sources: Doc/whatsnew/3.14.rst74-168 Doc/whatsnew/3.15.rst74-149 Lib/pdb.py1-116
The CPython build system supports cross-platform building and configuration:
configure.ac.configure (via Tools/build/regen-configure.sh) produce Makefiles.PCbuild/ such as pythoncore.vcxproj.pyconfig.h are generated from templates during build configure.PCbuild/readme.txt.The current development version is defined as 3.16 in configure.ac.
Sources: configure.ac1-18 PCbuild/pythoncore.vcxproj1-72 Doc/using/configure.rst1-43
For detailed guidance on obtaining, configuring, and building CPython, see the child page:
Getting Started and Configuration
This overview provides a map of CPython’s architecture, emphasizing code entities, file locations, and the flow of execution from source code to runtime behavior. For each major component, consult the dedicated child pages for deeper technical information.
Refresh this wiki