Skip to content

[Bug][tools] scons --attach 在中文 Windows(GBK 控制台)下抛 UnicodeEncodeError #11906

Open

Description

@Yunlk

问题

Windows 中文环境下(控制台默认代码页 936 / GBK),执行 scons --attach=? 会直接异常退出:

scons: Reading SConscript files ...
UnicodeEncodeError: 'gbk' codec can't encode character '\u2705' in position 5: illegal multibyte sequence:
  File "D:\git\rt-thread\bsp\renesas\ra6m3-hmi-board\SConstruct", line 61:
    objs = PrepareBuilding(env, RTT_ROOT, has_libcpu=False)
  File "D:\git\rt-thread\tools\building.py", line 396:
    GenAttachConfigProject()
  File "D:\git\rt-thread\tools\attachconfig.py", line 18:
    print("\033[32m\u2705 AttachConfig has: \033[0m")
  File "D:\...\.venv\Lib\site-packages\SCons\Util\__init__.py", line 1246:
    self.file.write(arg)

复现

环境:Windows 11 中文版,Python 3.14,SCons 4.11.1,master 1c801efa。

  1. 任选一个带 .ci/attachconfig/attachconfig.yml 的 BSP(如 bsp/renesas/ra6m3-hmi-board)
  2. 在默认代码页(936)的终端执行 scons --attach=?

必现。设置 PYTHONIOENCODING=utf-8 后同样的命令正常输出,可以确认是输出编码问题而非功能问题。

原因

tools/attachconfig.py 用 emoji 做状态提示,共 4 处:

line 18: print("\033[32m✅ AttachConfig has: \033[0m")
line 35: print("\033[32m✅ Default .config and rtconfig.h recovery success!\033[0m")
line 41: print("❌\033[31m Without this AttachConfig:"+attachconfig+"\033[0m")
line 52: print("\033[32m✅ AttachConfig add success!\033[0m")

Python 在 Windows 下按 locale 编码写 stdout,✅(U+2705) / ❌(U+274C) 无法用 GBK 编码,于是抛 UnicodeEncodeError。

影响范围

--attach=?、--attach=<name>、--attach=default 三条命令全部不可用——它们都在 GenAttachConfigProject() 里,每条退出路径都有 emoji。功能本身是好的,只在中文 Windows 上无法使用。

考虑到 RT-Thread 的 Windows 用户大多是中文环境,实际影响面不小。目前只能靠 set PYTHONIOENCODING=utf-8 绕过,普通用户不会知道这个办法。

建议

  1. 去掉 emoji,换成 ASCII 前缀([OK] / [ERR])。tools/ 下其余脚本基本都是纯 ASCII 输出风格,这样也和它们一致。
  2. 或者在输出前统一做一次编码兜底,避免同类问题在别处复现。
  3. 这 4 处需要一并改掉,只改第 18 行的话,--attach=<name> 和 --attach=default 仍会崩。

Activity

  1. github-actions commented on Oct 20, 2026

    @github-actions

    如需认领并处理此 Issue,请评论 /claim。

    To claim and work on this issue, comment /claim.

  2. Yunlk commented on Oct 20, 2026

    @Yunlk
    ContributorAuthor

    /claim

  3. github-actions commented on Oct 20, 2026

    @github-actions

    @Yunlk has claimed this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions