在通过 execshell 执行python脚本时,未正确设置编码,导致UTF8 变成GBK
Description
windows 系统,安装了conda,使用powshell或者cmd进入conda环境, 执行 python -c "import sys; print(sys.stdout.encoding)", 输出 utf-8
启动 codewhale, 让它执行 python -c "import sys; print(sys.stdout.encoding)",输出 gbk
两种方式执行的编码不一致
从CodeWhale源码自我分析的原因如下
● 好了,现在链条完全清晰了。让我给你完整解释。
▏
▏ ────────────────────────────────────────────────────────────
▏
▏ 根本原因:exec_shell 用管道(pipe)接收输出,Python 对管道的编码检测逻辑不同
▏
▏ 整个链路涉及 4 个环节,逐一分析:
▏
▏ 第 1 环:CodeWhale 怎么调用命令
▏
▏ crates/tui/src/tools/shell.rs:1162-1164:
▏
let mut cmd = Command::new(program); // 比如 cmd.exe 或 pwsh.exe
cmd.stdout(Stdio::piped()) // ← 关键!stdout 用管道接收!
cmd.stderr(Stdio::piped()) // stderr 也用管道
▏
▏ stdout 是管道,不是终端。 你直接在 PowerShell 里执行命令时,stdout 连接到真实控制台。但 exec_shell 通过管道捕获输出。
▏
▏ 第 2 环:CodeWhale 怎么设置编码
▏
▏ CodeWhale 启动时确实有 UTF-8 设置:
▏
▏ crates/tui/src/main.rs:130-136:
▏
fn configure_windows_console_utf8() {
SetConsoleCP(CP_UTF8); // 设置 TUI 进程的输入代码页
SetConsoleOutputCP(CP_UTF8); // 设置 TUI 进程的输出代码页
}
▏
▏ 但这只影响 CodeWhale 自身的进程,不影响子进程。
▏
▏ exec_shell 创建子进程时还加了编码前缀:
▏
▏ crates/tui/src/sandbox/mod.rs:93-103:
▏
// 对 PowerShell
format!("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; {command}")
// 对 cmd.exe
format!("chcp 65001 >NUL & {command}")
▏
▏ 第 3 环:Python 的编码检测逻辑
▏
▏ Python 的 sys.stdout.encoding 在不同场景下检测逻辑不同:
▏
▏ ┌────────────────────────────────────────┬────────────────────────────────────────┬────────────────────────────────────────┐
▏ │ 场景 │ stdout 类型 │ Python 如何检测编码 │
▏ ├────────────────────────────────────────┼────────────────────────────────────────┼────────────────────────────────────────┤
▏ │ 你在 PowerShell 直接运行 │ 真实控制台 (CONOUT$) │ 调用 Windows API │
▏ │ │ │ GetConsoleOutputCP() → 返回 │
▏ │ │ │ UTF-8(用户终端已设 65001) │
▏ ├────────────────────────────────────────┼────────────────────────────────────────┼────────────────────────────────────────┤
▏ │ exec_shell 通过管道捕获 │ 匿名管道 (pipe) │ 无法调 GetConsoleOutputCP() → 回退到 │
▏ │ │ │ 系统 ANSI 代码页 → 中文 Windows │
▏ │ │ │ 默认 GBK (cp936) │
▏ └────────────────────────────────────────┴────────────────────────────────────────┴────────────────────────────────────────┘
▏
▏ 这就是差异的核心!
▏
▏ chcp 65001 和 [Console]::OutputEncoding = UTF8 只修改 控制台的代码页。当 stdout 是管道时,Python
▏ 根本拿不到控制台代码页,它回退到 locale.getencoding(),在中文 Windows 上就是 gbk。
▏
▏ 第 4 环:环境变量也被清零了
▏
▏ child_env::apply_to_command 中:
▏
cmd.env_clear(); // 清空所有环境变量
// 然后只恢复白名单中的变量
▏
▏ 查看 child_env.rs 的允许列表(crates/tui/src/child_env.rs:281-284),里面没有 PYTHONIOENCODING 和 PYTHONUTF8。
▏
▏ 而 Python 3.7+ 支持两个环境变量直接覆盖编码行为:
▏
▏ ┌─────────────────────────────────────────────────────────────┬─────────────────────────────────────────────────────────────┐
▏ │ 环境变量 │ 作用 │
▏ ├─────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────┤
▏ │ PYTHONUTF8=1 │ 强制使用 UTF-8 作为默认编码 │
▏ ├─────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────┤
▏ │ PYTHONIOENCODING=utf-8 │ 强制 stdin/stdout/stderr 使用 UTF-8 │
▏ └─────────────────────────────────────────────────────────────┴─────────────────────────────────────────────────────────────┘
▏
▏ 如果你在 PowerShell 里有设过这些环境变量(比如 .profile 或系统级),在直接终端里有效,但 exec_shell 因为 env_clear()
▏ 把它们丢了。
▏
▏ ────────────────────────────────────────────────────────────
▏
▏ 完整问题链路图
▏
你直接在 PowerShell 里运行:
powershell.exe → stdout=CONSOLE → Python 调用 GetConsoleOutputCP() → 65001 → utf-8 ✅
exec_shell 内部:
CodeWhale → 创建子进程 → stdout=PIPE → Python 无法获取控制台代码页
→ 回退到 locale.getencoding() → GBK ❌
→ 而且 env_clear() 去掉了 PYTHONIOENCODING / PYTHONUTF8
▏
▏ ────────────────────────────────────────────────────────────
▏
▏ 两种修复思路
▏
▏ 方案 A:设置 PYTHONIOENCODING=utf-8
▏
▏ 在 child_env.rs 的白名单中增加 PYTHONIOENCODING,或者在创建子进程时强制设置它:
▏
// 在 sandbox/mod.rs 的 CommandSpec::shell() 中
env.insert("PYTHONIOENCODING".to_string(), "utf-8".to_string());
▏
▏ 方案 B:你自己用 exec_shell 时主动传
▏
exec_shell PYTHONIOENCODING=utf-8 python -c "import sys; print(sys.stdout.encoding)"
▏
▏ 不过 exec_shell 内部逻辑会把这个整个字符串传给 shell(cmd /C "PYTHONIOENCODING=utf-8 python ..."),在 cmd.exe 里用 set
▏ VAR=VALUE && command 语法才能生效。
▏
▏ ────────────────────────────────────────────────────────────
▏
▏ 验证一下
▏
▏ 你可以用这个命令确认:
▏
exec_shell python -c "import sys; print(sys.stdout.encoding)"
# → gbk
exec_shell python -c "import os; os.environ.setdefault('PYTHONIOENCODING', 'utf-8'); import sys; print(sys.stdout.encoding)"
# → 如果系统能传进去就会变,但 exec_shell 的 env_clear 会清掉
▏ ▏ 最直接的验证:在 CodeWhale 里运行: ▏ exec_shell cmd /C "set PYTHONIOENCODING=utf-8 && python -c "import sys; print(sys.stdout.encoding)" ▏ ▏ 如果输出变成 utf-8,就确认是环境变量的问题。 ▏ ▏ ────────────────────────────────────────────────────────────
Verified on main
- Merge: PR #4295 →
4d9f6bdee(harvest of @cyq1017 draft #4281) - Proof:
crates/tui/src/child_env.rs— allowlist preservesPYTHONIOENCODING/PYTHONUTF8crates/tui/src/sandbox/mod.rs— Windows shell defaultPYTHONIOENCODING=utf-8
- Credit: @cyq1017
Closing as completed for the Windows UTF-8 Python stdio issue.