bfddfdfdfewe

This commit is contained in:
Mavis
2026-06-11 17:24:46 +08:00
parent fd7817b881
commit 24478604b1
25 changed files with 2612 additions and 4 deletions

39
scripts/append_mem2.py Normal file
View File

@@ -0,0 +1,39 @@
p = r'C:\Users\Administrator\.mavis\agents\mavis\memory\MEMORY.md'
with open(p, encoding='utf-8') as f:
s = f.read()
old = '''**已犯**:diary-news healthcheck.py 用了 `f"...%{{http_code}}..."` 写法,3 处全部 NameError,在 detail 解析时 m 还会变 None 引发二次 AttributeError。修法:3 处改普通字符串拼接 + rsplit 拿 status_part。'''
new = '''**已犯**:diary-news healthcheck.py 用了 `f"...%{{http_code}}..."` 写法,3 处全部 NameError,在 detail 解析时 m 还会变 None 引发二次 AttributeError。修法:3 处改普通字符串拼接 + rsplit 拿 status_part。
### global + lambda 闭包 + 条件赋值的隐藏 NameError (2026-06-11)
Type: pitfall
**坑**:模块顶层 GROUPS 字典里的 `lambda r: check_xxx(r, GLOBAL_VAR)`,GLOBAL_VAR 在 main() 里用 `global X` 声明,只在某个 if 分支里赋值。
**症状**:lambda 调用时 `name 'X' is not defined`,但代码里**所有** global 块都正确声明了。
**根因**:`global X` 只是声明"当前作用域的 X 指向模块 dict",**不**会自动在模块 dict 里创建键。如果 if 分支没走到,模块 dict 里压根没 X 键,lambda 闭包查找时 NameError。
**关键诊断**:`AUTH_TOKEN in module_dict` → False(在 main 跑过后,在没传参的 else 分支里)
**修法**:`global X` 后**立即无条件** `X = ""`(或合理的默认值),保证键存在;再在 if 分支里覆盖。
```python
def main():
global AUTH_TOKEN
AUTH_TOKEN = "" # ← 必须,即使后续不登录也要先写空串
if login_success:
AUTH_TOKEN = get_token()
# else 分支走不到时,lambda 仍能读到 ""
```
**和 f-string 那个坑的对比**:那次是 f-string 求值时机问题(import 期就报);这次是 lambda deferred 求值 + 模块 dict 缺键,跑 main() 之后才暴露。**两类都要靠"无条件初始化"防御**。'''
if old in s:
s = s.replace(old, new, 1)
with open(p, 'w', encoding='utf-8') as f:
f.write(s)
print('appended')
else:
print('old not found, skipping')