This document provides a high-level architectural overview of CPython, the reference implementation of the Python programming language. It covers the major subsystems, their interactions, and the overall flow from Python source code to execution. This page serves as an entry point to understanding the codebase structure and design.
For detailed information on specific subsystems:
CPython is the canonical Python interpreter written in C. It transforms Python source code into bytecode, which is then executed by a virtual machine. The codebase is organized around several core responsibilities:
| Component | Primary Location | Purpose |
|---|---|---|
| Bytecode Compiler | Python/compile.c, Python/flowgraph.c | Transforms AST to bytecode |
| Tier 1 Interpreter | Python/ceval.c, Python/generated_cases.c.h | Executes bytecode instructions |
| Tier 2 JIT Optimizer | Python/optimizer.c, Python/executor_cases.c.h | Optimizes hot code paths |
| Object System | Objects/ directory | Implements Python's data types |
| Runtime State | Python/pystate.c, Python/ceval_gil.c | Manages interpreter and thread state |
| Build System | configure.ac, Makefile.pre.in | Configures and builds CPython |
Sources: Python/bytecodes.c1-50 Python/ceval.c1-50 Python/optimizer.c1-50 configure.ac1-50
Sources: Python/bytecodes.c1-100 Python/ceval.c1-300 Python/optimizer.c1-100 Python/generated_cases.c.h1-50 Python/executor_cases.c.h1-50
Sources: Python/bytecodes.c1-10 Python/ceval.c1-50 Python/pystate.c1-50 configure.ac1-50 Makefile.pre.in1-50
CPython implements a two-tier execution system to balance startup speed with long-running performance:
The Tier 1 interpreter is the traditional bytecode execution engine defined in Python/ceval.c with instruction implementations generated from Python/bytecodes.c into Python/generated_cases.c.h
Key characteristics:
_PyEval_EvalFrameDefault()Sources: Python/ceval.c1-300 Python/generated_cases.c.h1-100 Python/bytecodes.c1-150
When code becomes "hot" (executed frequently), the Tier 2 system builds optimized traces:
Key components:
Python/optimizer.c): Builds traces from bytecode, applies optimizationsPython/optimizer_analysis.c): Performs data-flow analysis_PyExecutorObject): Executes optimized micro-operation sequencesPython/executor_cases.c.h): Low-level operations for Tier 2Sources: Python/optimizer.c1-100 Python/optimizer_analysis.c1-50 Python/executor_cases.c.h1-100 Python/optimizer_bytecodes.c1-100
All Python objects share a common base structure and type system:
Sources: Objects/object.c1-100 Objects/typeobject.c1-200 Objects/dictobject.c1-100
Dictionaries are central to Python's implementation, used for namespaces, class attributes, and instance storage:
Sources: Objects/dictobject.c1-100 Include/internal/pycore_dict.h1-50
CPython maintains a hierarchy of state objects that manage execution:
Sources: Python/pystate.c1-100 Python/ceval_gil.c1-50 Include/internal/pycore_pystate.h1-100
CPython uses an autoconf-based build system that adapts to different platforms:
Sources: configure.ac1-100 Makefile.pre.in1-100
The posixmodule.c provides a unified OS interface:
Sources: Modules/posixmodule.c1-100 configure.ac300-400
CPython generates several critical files from source definitions:
Sources: Python/bytecodes.c1-10 Python/generated_cases.c.h1-20 Python/executor_cases.c.h1-20 Python/optimizer_cases.c.h1-20
CPython uses reference counting as the primary memory management strategy, supplemented by a cyclic garbage collector:
Sources: Objects/object.c1-200 Modules/gcmodule.c1-100 Python/ceval.c100-150
CPython has extensive testing infrastructure:
| Test Type | Location | Purpose |
|---|---|---|
| Unit Tests | Lib/test/test_*.py | Test individual modules and features |
| Optimizer Tests | Lib/test/test_capi/test_opt.py | Test JIT optimizer behavior |
| CI Workflows | .github/workflows/*.yml | Automated testing across platforms |
| Build Tests | Tools/build/ | Test build system |
Sources: Lib/test/test_capi/test_opt.py1-100
bytecodes.c for maintainabilityposixmodule.c) with platform-specific implementationsSources: Python/bytecodes.c1-150 Python/ceval.c1-300 Python/optimizer.c1-100 Modules/posixmodule.c1-100
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.