python / python/cpython

turtledemo example viewer decodes scripts with the locale default encoding

Đang mở
#153,319 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.13 3.14 3.15 3.16 stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu tại Lib/turtledemo/main.py, trong DemoWindow.loadfile, nơi mã nguồn của bản demo được mở để hiển thị. Xác nhận rằng trình xem sử dụng encoding của mã nguồn được phát hiện từ tệp, và kiểm tra rằng việc tải các ví dụ không còn phát ra EncodingWarning khi dùng -X warn_default_encoding.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
desktop
Loại issue
Lỗi
Độ khó
1/5
Thời gian dự kiến
Dưới một giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.