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

离线校验运行配置模型、bdb 条件/异常断点触发、Watches 动态求值与多进程排查清单

PYTHONWindows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。explore-run-debug.py
explore-run-debug.py187 行
"""Offline Python verification lab for PyCharm Chapter 12 (run-debug)."""
import bdb
import json

VERSION = "CPython 3.12.14 offline PyCharm workflow verifier (desktop IDE not launched)"


def case_interpreter_and_run_config_model():
    interpreter_types = ["Virtualenv", "Conda", "Pipenv", "System", "Remote(SSH/Docker/Vagrant)"]
    run_configs = {
        "script_mode": {
            "script_path": "app.py",
            "parameters": "--port 8000",
            "working_dir": "/workspace/project",
            "env": {"PYTHONPATH": "/workspace/project/src"},
        },
        "flask_module": {
            "module_name": "flask",
            "parameters": "run --port=5000",
        },
        "django_script": {
            "script_path": "manage.py",
            "parameters": "runserver 0.0.0.0:8000",
        },
    }
    expected = {
        "interpreter_kinds": 5,
        "script_has_pythonpath": True,
        "flask_module_name": "flask",
        "django_params": "runserver 0.0.0.0:8000",
    }
    actual = {
        "interpreter_kinds": len(interpreter_types),
        "script_has_pythonpath": "PYTHONPATH" in run_configs["script_mode"]["env"],
        "flask_module_name": run_configs["flask_module"]["module_name"],
        "django_params": run_configs["django_script"]["parameters"],
    }
    return {
        "name": "interpreter_and_run_config_model",
        "summary": "校验虚拟/系统/远程解释器类型及单文件、Flask 模块与 Django 开发服务器运行配置参数",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_conditional_and_exception_breakpoints():
    hits = []
    exc_caught = []

    class TraceDebugger(bdb.Bdb):
        def user_line(self, frame):
            if frame.f_code.co_name == "compute_batch":
                x_val = frame.f_locals.get("x")
                if x_val is not None and x_val > 10:
                    hits.append(x_val)
            self.set_continue()

        def user_exception(self, frame, exc_info):
            if frame.f_code.co_name == "risky_div":
                exc_caught.append(exc_info[0].__name__)
            self.set_continue()

    def compute_batch(values):
        total = 0
        for x in values:
            total += x
        return total

    def risky_div(a, b):
        return a / b

    dbg = TraceDebugger()
    target_line = compute_batch.__code__.co_firstlineno + 3
    dbg.set_break(__file__, target_line, cond="x > 10")
    res = dbg.runcall(compute_batch, [4, 8, 12, 18])

    class ExceptionBreakpointDebugger(bdb.Bdb):
        def user_line(self, frame):
            self.set_step()

        def user_exception(self, frame, exc_info):
            if frame.f_code.co_name == "risky_div":
                exc_caught.append(exc_info[0].__name__)
            self.set_continue()

    dbg2 = ExceptionBreakpointDebugger()
    try:
        dbg2.runcall(risky_div, 10, 0)
    except ZeroDivisionError:
        pass

    expected = {
        "sum_result": 42,
        "conditional_hits": [12, 18],
        "exception_breakpoint_type": ["ZeroDivisionError"],
    }
    actual = {
        "sum_result": res,
        "conditional_hits": hits,
        "exception_breakpoint_type": exc_caught,
    }
    return {
        "name": "conditional_and_exception_breakpoints",
        "summary": "基于标准库 bdb 真实触发条件断点(x > 10)与异常断点(ZeroDivisionError)并记录命中快照",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_step_controls_and_evaluate_expression():
    step_table = {
        "步过(Step Over)": ("F8", "F8"),
        "步入(Step Into)": ("F7", "F7"),
        "步出(Step Out)": ("Shift + F8", "⇧ + F8"),
        "继续(Resume)": ("F9", "⌥⌘R / ⌘+F8(源表)"),
        "停止(Stop)": ("Ctrl + F2", "⌘ + F2"),
    }
    # Simulate Variables panel, Watches expression len(items), and Debug Console mutation
    frame_locals = {"items": [10, 20, 30], "factor": 2}
    watch_len = eval("len(items)", {}, frame_locals)
    eval_expr = eval("sum(items) * factor", {}, frame_locals)
    frame_locals["factor"] = 3
    eval_after_edit = eval("sum(items) * factor", {}, frame_locals)

    expected = {
        "control_count": 5,
        "watch_len_items": 3,
        "eval_before_edit": 120,
        "eval_after_edit": 180,
    }
    actual = {
        "control_count": len(step_table),
        "watch_len_items": watch_len,
        "eval_before_edit": eval_expr,
        "eval_after_edit": eval_after_edit,
    }
    return {
        "name": "step_controls_and_evaluate_expression",
        "summary": "核对 5 项单步调试控制键并模拟 Watches(len(items))与调试控制台动态求值/变量修改",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_remote_multiprocess_and_troubleshoot():
    remote_backends = ["SSH", "Docker", "Vagrant"]
    multiprocess_opts = {
        "debug_child_processes": True,
        "supported_modules": ["multiprocessing", "subprocess"],
        "attach_local_process": True,
    }
    breakpoint_checks = ["interpreter_match", "file_saved_ctrl_s", "breakpoint_enabled_not_muted"]
    expected = {
        "remote_backends": ["SSH", "Docker", "Vagrant"],
        "child_process_debug": True,
        "troubleshoot_checks": 3,
    }
    actual = {
        "remote_backends": remote_backends,
        "child_process_debug": multiprocess_opts["debug_child_processes"] and len(multiprocess_opts["supported_modules"]) == 2,
        "troubleshoot_checks": len(breakpoint_checks),
    }
    return {
        "name": "remote_multiprocess_and_troubleshoot",
        "summary": "校验远程调试后端、子进程与附加本地进程调试配置以及断点不生效排查清单",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_interpreter_and_run_config_model(),
        case_conditional_and_exception_breakpoints(),
        case_step_controls_and_evaluate_expression(),
        case_remote_multiprocess_and_troubleshoot(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。

入口文件:projects/pycharm-run-debug-lab/explore-run-debug.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-run-debug.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PyCharm

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

下载课程关联工程包:PyCharm

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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