蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Eclipse Debug 启动快捷键、断点管理、调用栈/变量与单步执行状态机离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-debugging-program.py
explore-debugging-program.py154 行
"""Offline Eclipse Debug session, breakpoints, call stack & stepping verifier for Ch21 (debugging-program)."""
import json

VERSION = "CPython 3.12.14 offline Eclipse JDT Debug state-machine verifier (desktop SWT IDE absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = []

    # Case 1: Debug launch entry points, shortcuts, and Debug Perspective prompt
    launch_shortcuts = {
        "debug_as_java_application": "Alt+Shift+D, J",
        "relaunch_last_debug": "F11",
        "resume": "F8",
        "step_into": "F5",
        "step_over": "F6",
        "step_return": "F7",
    }
    session_started_in_java_perspective = True
    prompts_debug_perspective = session_started_in_java_perspective

    cases.append(
        check(
            "debug_entry_and_shortcuts",
            "Verify Debug As, F11 relaunch, stepping shortcuts, and Debug Perspective prompt contract",
            {
                "debug_as_shortcut": "Alt+Shift+D, J",
                "relaunch_shortcut": "F11",
                "prompts_debug_perspective": True,
                "step_keys": ["F5", "F6", "F7", "F8"],
            },
            {
                "debug_as_shortcut": launch_shortcuts["debug_as_java_application"],
                "relaunch_shortcut": launch_shortcuts["relaunch_last_debug"],
                "prompts_debug_perspective": prompts_debug_perspective,
                "step_keys": [
                    launch_shortcuts["step_into"],
                    launch_shortcuts["step_over"],
                    launch_shortcuts["step_return"],
                    launch_shortcuts["resume"],
                ],
            },
        )
    )

    # Case 2: Toggle Breakpoint sync between editor marker bar and Breakpoints view
    breakpoints = set()

    def toggle_breakpoint(file_name, line):
        key = (file_name, line)
        if key in breakpoints:
            breakpoints.remove(key)
        else:
            breakpoints.add(key)

    toggle_breakpoint("InvoiceApp.java", 14)
    toggle_breakpoint("InvoiceApp.java", 22)
    after_add = sorted(breakpoints)
    toggle_breakpoint("InvoiceApp.java", 14)
    after_remove = sorted(breakpoints)

    cases.append(
        check(
            "toggle_breakpoint_and_view",
            "Verify Toggle Breakpoint adds/removes line markers reflected in Breakpoints view",
            {
                "after_add": [["InvoiceApp.java", 14], ["InvoiceApp.java", 22]],
                "after_remove": [["InvoiceApp.java", 22]],
            },
            {
                "after_add": [list(x) for x in after_add],
                "after_remove": [list(x) for x in after_remove],
            },
        )
    )

    # Case 3: Suspended JVM state exposing Debug view call stack & Variables view locals
    call_stack = [
        {"method": "InvoiceApp.applyDiscount", "line": 22, "locals": {"price": 200, "rate": 0.1, "finalPrice": 180.0}},
        {"method": "InvoiceApp.calculateTotal", "line": 14, "locals": {"items": 2, "subtotal": 200}},
        {"method": "InvoiceApp.main", "line": 7, "locals": {"args": []}},
    ]
    top_frame = call_stack[0]

    cases.append(
        check(
            "suspended_call_stack_and_variables",
            "Verify suspended breakpoint exposes call stack frames in Debug view and locals in Variables view",
            {
                "stack_depth": 3,
                "top_method": "InvoiceApp.applyDiscount",
                "top_line": 22,
                "finalPrice": 180.0,
            },
            {
                "stack_depth": len(call_stack),
                "top_method": top_frame["method"],
                "top_line": top_frame["line"],
                "finalPrice": top_frame["locals"]["finalPrice"],
            },
        )
    )

    # Case 4: Step Into (F5), Step Over (F6), Step Return (F7), Resume (F8) trace
    # Trace starting at main:7 (call calculateTotal -> line 12 -> line 14 call applyDiscount -> line 22 -> return)
    trace = []
    state = {"method": "main", "line": 7, "depth": 1, "suspended": True}
    # F5 Step Into calculateTotal
    state = {"method": "calculateTotal", "line": 12, "depth": 2, "suspended": True}
    trace.append(("Step Into", state["method"], state["line"], state["depth"]))
    # F6 Step Over line 12 -> line 14
    state = {"method": "calculateTotal", "line": 14, "depth": 2, "suspended": True}
    trace.append(("Step Over", state["method"], state["line"], state["depth"]))
    # F5 Step Into applyDiscount
    state = {"method": "applyDiscount", "line": 22, "depth": 3, "suspended": True}
    trace.append(("Step Into", state["method"], state["line"], state["depth"]))
    # F7 Step Return back to calculateTotal:15
    state = {"method": "calculateTotal", "line": 15, "depth": 2, "suspended": True}
    trace.append(("Step Return", state["method"], state["line"], state["depth"]))
    # F8 Resume until program exit
    state = {"method": None, "line": None, "depth": 0, "suspended": False}
    trace.append(("Resume", state["method"], state["line"], state["depth"]))

    cases.append(
        check(
            "step_into_over_resume_control",
            "Verify Step Into, Step Over, Step Return, and Resume control transitions",
            [
                ["Step Into", "calculateTotal", 12, 2],
                ["Step Over", "calculateTotal", 14, 2],
                ["Step Into", "applyDiscount", 22, 3],
                ["Step Return", "calculateTotal", 15, 2],
                ["Resume", None, None, 0],
            ],
            [list(t) for t in trace],
        )
    )

    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/eclipse-debugging-program-lab/explore-debugging-program.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-debugging-program.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Eclipse

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip

下载课程关联工程包:Eclipse

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2272.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习