Skip to content

gh-153333: Read tkinter profile scripts with the source file's encoding#153334

Merged
serhiy-storchaka merged 4 commits into
python:mainfrom
tonghuaroot:fix-tkinter-readprofile-encoding
Jul 11, 2026
Merged

gh-153333: Read tkinter profile scripts with the source file's encoding#153334
serhiy-storchaka merged 4 commits into
python:mainfrom
tonghuaroot:fix-tkinter-readprofile-encoding

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Tk.readprofile read the ~/.CLASS.py / ~/.BASE.py scripts with a plain open().read(), so they were decoded with the locale encoding and left open until GC. Read them in binary mode so exec uses each file's own coding cookie.

…encoding

Tk.readprofile execed the user's ~/.CLASSNAME.py and ~/.BASENAME.py profile scripts read via open() with no encoding argument, i.e. the locale default encoding. That emits an EncodingWarning under -X warn_default_encoding and mis-decodes a profile whose source carries a PEP 263 coding cookie. Read them with tokenize.open() instead, which decodes using the encoding detected from the file (its coding cookie, else UTF-8), matching how Python reads source. As an incidental effect of using the context-manager form, each script is now closed promptly after reading instead of at garbage collection, removing a ResourceWarning.

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

Simply open them in binary mode.

Pass the raw bytes to exec() so the compiler applies the source file's
encoding declaration, instead of detecting it with tokenize.open().

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

I left some comments to the test.

If you use AI to generate this PR, please tell it to simplify and trim the PR message and the commit message. Several times if needed. Nobody want to read such large boring text. Comments also can be shortened.

Comment thread Lib/test/test_tkinter/test_misc.py Outdated
Comment thread Lib/test/test_tkinter/test_misc.py Outdated
Comment thread Lib/test/test_tkinter/test_misc.py Outdated

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

Thank you. LGTM. 👍

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) July 11, 2026 04:12
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 11, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

I consider this a bug fix, but backporting it to released Python versions can break user code. So I only backport it to 3.15, which has not yet been released.

@serhiy-storchaka serhiy-storchaka merged commit bac73b0 into python:main Jul 11, 2026
53 checks passed
@miss-islington-app

Copy link
Copy Markdown

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

@miss-islington-app

Copy link
Copy Markdown

Sorry, @tonghuaroot and @serhiy-storchaka, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker bac73b0f4ea475cbfb11f41579129c9d07075c34 3.15

@bedevere-app

bedevere-app Bot commented Jul 11, 2026

Copy link
Copy Markdown

GH-153541 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 Jul 11, 2026
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 11, 2026
serhiy-storchaka added a commit that referenced this pull request Jul 11, 2026
… encoding (GH-153334) (GH-153541)

Tk.readprofile ran the user's ~/.CLASS.py and ~/.BASE.py scripts with
exec(open(path).read()), decoding them with the locale encoding.  Read
them in binary mode so exec() honors each script's own coding cookie.
(cherry picked from commit bac73b0)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
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.

2 participants