gh-153319: Read turtledemo scripts with the source file's own encoding - #153321
Open
tonghuaroot wants to merge 3 commits into
Open
tonghuaroot wants to merge 3 commits into
tonghuaroot wants to merge 3 commits into
Conversation
…ncoding The demo viewer read each script with open() and no encoding argument, i.e. the locale default encoding. That emits an EncodingWarning under -X warn_default_encoding and can mis-decode a script on a non-UTF-8 locale. Read the source with tokenize.open() instead, which decodes using the encoding detected from the file (its coding cookie, else UTF-8), matching how Python read the module when it was imported just above.
serhiy-storchaka
approved these changes
Aug 16, 2026
serhiy-storchaka
left a comment
Member
There was a problem hiding this comment.
LGTM. But did you consider using inspect.getsource()?
Member
|
AFAIK, turtledemo examples that are not all ascii (are their any?) are utf-8 encoded. Why not just add this to the open call? |
Per review, open the demo scripts with encoding='utf-8' instead of tokenize.open(); the scripts are UTF-8.
Contributor
Author
|
Good point, done. The demo scripts are UTF-8, so I switched to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
turtledemo's example viewer opened each demo script for display withopen(self.module.__file__, 'r')(noencoding), decoding with the localedefault encoding. That emits an
EncodingWarningunder-X warn_default_encodingand, on a non-UTF-8 locale, can mis-decode a UTF-8or coding-cookie'd demo, showing mojibake.
This reads the source with
tokenize.open()instead, which decodes using theencoding 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 imported one line above. It is the only unspecified-encoding
open()inLib/turtledemo(grep-verified).The viewer is Tkinter GUI code with no test harness (
test_turtle.pycoversthe
turtlemodule, not the demo runner), so this was verified manually inboth directions: under
-X warn_default_encodingthe oldopen()raises anEncodingWarningand the newtokenize.open()does not, while reading thefile unchanged.