|
|
@@ -0,0 +1,29 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+"""控制台输出兜底 —— 中文 Windows 默认控制台代码页是 936 (GBK), 而脚本正文里常用 ✔ ✘ → 等符号。
|
|
|
+
|
|
|
+踩过的坑: `scripts/page_fingerprint.py` 比对结果"全部一致", 却因为最后一行 print 里的 ✔
|
|
|
+编不出来抛 `UnicodeEncodeError: 'gbk' codec can't encode character '\\u2714'`, 进程以退出码 1 结束 ——
|
|
|
+**闸门报告失败, 而实际是通过的**。2026-09-11 因此误判过一次 (同一坑在 portal_build.py 也踩了一次)。
|
|
|
+
|
|
|
+所以: 凡是"以退出码讲话"的脚本 (回归闸门/校验/巡检), 入口先调 `soft()`, 把 stdout/stderr 的
|
|
|
+错误处理降级为 replace (编不出的符号写成 `?`), 而不是让整条校验因为一个装饰性符号崩掉。
|
|
|
+
|
|
|
+只降 errors, 不改 encoding: 控制台是 GBK 时中文照旧可读; 控制台是 UTF-8 时一切正常。
|
|
|
+"""
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
+import sys
|
|
|
+
|
|
|
+
|
|
|
+def soft(streams=None) -> None:
|
|
|
+ """把 stdout/stderr 的编码错误策略降级为 'replace' (幂等, 失败静默)。"""
|
|
|
+ for s in (streams or (sys.stdout, sys.stderr)):
|
|
|
+ try:
|
|
|
+ s.reconfigure(errors='replace')
|
|
|
+ except Exception:
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
+def setup() -> None:
|
|
|
+ """别名, 语义同 soft (给"入口初始化"读起来更顺)。"""
|
|
|
+ soft()
|