python / python/cpython

turtledemo example viewer decodes scripts with the locale default encoding

未关闭
#153,319 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.13 3.14 3.15 3.16 stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

The turtledemo example viewer opens each demo script for display with
open(self.module.__file__, 'r') in DemoWindow.loadfile
(Lib/turtledemo/__main__.py). No encoding argument is passed, so the
source is decoded with the locale default encoding.

This emits an EncodingWarning under -X warn_default_encoding, and on a
non-UTF-8 locale (for example Windows cp1252) it can mis-decode a demo whose
source is UTF-8 or carries a PEP 263 coding cookie, showing mojibake in the
viewer.

It is the only unspecified-encoding open() in Lib/turtledemo.

Reproduction

Launching the viewer under -X warn_default_encoding and loading any example
reaches open(self.module.__file__, 'r') in DemoWindow.loadfile, which
raises under -W error::EncodingWarning:

EncodingWarning: 'encoding' argument not specified

On a non-UTF-8 locale the same call decodes the source with the wrong codec.

Fix

Read the source with tokenize.open(), which decodes using the encoding
detected from the file (its PEP 263 coding cookie, else UTF-8). That is the
correct encoding for reading Python source, and it matches how the module was
just imported one line above.

Linked PRs
  • gh-153321

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/turtledemo/main.py 中的 DemoWindow.loadfile 开始,这里会打开演示源代码以供显示。确认查看器使用从文件中检测到的源代码编码,并验证在 -X warn_default_encoding 下加载示例不再发出 EncodingWarning。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
desktop
Issue 类型
缺陷
难度
1/5
预计耗时
1 小时以内
活跃度
停滞
描述清晰度
描述清楚
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。