On this page
bdb
Prev: base64 | Up: stdlib_module | Next: binascii
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — bdb
Tags: #public #python3 #stdlib
Import
import bdb
Overview
Base debugger framework (Bdb, Breakpoint, BdbQuit) that powers Python's interactive pdb debugger and IDE debuggers. Subclass bdb.Bdb when building a custom debugger, step tracer, or programmatic breakpoint runner without re-implementing sys.settrace() frame management from scratch.
Quick Start
import bdb
class CallLogger(bdb.Bdb):
def __init__(self) -> None:
super().__init__()
self.calls: list[str] = []
def user_call(self, frame, argument_list) -> None:
self.calls.append(frame.f_code.co_name)
def greet() -> str:
return "hi"
tracer = CallLogger()
result = tracer.runcall(greet)
assert result == "hi" and "greet" in tracer.calls
Implementation Walkthrough
1. Implementing user_line() for Line-by-Line Stepping
Override user_line(frame) to inspect locals or stop at breakpoints while calling self.set_step() / self.set_next(frame) / self.set_continue() to control execution flow.
Gotchas & Boundaries
- Significant runtime overhead:
bdbinstalls a trace hook viasys.settrace()(orsys.monitoringin newer Python), which slows execution—don't leave it active in production hot paths. - Use pdb for interactive debugging:
bdbprovides no user interface;pdb.Pdbcombinesbdb.Bdbwith cmd. - Clear trace hooks cleanly: Always run via
runcall()/run()or callsys.settrace(None)in afinallyblock.
Classes & Objects
- bdb.Bdb — Generic Python debugger base class. (Methods:
.break_anywhere(),.break_here(),.canonic(),.clear_all_breaks(),.clear_all_file_breaks(),.clear_bpbynumber()) - bdb.BdbQuit — Exception to give up completely. (Methods:
.add_note(),.with_traceback()) - bdb.Breakpoint — Breakpoint class. (Methods:
.bpformat(),.bpprint(),.clearBreakpoints(),.deleteMe(),.disable(),.enable())
Linked from
Related notes
- tuts/python3/stdlib_module/base64 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/bdb/Bdb Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/bdb/BdbQuit Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/bdb/Breakpoint Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/cmd Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/doctest Linked here; shared tags: #stdlib