turtledemo example viewer decodes scripts with the locale default encoding
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Lib/turtledemo/main.py at DemoWindow.loadfile, where the demo source is opened for display. Confirm the viewer uses the source encoding detected from the file, and verify that loading examples no longer emits an EncodingWarning under -X warn_default_encoding.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 20/100