Skip to content

gh-153319: Read turtledemo scripts as UTF-8 - #153321

Merged
terryjreedy merged 4 commits into
python:mainfrom
tonghuaroot:fix-turtledemo-encoding
Sep 20, 2026
Merged

terryjreedy merged 4 commits into
python:mainfrom
tonghuaroot:fix-turtledemo-encoding

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Stop using locale default encoding. All stdlib .py files (except for testing) are utf-8 encoded.

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

@terryjreedy terryjreedy 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.

It would be a bug if anyone merged a not-all-ascii script coded as anything other than utf8.

@terryjreedy

Copy link
Copy Markdown
Member

Re-running un-related failure of Docs check.

@terryjreedy

terryjreedy commented Sep 20, 2026

Copy link
Copy Markdown
Member

Repeat failure:

File "/home/runner/work/cpython/cpython/Doc/venv/lib/python3.14/site-packages/sphinx/builders/changes.py", line 61, in write_documents
        ttext = self.typemap[changeset.type]
                ~~~~~~~~~~~~^^^^^^^^^^^^^^^^
    KeyError: 'soft-deprecated'
The full traceback has been saved in:
/tmp/sphinx-err-mf7e_eg3.log
To report this error to the developers, please open an issue at <https://github.com/sphinx-doc/sphinx/issues/>. Thanks!
Please also report this if it was a user error, so that a better error message can be provided next time.
make: *** [Makefile:56: build] Error 2
make: Leaving directory '/home/runner/work/cpython/cpython/Doc'
Error: Process completed with exit code 2.

Updating branch since something in doc processing may have been updated since July.
Will rerun all tests.

@terryjreedy terryjreedy changed the title gh-153319: Read turtledemo scripts with the source file's own encoding gh-153319: Read turtledemo scripts as UTF-8 Sep 20, 2026
@terryjreedy
terryjreedy merged commit de0d976 into python:main Sep 20, 2026
52 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @terryjreedy for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Sep 20, 2026

Copy link
Copy Markdown

GH-157872 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 20, 2026
@bedevere-app

bedevere-app Bot commented Sep 20, 2026

Copy link
Copy Markdown

GH-157873 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 20, 2026
@bedevere-app

bedevere-app Bot commented Sep 20, 2026

Copy link
Copy Markdown

GH-157874 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Sep 20, 2026
@bedevere-app

bedevere-app Bot commented Sep 20, 2026

Copy link
Copy Markdown

GH-157873 is a backport of this pull request to the 3.14 branch.

@bedevere-app

bedevere-app Bot commented Sep 20, 2026

Copy link
Copy Markdown

GH-157874 is a backport of this pull request to the 3.13 branch.

terryjreedy added a commit that referenced this pull request Sep 20, 2026
Stop using locale default encoding. All stdlib .py files (except for testing) are utf-8 encoded.

---------
(cherry picked from commit de0d976)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
terryjreedy added a commit that referenced this pull request Sep 20, 2026
gh-153319: Read turtledemo scripts with the source file's own encoding (GH-153321)

Stop using locale default encoding. All stdlib .py files (except for testing) are utf-8 encoded.
---------
(cherry picked from commit de0d976)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
@hugovk

hugovk commented Sep 21, 2026

Copy link
Copy Markdown
Member

This is a small change, but is this actually needed for 3.15+?

All the 19 demos are pure ASCII, and from 3.15 UTF-8 mode is the default (PEP 686).

This would only be a problem if:

  1. we committed a non-ASCII demo
  2. and someone ran turtledemo on Windows (or legacy locale on Unix)
  3. and they explicitly disabled UTF-8 mode (for example PYTHONUTF8=0)

(And for 3.14 and earlier, would be a problem without step 3. Which I think is already quite rare.)

The last demo was added in 2015, and before that, 2009.

This PR feels both unnecessary, and the sort of churn we try to avoid.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants