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
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