-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
turtledemo example viewer decodes scripts with the locale default encoding #153319
Copy link
Copy link
Closed
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
Metadata
Metadata
Assignees
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
The
turtledemoexample viewer opens each demo script for display withopen(self.module.__file__, 'r')inDemoWindow.loadfile(
Lib/turtledemo/__main__.py). Noencodingargument is passed, so thesource is decoded with the locale default encoding.
This emits an
EncodingWarningunder-X warn_default_encoding, and on anon-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()inLib/turtledemo.Reproduction
Launching the viewer under
-X warn_default_encodingand loading any examplereaches
open(self.module.__file__, 'r')inDemoWindow.loadfile, whichraises under
-W error::EncodingWarning: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 encodingdetected 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