"""Offline VS Code launch.json config & real Python bdb breakpoint debugger verifier for Ch17."""
import bdb
import io
import json
import sys
import tempfile
from contextlib import redirect_stdout
from pathlib import Path
VERSION = "CPython 3.12.14 offline VS Code launch.json & bdb breakpoint verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
class TraceDebugger(bdb.Bdb):
def __init__(self, target_file: str):
super().__init__()
self.target_file = self.canonic(target_file)
self.stops = []
def user_line(self, frame):
if self.canonic(frame.f_code.co_filename) == self.target_file and self.break_here(frame):
self.stops.append({
"lineno": frame.f_lineno,
"func": frame.f_code.co_name,
"name_var": frame.f_locals.get("name"),
})
self.set_continue()
def main():
cases = []
# Case 1: .vscode/launch.json Python debug schema & interpreter selection check
launch_cfg = {
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}
],
}
cfg = launch_cfg["configurations"][0]
cases.append(
check(
"launch_json_and_interpreter_selection",
"Verify .vscode/launch.json Python Current File configuration and Python 3 interpreter version",
{
"schema_version": "0.2.0",
"debugger_type": "debugpy",
"request": "launch",
"program_macro": "${file}",
"python_major_is_3": True,
},
{
"schema_version": launch_cfg["version"],
"debugger_type": cfg["type"],
"request": cfg["request"],
"program_macro": cfg["program"],
"python_major_is_3": sys.version_info.major == 3,
},
)
)
# Case 2: Real bdb breakpoint pause on print() line, inspect `name` variable, then Continue
source = (
"def say_hello(name):\n"
' print("Hello, " + name)\n'
'say_hello("VS Code")\n'
)
with tempfile.TemporaryDirectory() as tmp:
py_file = Path(tmp) / "hello.py"
py_file.write_text(source, encoding="utf-8")
filename = str(py_file)
dbg = TraceDebugger(filename)
err = dbg.set_break(filename, 2)
assert err is None, err
buf = io.StringIO()
code_obj = compile(source, filename, "exec")
with redirect_stdout(buf):
dbg.run(code_obj, {}, {})
cases.append(
check(
"bdb_breakpoint_pause_and_inspect",
"Verify real Python bdb breakpoint hit on line 2, local variable name=='VS Code', and continue output",
{
"breakpoint_hits": 1,
"paused_line": 2,
"paused_func": "say_hello",
"inspected_name": "VS Code",
"final_stdout": "Hello, VS Code",
},
{
"breakpoint_hits": len(dbg.stops),
"paused_line": dbg.stops[0]["lineno"],
"paused_func": dbg.stops[0]["func"],
"inspected_name": dbg.stops[0]["name_var"],
"final_stdout": buf.getvalue().strip(),
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vscode-run-and-debug-lab/explore-run-and-debug.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-run-and-debug.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2216.zip