From 336777457411cd355bfd4e1d60016b2e3ac6d4ab Mon Sep 17 00:00:00 2001 From: DXC Date: Fri, 10 Jul 2026 15:20:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(server=5Fwaitress.py):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=20PyInstaller=20=E6=89=93=E5=8C=85=E5=90=8E=20UTF-8=20?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E5=8C=85=E8=A3=85=E5=AF=BC=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sys.stdout/sys.stderr 包装为 UTF-8 TextIOWrapper 时, 在 PyInstaller --onefile 无控制台模式或管道重定向场景下, sys.stdout.buffer 可能不存在,抛出 AttributeError 导致 EXE 启动失败。 增加 try/except 捕获 AttributeError 和 ValueError,失败时跳过编码包装。 --- server_waitress.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server_waitress.py b/server_waitress.py index bac4c4d..42d4e86 100644 --- a/server_waitress.py +++ b/server_waitress.py @@ -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()