This document provides an overview of CPython's development and debugging tools, which enable developers to inspect, analyze, and debug Python code execution. These tools include interactive debuggers, bytecode disassemblers, and compilation analysis utilities that expose CPython's internal workings.
For detailed information about specific tools:
pdb and bdb framework) is covered in Python DebuggerCPython provides three primary categories of development tools:
| Tool Category | Primary Modules | Purpose |
|---|---|---|
| Interactive Debugging | pdb, bdb | Step through code execution, inspect variables, set breakpoints |
| Bytecode Analysis | dis, opcode | Examine compiled bytecode, understand optimization behavior |
| Compilation Inspection | symtable, ast, compile | Analyze symbol tables, AST structures, and compilation process |
These tools integrate with CPython's runtime and compilation pipeline to provide visibility into program execution and code generation.
Sources: Lib/pdb.py1-112 Lib/bdb.py1-11 Lib/dis.py1-27
Debugger Architecture: Event Flow from User to Runtime
The debugging infrastructure uses a layered architecture where pdb.Pdb provides the user interface, bdb.Bdb implements the core debugging logic, and either sys.monitoring (default) or sys.settrace (legacy) provides runtime integration.
Sources: Lib/pdb.py351-389 Lib/bdb.py22-75 Lib/bdb.py362-430
CPython's debugger supports two backends for intercepting execution:
Monitoring Backend (Default)
sys.monitoring API (PEP 669) Lib/bdb.py22-75sys.monitoring.DEBUGGER_IDPY_START, LINE, PY_RETURN, RAISE, etc.bdb._MonitoringTracer.EVENT_CALLBACK_MAP Lib/bdb.py23-35Settrace Backend (Legacy)
sys.settrace() API Lib/bdb.py236-324'call', 'line', 'return', 'exception' eventsbackend='settrace' parameter Lib/pdb.py364Sources: Lib/bdb.py22-75 Lib/bdb.py236-324 Lib/pdb.py338-348
Bytecode Analysis: From Code Objects to Human-Readable Output
The dis module provides comprehensive bytecode analysis by iterating over code object bytecode, decoding instructions, and formatting output with opcode metadata.
Sources: Lib/dis.py85-149 Lib/dis.py223-430 Lib/dis.py450-549
The Instruction class encapsulates a single bytecode instruction with rich metadata:
| Field | Type | Description |
|---|---|---|
opname | str | Human-readable opcode name (e.g., 'LOAD_FAST') |
opcode | int | Numeric opcode value |
arg | int or None | Instruction argument |
argval | Any | Resolved argument value (e.g., variable name, constant) |
argrepr | str | Human-readable argument representation |
offset | int | Byte offset in code object |
start_offset | int | Starting offset including CACHE entries |
cache_offset | int | Offset of first cache entry |
end_offset | int | Offset after instruction and caches |
starts_line | int or None | Source line number if new line starts |
line_number | int or None | Current source line number |
is_jump_target | bool | Whether this instruction is a jump target |
jump_target | int or None | Target offset for jump instructions |
positions | dis.Positions | Source location (lineno, end_lineno, col_offset, end_col_offset) |
Sources: Lib/dis.py82-150 Lib/dis.py450-549
Compilation Pipeline with Analysis Points
Developers can inspect the compilation process at multiple stages using built-in tools. Each stage exposes different aspects of how Python code is transformed into executable bytecode.
Sources: Python/compile.c1-155 Python/symtable.c1-10 Lib/symtable.py1-8
The symtable module provides programmatic access to the symbol table constructed during compilation:
Symbol Table API: Querying Variable Scopes and Properties
The symbol table API allows inspection of variable scopes, nested function relationships, and name binding patterns. This is useful for static analysis tools and IDE features.
Sources: Lib/symtable.py1-300 Python/symtable.c92-171
The debugging and analysis tools integrate deeply with CPython's runtime execution model:
Frame Object Access: Debuggers access frame objects via sys._getframe() to inspect local variables, global namespace, and code object Lib/pdb.py433
Execution Control:
bdb.set_step(): Execute one instruction/line Lib/bdb.py329-334bdb.set_next(): Execute until next line in current frame Lib/bdb.py336-344bdb.set_continue(): Resume normal execution Lib/bdb.py346-349bdb.set_return(): Execute until current function returns Lib/bdb.py351-354Breakpoint Management: Breakpoints stored in bdb.Breakpoint.bpbynumber list and bdb.Breakpoint.bplist dict Lib/bdb.py602-800
Event Callbacks: User-overridable methods for execution events:
user_call(frame, argument_list) Lib/pdb.py553-560user_line(frame) Lib/pdb.py562-577user_return(frame, return_value) Lib/pdb.py595-602user_exception(frame, exc_info) Lib/pdb.py604-621Sources: Lib/bdb.py329-361 Lib/pdb.py553-621
Code Object Inspection: The types.CodeType object exposes compiled bytecode and metadata:
co_code: Raw bytecode bytesco_consts: Tuple of constants usedco_names: Tuple of global/builtin namesco_varnames: Tuple of local variable namesco_freevars: Tuple of free variables (closures)co_cellvars: Tuple of cell variables (captured by nested functions)Opcode Metadata: The opcode module provides mappings and metadata:
opname: List mapping opcode numbers to names Lib/dis.py60opmap: Dict mapping names to opcode numbers Lib/dis.py61_cache_format: Inline cache entry layouts Lib/dis.py11_specializations: Adaptive specialization families Lib/dis.py18Position Information: Python 3.11+ includes precise source location information:
co_positions(): Iterator over position tuplesInstruction.positions: Per-instruction source spans Lib/dis.py100-104Sources: Lib/dis.py8-21 Lib/dis.py450-549
| Method | Usage | Entry Point |
|---|---|---|
| Import & call | import pdb; pdb.set_trace() | Lib/pdb.py430-438 |
| Built-in function | breakpoint() | Calls sys.breakpointhook() → pdb.set_trace() |
| Command line (script) | python -m pdb script.py | Lib/pdb.py2457-2515 |
| Command line (module) | python -m pdb -m module | Lib/pdb.py2457-2515 |
| Command line (attach) | python -m pdb -p <pid> | Lib/pdb.py2457-2515 |
| Post-mortem | pdb.pm() after exception | Lib/pdb.py2354-2369 |
| Programmatic | pdb.run(statement) | Lib/pdb.py2311-2322 |
| Async debugging | await pdb.set_trace_async() | Lib/pdb.py440-489 |
| Method | Usage | Entry Point |
|---|---|---|
| Command line | python -m dis script.py | Lib/dis.py815-856 |
| Function call | dis.dis(function) | Lib/dis.py85-149 |
| Bytecode object | bc = dis.Bytecode(code); bc.dis() | Lib/dis.py152-265 |
| Instruction iterator | for instr in dis.get_instructions(code) | Lib/dis.py450-549 |
| Traceback analysis | dis.distb(traceback) | Lib/dis.py268-285 |
| String compilation | dis.disco(source_string) | Lib/dis.py287-302 |
| Method | Usage | Entry Point |
|---|---|---|
| From source string | st = symtable.symtable(code, '<string>', 'exec') | Lib/symtable.py1-20 |
| From file | with open(file) as f: st = symtable.symtable(f.read(), file, 'exec') | Lib/symtable.py1-20 |
Sources: Lib/pdb.py2311-2515 Lib/dis.py85-856 Lib/symtable.py1-20
python -m pdb [options] script.py [args]
Options:
-c, --command <cmd> Execute pdb commands from .pdbrc file
-m <module> Run module as script (like python -m)
-p, --pid <pid> Attach to running process (3.14+)
python -m dis [options] [infile]
Options:
-C, --show-caches Show inline cache entries
-O, --show-offsets Show instruction offsets
-P, --show-positions Show source positions
-S, --specialized Show specialized bytecode
sys.settrace(), events dispatched directly by VMtrace_opcodes=True, generates event per instruction Lib/pdb.py568-574Bytecode object doesn't decode instructions until iterated Lib/dis.py152-265dis._get_code_array() Lib/dis.py580-610adaptive=True shows specialized instructions without re-compiling Lib/dis.py85-149Sources: Lib/bdb.py22-75 Lib/pdb.py568-574 Lib/dis.py152-265
Refresh this wiki
This wiki was recently refreshed. Please wait 5 days to refresh again.