fix(server_waitress.py): 修复 PyInstaller 打包后 UTF-8 编码包装导致的启动崩溃

sys.stdout/sys.stderr 包装为 UTF-8 TextIOWrapper 时,
在 PyInstaller --onefile 无控制台模式或管道重定向场景下,
sys.stdout.buffer 可能不存在,抛出 AttributeError 导致 EXE 启动失败。
增加 try/except 捕获 AttributeError 和 ValueError,失败时跳过编码包装。
This commit is contained in:
DXC
2026-07-10 15:20:03 +08:00
parent 616783fb52
commit 3367774574

View File

@ -45,9 +45,17 @@ import sys
from pathlib import Path
# 设置控制台编码为 UTF-8解决 Windows 中文乱码问题)
# PyInstaller onefile 模式下 sys.stdout.buffer 可能不存在(无控制台或管道重定向),
# 此时跳过编码包装,避免 AttributeError 导致启动崩溃
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
try:
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
except (AttributeError, ValueError):
pass
try:
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
except (AttributeError, ValueError):
pass
# Add the project root and src directory to PYTHONPATH
project_root = Path(__file__).parent.absolute()