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
分支
36k
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 摘要。