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

离线模拟 Application 参考接口的 Contents 移除、StaticObjects 保留、重入 Lock/Unlock 与事件序列

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-ref-application.py
explore-ref-application.py125 行
"""Offline Classic ASP / VBScript verifier for Chapter 16 (ref-application)."""
from __future__ import annotations

import json

VERSION = "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)"


class ApplicationRefSim:
    def __init__(self) -> None:
        self.contents: dict[str, object] = {}
        self.static_objects: dict[str, str] = {"SharedDict": "Scripting.Dictionary"}
        self.events_fired: list[str] = []
        self._lock_owner: str | None = None
        self._lock_count = 0

    def trigger_on_start(self) -> None:
        self.events_fired.append("Application_OnStart")
        self.contents["dsn"] = "Provider=SQLOLEDB;Server=local"
        self.contents["hits"] = 0
        self.contents["temp"] = "remove-me"

    def trigger_on_end(self) -> None:
        self.events_fired.append("Application_OnEnd")

    def remove(self, key: str) -> None:
        self.contents.pop(key, None)

    def remove_all(self) -> None:
        self.contents.clear()

    def lock(self, owner: str) -> None:
        if self._lock_owner is not None and self._lock_owner != owner:
            raise RuntimeError("Locked by another request")
        self._lock_owner = owner
        self._lock_count += 1

    def unlock(self, owner: str) -> None:
        if self._lock_owner == owner and self._lock_count > 0:
            self._lock_count -= 1
            if self._lock_count == 0:
                self._lock_owner = None


def main() -> None:
    app = ApplicationRefSim()
    app.trigger_on_start()
    initial_keys = list(app.contents.keys())
    app.remove("temp")
    after_remove_keys = list(app.contents.keys())

    # Lock / Unlock re-entrancy and exclusive write protection
    app.lock("worker_a")
    app.lock("worker_a")
    blocked_b = False
    try:
        app.lock("worker_b")
    except RuntimeError:
        blocked_b = True
    app.contents["hits"] = int(app.contents["hits"]) + 1
    app.unlock("worker_a")
    still_locked_after_one_unlock = app._lock_owner == "worker_a"
    app.unlock("worker_a")
    unlocked_cleanly = app._lock_owner is None

    app.remove_all()
    after_remove_all_contents = len(app.contents)
    static_preserved = list(app.static_objects.keys())
    app.trigger_on_end()

    cases = [
        {
            "name": "application_contents_and_remove",
            "summary": "验证 Contents 写入、Contents.Remove 删单键与 Contents.RemoveAll() 清空且不影响 StaticObjects",
            "expected": {
                "initial_keys": ["dsn", "hits", "temp"],
                "after_remove_keys": ["dsn", "hits"],
                "after_remove_all_count": 0,
                "static_preserved": ["SharedDict"],
            },
            "actual": {
                "initial_keys": initial_keys,
                "after_remove_keys": after_remove_keys,
                "after_remove_all_count": after_remove_all_contents,
                "static_preserved": static_preserved,
            },
            "passed": True,
        },
        {
            "name": "application_staticobjects_and_events",
            "summary": "核对 StaticObjects 集合读取及 Application_OnStart / Application_OnEnd 生命周期触发顺序",
            "expected": {
                "static_progid": "Scripting.Dictionary",
                "events": ["Application_OnStart", "Application_OnEnd"],
            },
            "actual": {
                "static_progid": app.static_objects["SharedDict"],
                "events": app.events_fired,
            },
            "passed": True,
        },
        {
            "name": "application_lock_unlock_reference",
            "summary": "验证 Application.Lock 嵌套重入计数、阻塞其他并发写请求与配对 Unlock 释放",
            "expected": {
                "blocked_other_worker": True,
                "still_locked_after_first_unlock": True,
                "unlocked_after_second_unlock": True,
            },
            "actual": {
                "blocked_other_worker": blocked_b,
                "still_locked_after_first_unlock": still_locked_after_one_unlock,
                "unlocked_after_second_unlock": unlocked_cleanly,
            },
            "passed": True,
        },
    ]
    for c in cases:
        c["passed"] = c["actual"] == c["expected"]
    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/asp-ref-application-lab/explore-ref-application.py

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

下载课程关联工程包:ASP

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

下载课程关联工程包:ASP

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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