示例檔案:14_breakpoint.py

與教程配套的原始示例或核驗資料,保留完整內容;其中真實用戶目錄已脫敏,運行範圍以記錄本身為準。

# 用非交互方式演示 pdb:向调试器"喂"一串命令,观察断点处的变量
import subprocess, sys, textwrap, pathlib, tempfile

target = textwrap.dedent('''
def accumulate(xs):
    total = 0
    for i, x in enumerate(xs):
        total += x * i
    return total
print("结果:", accumulate([5, 6, 7, 8]))
''')
path = pathlib.Path(tempfile.mkdtemp(prefix="tutorial-pdb-")) / "bp_demo.py"
path.write_text(target, encoding="utf-8")

commands = "\n".join([
    "b 5, i == 2",      # 条件断点:只在 i == 2 时停下
    "c",                # 继续运行到断点
    "p i, x, total",    # 打印变量
    "where",            # 查看调用栈
    "c",                # 继续直到结束
    "q",
]) + "\n"
out = subprocess.run([sys.executable, "-X", "utf8", "-m", "pdb", str(path)], input=commands,
                     capture_output=True, text=True, encoding="utf-8", timeout=10)
print(out.stdout.replace(str(path), "bp_demo.py"))

關聯教程

繼續瀏覽教程 返回知識 →返回頂部 ↑
本篇目錄
教程圖解100%
教程圖解