Skip to content

gh-153319: Read turtledemo scripts with the source file's own encoding - #153321

Open
tonghuaroot wants to merge 3 commits into
python:mainfrom
tonghuaroot:fix-turtledemo-encoding
Open

tonghuaroot wants to merge 3 commits into
python:mainfrom
tonghuaroot:fix-turtledemo-encoding

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

turtledemo's example viewer opened each demo script for display with
open(self.module.__file__, 'r') (no encoding), decoding with the locale
default encoding. That emits an EncodingWarning under
-X warn_default_encoding and, on a non-UTF-8 locale, can mis-decode a UTF-8
or coding-cookie'd demo, showing mojibake.

This reads the source with tokenize.open() instead, 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 imported one line above. It is the only unspecified-encoding
open() in Lib/turtledemo (grep-verified).

The viewer is Tkinter GUI code with no test harness (test_turtle.py covers
the turtle module, not the demo runner), so this was verified manually in
both directions: under -X warn_default_encoding the old open() raises an
EncodingWarning and the new tokenize.open() does not, while reading the
file unchanged.

…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 serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. But did you consider using inspect.getsource()?

@serhiy-storchaka serhiy-storchaka added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes needs backport to 3.13 bugs and security fixes labels Aug 16, 2026
@terryjreedy

Copy link
Copy Markdown
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.
@tonghuaroot

Copy link
Copy Markdown
Contributor Author

Good point, done. The demo scripts are UTF-8, so I switched to open(..., encoding='utf-8') and dropped the tokenize import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants